#!/bin/bash
# Includes file for Red Hat and Fedora-based Linux distributions
# RHEL, CentOS, Fedora, and Scientific Linux

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

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

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

# Check if we're building the chroot from RHEL/CentOS 8
if [ -z "$PKG_MGR" ]; then
    if [ $(awk -F= '/VERSION_ID/ {print $2}' < /etc/os-release) = '"8"' ]; then
        PKG_MGR="dnf"
    fi
fi

if [ "$PKG_MGR" = "dnf" ]; then
    YUM_CONF="/etc/dnf/dnf.conf"
    YUM_CMD="dnf -y -c $CHROOTDIR/$YUM_CONF --installroot $CHROOTDIR --setopt=reposdir=$CHROOTDIR/etc/yum.repos.d $EXTRA_ARGS" 
    YUM_REPOCMD="dnf config-manager -y -c $CHROOTDIR/$YUM_CONF --installroot $CHROOTDIR --setopt=reposdir=$CHROOTDIR/etc/yum.repos.d $EXTRA_ARGS"
else
    PKG_MGR="yum"    
    YUM_CONF="/etc/yum.conf"
    YUM_CMD="yum -y -c $CHROOTDIR/$YUM_CONF --tolerant --installroot $CHROOTDIR $EXTRA_ARGS"
    YUM_REPOCMD="yum-config-manager -y -c $CHROOTDIR/$YUM_CONF --tolerant --installroot $CHROOTDIR $EXTRA_ARGS"
fi

distro_check() {
    if [ "$PKG_MGR" = "dnf" ]; then
        if ! dnf --version >/dev/null 2>&1 ; then
        echo "ERROR: dnf command not available"
        return 1
        fi
    else 
        if ! yum --version >/dev/null 2>&1 ; then
        echo "ERROR: yum command not available"
        return 1
        fi
        if ! yum-config-manager --version >/dev/null 2>&1 ; then
        echo "ERROR: yum-config-manager command not available"
        return 1
        fi
    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/redhat-release" ]; then
        echo "ERROR: This must be a Red Hat compatible chroot!"
        return 1
    fi
    PKGR_CMD="$YUM_CMD install $PKGLIST"
    return 0
}

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

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

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

    mkdir -m 0755 -p $CHROOTDIR/$(dirname $YUM_CONF)
    
    # Write a yum or dnf config file, with minor differences
    echo "[main]" > $CHROOTDIR/$YUM_CONF
    echo "keepcache=0" >> $CHROOTDIR/$YUM_CONF
    echo "debuglevel=2" >> $CHROOTDIR/$YUM_CONF
    echo "logfile=/var/log/$PKG_MGR.log" >> $CHROOTDIR/$YUM_CONF
    echo "obsoletes=1" >> $CHROOTDIR/$YUM_CONF
    echo "gpgcheck=0" >> $CHROOTDIR/$YUM_CONF
    echo "plugins=1" >> $CHROOTDIR/$YUM_CONF
    echo "reposdir=0" >> $CHROOTDIR/$YUM_CONF
    echo "reposdir=0" >> $CHROOTDIR/$YUM_CONF
    if [ "$PKG_MGR" = "dnf" ]; then
        echo "install_weak_deps=0" >> $CHROOTDIR/$YUM_CONF
        echo "cachedir=/var/cache/dnf" >> $CHROOTDIR/$YUM_CONF
        # New DNF requires the os-release file with platform ID
        echo "PLATFORM_ID=\"$PLATFORMID\"" > $CHROOTDIR/etc/os-release
    else
        echo "exactarch=1" >> $CHROOTDIR/$YUM_CONF
        echo "cachedir=/var/cache/yum/\$basearch/\$releasever" >> $CHROOTDIR/$YUM_CONF
    fi
    echo "" >> $CHROOTDIR/$YUM_CONF

    # Add all of the repos found on the ISO image
    if [ -n "$INSTALL_ISO" ]; then
        for i in $(find $MEDIA_MOUNTPATH -type d -name repodata); do
             INSTALLDIRS="$INSTALLDIRS,file://$(dirname $i)"
        done
        YUM_MIRROR="$YUM_MIRROR,$INSTALLDIRS"
    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 [ "0$NOGPGCHECK" -eq "00" ]; then REPO_NOGPGCHECK=0; else REPO_NOGPGCHECK=1; fi
    for i in ${YUM_MIRROR//,/ }; do
        $YUM_REPOCMD --setopt=gpgcheck=$REPO_NOGPGCHECK --add-repo $i
    done

    PKGR_CMD="$YUM_CMD install $PKGLIST"
    return 0
}

postchroot() {
    touch $CHROOTDIR/fastboot

    # If /etc/os-release.rpmnew exists, make it the real /etc/os-release
    if [ -f $CHROOTDIR/etc/os-release.rpmnew ]; then
        mv $CHROOTDIR/etc/os-release $CHROOTDIR/etc/os-release.warewulf
        mv $CHROOTDIR/etc/os-release.rpmnew $CHROOTDIR/etc/os-release
    fi

    # we only want to add this kludge to the file once. lets make sure an
    # improperly writen overlay template wont cause repeated file concatenation
    # network-functions is not present on RHEL 8
    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

    # Dracut on newer releases doesn't work within a chroot without sys/proc filesystems mounted.
    # Post-install scripts on kernel-core RPM will fail
    # Manually add a pointer to the Linux kernel to /boot
    # This provides for initrd images to be created from the chroot.
    for i in $(find $CHROOTDIR/lib/modules -path /*/vmlinuz -printf "%P\n" | cut -d / -f 1); do
        if [ ! -f /$CHROOTDIR/boot/vmlinuz-$i ]; then
            ln -s ../lib/modules/$i/vmlinuz $CHROOTDIR/boot/vmlinuz-$i
        fi
    done

    return 0
}

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