#!/bin/bash
# Disable gpg checking
[ -n "$NOGPGCHECK" ] && EXTRA_ARGS="${EXTRA_ARGS} --nogpgcheck"

# Set DNF extra vebosity
[ -n "$DEBUG" ] && EXTRA_ARGS="${EXTRA_ARGS} -v --debuglevel=5"

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

DNF_CONF="/etc/dnf/dnf.conf"
DNF_CMD="dnf -y -c $CHROOTDIR/$DNF_CONF --installroot $CHROOTDIR --setopt=reposdir=$CHROOTDIR/etc/yum.repos.d $EXTRA_ARGS"
DNF_REPOCMD="dnf config-manager -y -c $CHROOTDIR/$DNF_CONF --installroot $CHROOTDIR --setopt=reposdir=$CHROOTDIR/etc/yum.repos.d $EXTRA_ARGS"

distro_check() {
    if ! rpm -q dnf >/dev/null 2>&1 ; then
        echo "ERROR: Could not query RPM for DNF"
        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
    if [ ! -f "$CHROOTDIR/etc/openEuler-release" ]; then
        echo "ERROR: This must be a openEuler compatible chroot!"
        return 1
    fi
    PKGR_CMD="$DNF_CMD install $PKGLIST"
    return 0
}

prechroot() {
    set -x
    if [ -n "$OS_MIRROR" ]; then
        YUM_MIRROR="$OS_MIRROR"
    fi
    if [[ -z "$YUM_MIRROR" && -z "$INSTALL_ISO" ]]; then
        echo "ERROR: You must define the \$YUM_MIRROR variable in the template"
        cleanup
        return 1
    fi

    mkdir -p $CHROOTDIR/var/log
    mkdir -p $CHROOTDIR/var/cache

    mkdir -m 0755 -p $CHROOTDIR/$(dirname $DNF_CONF)

    echo "[main]" >> $CHROOTDIR/$DNF_CONF
    echo "keepcache=0" >> $CHROOTDIR/$DNF_CONF
    echo "debuglevel=2" >> $CHROOTDIR/$DNF_CONF
    echo "logfile=/var/log/dnf.log" >> $CHROOTDIR/$DNF_CONF
    echo "exactarch=1" >> $CHROOTDIR/$DNF_CONF
    echo "obsoletes=1" >> $CHROOTDIR/$DNF_CONF
    echo "gpgcheck=0" >> $CHROOTDIR/$DNF_CONF
    echo "plugins=1" >> $CHROOTDIR/$DNF_CONF
    echo "install_weak_deps=1" >> $CHROOTDIR/$DNF_CONF
    echo "cachedir=/var/cache/dnf" >> $CHROOTDIR/$DNF_CONF
    echo "" >> $CHROOTDIR/$DNF_CONF

    mkdir -p $CHROOTDIR/etc/yum.repos.d/
    # karl@koomie.com - I don't grok this next step. epol-update.repo is not owned by
    # any package - adding a file check since not present by default.
    
    if [ -e /etc/yum.repos.d/epol-update.repo ];then
	cp /etc/yum.repos.d/epol-update.repo $CHROOTDIR/etc/yum.repos.d/
    fi

    # Create new repos from comma-seperated list (using bash inline S&R)
    if [ "0$NOGPGCHECK" -eq "00" ]; then REPO_NOGPGCHECK=0; else REPO_NOGPGCHECK=1; fi
    for i in ${YUM_MIRROR//,/ }; do
        $DNF_REPOCMD --setopt=gpgcheck=$REPO_NOGPGCHECK --add-repo $i
    done

    PKGR_CMD="$DNF_CMD install $PKGLIST"
    set +x
    return 0
}

postchroot() {
    touch $CHROOTDIR/fastboot

    NETFILE=$CHROOTDIR/etc/sysconfig/network-scripts/network-functions
    if [ -f $NETFILE ]; then
        if grep -q 'rename_device' $NETFILE && ! grep -q 'rename_device() { return 0; }' $NETFILE; then
            echo "" >> $NETFILE
            echo "# This is a kludge added by Warewulf so devices don't get renamed (broke things with IB)" >> $NETFILE
            echo "rename_device() { return 0; }" >> $NETFILE
        fi
    fi

    # update default openEuler.repo to honor YUM_MIRROR_BASE if provided (karl@koomie.com)
    if [ -n "$YUM_MIRROR_BASE" ];then
        if [ -e $CHROOTDIR/etc/yum.repos.d/openEuler.repo ];then
            sed -i "s|http://repo.openeuler.org|${YUM_MIRROR_BASE}|g" $CHROOTDIR/etc/yum.repos.d/openEuler.repo
        fi
    fi

    return 0
}


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