#! /bin/bash

# Include file for SUSE Linux distributions
# Use this for SLE, OpenSUSE LEAP, and Tumbleweed

distro_check() {
    if ! zypper --version >$DEVNULL ; then
        echo "ERROR: zypper command not available"
        return 1
    fi

    # Disable gpg checking
    [ -n "$NOGPGCHECK" ] && EXTRA_ARGS="${EXTRA_ARGS} --no-gpg-checks"

    # Set zypper excess vebosity
    [ -n "$DEBUG" ] && EXTRA_ARGS="${EXTRA_ARGS} -vv"

    # Make zypper quiet if verbose and debug are not set
    [ -z "$VERBOSE" -a -z "$DEBUG" ] && EXTRA_ARGS="${EXTRA_ARGS} --quiet"

    ZYP_CMD="zypper --root $CHROOTDIR -n --gpg-auto-import-keys ${EXTRA_ARGS} -c $CHROOTDIR/etc/zypp/zypp.conf"
    [ -n "$DEBUG" ] && echo "ZYP_CMD = $ZYP_CMD" 

    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
    PKGR_CMD="$ZYP_CMD install --auto-agree-with-licenses --no-recommends $PKGLIST"
    [ -n "$DEBUG" ] && echo "PKGR_CMD=$PKGR_CMD" 
    return 0
}

prechroot() {
    # Configure compute nodes to use the latest single kernel, unless overridden
    [ -z "$KVERSION" ] && KVERSION="latest"

    if [ -n "$OS_MIRROR" -a -z "$ZYPP_MIRROR" ]; then
        ZYPP_MIRROR="$OS_MIRROR"
    fi

    if [ -z "$ZYPP_MIRROR" -a -z "$INSTALL_ISO" ]; then
        echo "ERROR: You must define the \$ZYPP_MIRROR or \$INSTALL_ISO variable"
        cleanup
        return 1
    fi

    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

    # Create new zypp.conf and zypper.conf files
    echo "[main]" > $CHROOTDIR/etc/zypp/zypper.conf	
    echo "[solver]" >> $CHROOTDIR/etc/zypp/zypper.conf
    echo "installRecommends = no" >> $CHROOTDIR/etc/zypp/zypper.conf
    echo "[commit]" >> $CHROOTDIR/etc/zypp/zypper.conf
    echo "autoAgreeWithLicenses = yes" >> $CHROOTDIR/etc/zypp/zypper.conf
    echo "[search]" >> $CHROOTDIR/etc/zypp/zypper.conf
    echo "[color]" >> $CHROOTDIR/etc/zypp/zypper.conf
    echo "[obs]" >> $CHROOTDIR/etc/zypp/zypper.conf
    [ -n "$VERBOSE" ] && echo "Created zypper.conf"
    [ -n "$DEBUG" ] && cat $CHROOTDIR/etc/zypp/zypper.conf
    echo "[main]" > $CHROOTDIR/etc/zypp/zypp.conf
    echo "solver.dupAllowVendorChange = false" >> $CHROOTDIR/etc/zypp/zypp.conf
    echo "multiversion = provides:multiversion(kernel)" >> $CHROOTDIR/etc/zypp/zypp.conf
    echo "multiversion.kernels = $KVERSION" >> $CHROOTDIR/etc/zypp/zypp.conf
    [ -n "$VERBOSE" ] && echo "Created zypp.conf"
    [ -n "$DEBUG" ] && cat z$CHROOTDIR/etc/zypp/zypp.conf

    # Add all of the repos found on the ISO images

    if [ -n "$INSTALL_ISO" ]; then
        for i in $(find $MEDIA_MOUNTPATH -type d -name repodata -printf "%P "); do
             [ -n "$VERBOSE" ] && echo "Found repodata at $i"
             INSTALLDIRS="$INSTALLDIRS,file:///tmp/wwmkchroot.mount/$(dirname $i)"
        done
        ZYPP_MIRROR="$ZYPP_MIRROR,$INSTALLDIRS"
        [ -n "$DEBUG" ] && echo "ZYPP_MIRROR = $ZYPP_MIRROR"
    fi

    # Remove all existing repos in CHROOT; just a precaution
    rm -f $CHROOTDIR/etc/yum.repos.d/*


    # Create new repos from comma-seperated list (using bash inline S&R)
    if [ "01" -eq "0$REPO_NOGPGCHECK" ]; then
        GPGARG="-G"
    else
        GPGARG="-g"
    fi
    [ -n "$DEBUG" ] && echo "GPGARG = $GPGARG"
    cnt=0
    # Bash pattern replace -- replace all commas with spaces
    for i in ${ZYPP_MIRROR//,/ }; do
        [ -n "$VERBOSE" ] && echo "Adding repo-$cnt: $i"
        $ZYP_CMD ar $GPGARG $i repo-$cnt
        let cnt++
    done

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

    return 0
}

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


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

