#!/bin/bash
# vim: ts=4 sw=4
# Installe les packages standards d'un serveur Easter-eggs
# Met en place les fichiers de configurations standards Easter-eggs
CONF=/etc/eeinstall/eeinstall.cfg

# Source configfile

if test -f $CONF
then
        . $CONF
else
        echo "No config file found, exiting..."
        exit 1
fi

AUTO=0
APTAUTO=""
if [ "x$2" == "xauto" ]
then
    AUTO=1
    APTAUTO=" -y "
fi

case $1 in
    base-deb)
        if test -x /usr/bin/apt-get && test -x /usr/bin/dpkg
        then
            if [ $AUTO -eq 0 ]
            then
                echo " Installing a base server:"
                echo " If your sources.list isn't ok, you can update it now."
                echo " (use CTRL+Z to put this process in background, modify"
                echo " your sources.list, then use \"fg\" to come back"
                echo " Press enter when ready for update :"
                read a
            fi
            echo " Updating database ..."
            apt-get $APTAUTO update
            PACKAGES=`mktemp $TEMPDIR/eeinstall_Packages.XXXXXX`
            cat /var/lib/apt/lists/*_Packages > $PACKAGES
            # Updating dselect database also
            dpkg --clear-avail
            # Do it twice to avoid dselect dtect "new" packages
            dpkg --update-avail $PACKAGES
            dpkg --update-avail $PACKAGES
            rm -f $PACKAGES
            # Preseed
            cat $DATADIR/eeinstall.preseed | debconf-set-selections
            # Installation
            echo " Installing packages..."
            # Mark all except kernel-images to be purged
            dpkg --get-selections '*' | grep -Ev "^kernel-image|^linux-image|^grub|^lilo|^mdadm|^firmware-|^libc6-i|^libc6-amd64|^libfreetype6|^efibootmgr|^ucf|^shim|^mokutil|^intel-microcode|^iucode-tool" | awk '{print $1}' | sed 's/$/ purge/'  | dpkg --set-selections
            # Mark packages we want to be installed
            cat $DATADIR/base | sed 's/$/ install/' | dpkg --set-selections
            # Perform the install/removal
            apt-get $APTAUTO dselect-upgrade
            apt-get $APTAUTO clean
            # Cleanup aptitude state
            rm -f /var/lib/aptitude/pkgstates
            # Cleanup apt state
            rm -f /var/lib/apt/extended_states
            aptitude $APTAUTO update
            aptitude $APTAUTO dist-upgrade

        else
            echo " Apt-get or/and dpkg is/are missing"
            exit 1
        fi
    ;;
    base-conf)
    if [ ! -f /etc/postfix/main.cf ]
    then
        echo -n "Initializing postfix main.cf: "
        HOSTNAME=`hostname`
        DOMAINNAME=`hostname -d`
        if [ -f /var/lib/eeinstall/data/main.cf.template ]
        then
            cat /var/lib/eeinstall/data/main.cf.template | sed "s/{HOSTNAME}/$HOSTNAME/g;s/{DOMAINNAME}/$DOMAINNAME/g" > /etc/postfix/main.cf

            if [ -z $DOMAINNAME ]
            then
                echo "No domain has been defined, please set it in /etc/postfix/main.cf"
            else
                newaliases
            fi
        else
            echo "no main.cf template!"
        fi
        echo "done."
    fi
    ;;
    base)
        $0 base-deb $2
        $0 base-conf $2
    ;;
    *)
        echo "Usage: eeinstall {base|base-deb}"
        echo ""
        echo "base       : full installation"
        echo "base-deb   : install only packages"
        echo "base-conf     : Initialize standard confs"
        exit 1
    ;;

esac
