ZYP_CMD="zypper -n --gpg-auto-import-keys"

distro_check() {
    if ! rpm -q zypper >/dev/null 2>&1 ; then
        echo "ERROR: Could not query RPM for Zypper"
        return 1
    fi
    return 0
}

set_overlay() {
    if [ ! -d "$CHROOTDIR" -o ! -x "$CHROOTDIR/sbin/init" ]; then
        echo "ERROR: This is an overlay that must work on an existing chroot!"
        return 1
    fi
    suse=0
    if [ -f "$CHROOTDIR/etc/os-release" ]; then
        if [[ "$(grep "ID_LIKE" $CHROOTDIR/etc/os-release | \
            awk -F= '{print $2;}')" =~ .*suse.* ]]; then
            suse=1
        fi
    elif [  -f "$CHROOTDIR/etc/SuSE-release" ]; then
        suse=1
    fi
    if [ $suse -eq 0 ]; then
        echo "ERROR: This must be a SUSE compatible chroot!"
       return 1
    fi
    # If no repos are available in the chroot assume they've been taken from
    # the host.
    chrootarg="--root $CHROOTDIR"
    if [ -z "$ZYP_MIRROR" -a -z "$INSTALL_ISO" ]; then
        echo "INFO: Taking repository information from running system"
	chrootarg="--installroot $CHROOTDIR"
    fi
    [ -n "$VERBOSE" ] && echo "Executing zypper with $chrootarg"
    PKGR_CMD="$ZYP_CMD $chrootarg install --auto-agree-with-licenses $PKGLIST"

    return 0
}

prechroot() {
    if [ -n "$OS_MIRROR" ]; then
        ZYP_MIRROR="$OS_MIRROR"
    fi

    if [ -e "/etc/os-release" ]; then
        VERSION=`rpm -qf /etc/os-release  --qf '%{VERSION}\n'`
    else
        VERSION=`rpm -qf /etc/SuSE-release  --qf '%{VERSION}\n'`
    fi
    mkdir -p $CHROOTDIR
    mkdir -p -m 755 $CHROOTDIR/etc
    mkdir -p -m 755 $CHROOTDIR/etc/zypp
    mkdir -p -m 755 $CHROOTDIR/dev
    rm -f $CHROOTDIR/dev/zero && mknod -m 666 $CHROOTDIR/dev/zero c 1 5

     chrootarg="--root $CHROOTDIR"
     # Use repos of installed system
    if [[ -z "$ZYP_MIRROR" && -z "$INSTALL_ISO" ]]; then
         chrootarg="--installroot $CHROOTDIR"
    elif [ -n "$INSTALL_ISO" ]; then
       cp -rap /etc/zypp/zypper.conf $CHROOTDIR/etc
       # Use ISO
        for mountpath in `ls -d $MEDIA_MOUNTPATH.*`; do
            if [ -z "$INSTALLDIRS" ]; then
                if [ -d ${mountpath}/repodata ]; then
                    # RHEL 6.x
                    INSTALLDIRS="file://${mountpath}"
                elif [ -d ${mountpath}/Server/repodata ]; then
                    # RHEL 5.x
                    INSTALLDIRS="file://${mountpath}/Server"
                fi
            else
                INSTALLDIRS="$INSTALLDIRS,file://${mountpath}"
            fi
        done
        ZYP_MIRROR=$INSTALLDIRS
    else
        cp -rap /etc/zypp/zypper.conf $CHROOTDIR/etc
    fi
    [ -n "$VERBOSE" ] && echo "Executing zypper with $chrootarg"
    if [ -n "${ZYP_MIRROR}" ]; then
        declare -i i=0 cnt
        cnt=$($ZYP_CMD $chrootarg lr | grep '|' | wc -l)
        while [ $i -lt $cnt ]; do
            $ZYP_CMD $chrootarg rr $i
            let i++
        done

        cnt=0
        for repourl in `echo $ZYP_MIRROR | sed -e 's/,/ /'`; do
            $ZYP_CMD $chrootarg ar $repourl repo-$((cnt++))
        done
    fi

    PKGR_CMD="$ZYP_CMD $chrootarg install --auto-agree-with-licenses $PKGLIST"

    return 0
}

postchroot() {
    touch $CHROOTDIR/fastboot
    return 0
}


# vim:filetype=sh:syntax=sh:expandtab:ts=4:sw=4:
