#!/bin/bash

USAGE="\n
crx_setup_agama_install_environment.sh  [-v Version] [-ueh ]\n
\n
\t\t-o\t\tSynchronize the repositories. Configure offline installation.\n
\t\t-e\t\tSynchronize education repository.\n
\t\t-p\t\tSynchronize PACKMAN repository.\n
\t\t-h\t\tThis page\n
"

. /etc/sysconfig/cranix
VERSION="16.0"
OFFLINE="no"
UPDATE="no"
EDUCATION="no"
PACKMAN="no"
SERVER="rsync.opensuse.org"
DATE=$( /usr/share/cranix/tools/crx_date.sh )

while getopts "V:oueph" c
do
    case $c in
        V)  VERSION="$OPTARG"
            ;;
        o)  OFFLINE="yes"
            ;;
        u)  UPDATE="yes"
            ;;
        e)  EDUCATION="yes"
            ;;
        p)  PACKMAN="yes"
            ;;
        h)  echo -e $USAGE 1>&2
            exit 2
            ;;
    esac
done

mkdir -p /srv/ftp/{akt,agama}

wget http://downloads.cephalix.eu/agama/Leap/linux  -O /srv/tftp/boot/linux
wget http://downloads.cephalix.eu/agama/Leap/initrd -O /srv/tftp/boot/initrd
wget http://downloads.cephalix.eu/agama/Leap/squashfs.img  -O /srv/ftp/agama/squashfs.img
wget http://downloads.cephalix.eu/agama/Leap/nsswitch.conf -O /srv/ftp/agama/nsswitch.conf

if [ "${OFFLINE}" == "yes" ]; then
    wget http://downloads.cephalix.eu/agama/Leap/cranix-client-offline.jsonnet -O /srv/ftp/agama/cranix-client.jsonnet.in
    INSTALL_URL="inst.install_url=ftp://install/akt/oss/"
else
    wget http://downloads.cephalix.eu/agama/Leap/cranix-client.jsonnet -O /srv/ftp/agama/cranix-client.jsonnet.in
fi

# Adapt autoinstallation jsonnet file
cp /etc/sssd/sssd.conf /srv/ftp/agama/
chmod 644 /srv/ftp/agama/*
PUBKEYFILE=$( ls /root/.ssh/id*.pub | head -n1 )
SSHPUBKEY=$( cat $PUBKEYFILE )
sed "s#SSHPUBKEY#${SSHPUBKEY}#" /srv/ftp/agama/cranix-client.jsonnet.in > /srv/ftp/agama/cranix-client.jsonnet

sed -i "s/VERSION/${VERSION}/g" /srv/ftp/agama/cranix-client.jsonnet

echo "###############################################"
echo -n "Insert password for local user"
PASSWD=$( openssl passwd -6 )
sed -i "s#PWLOCALUSER#$PASSWD#" /srv/ftp/agama/cranix-client.jsonnet
echo ""
echo ""
echo "###############################################"
echo -n "Insert password for the user root"
PASSWD=$( openssl passwd -6 )
sed -i "s#PWROOT#$PASSWD#" /srv/ftp/agama/cranix-client.jsonnet

if [ "${OFFLINE}" == "yes" ]; then
    echo "Now we start download the repositories"
    RSYNC_PARAMS="-avz --stats --delete --delete-after --exclude=src --exclude=ppc64le --exclude=aarch64 --exclude=aarch64_ilp32 --exclude=armv7hl --exclude=s390x"

    rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-full/opensuse/distribution/leap/${VERSION}/repo/oss/     /srv/ftp/akt/oss/
    rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-full/opensuse/distribution/leap/${VERSION}/repo/non-oss/ /srv/ftp/akt/non-oss/

    if [ $UPDATE = "yes" ]; then
        rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-updates/leap/${VERSION}/oss/     /srv/ftp/akt/updates/
        rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-updates/leap/${VERSION}/non-oss/ /srv/ftp/akt/updates-non-oss/
        rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-updates/leap/${VERSION}/sle/     /srv/ftp/akt/updates-sle/
        echo "#!/bin/bash
/usr/bin/rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-updates/leap/${VERSION}/oss/     /srv/ftp/akt/updates/
/usr/bin/rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-updates/leap/${VERSION}/non-oss/ /srv/ftp/akt/updates-non-oss/
/usr/bin/rsync ${RSYNC_PARAMS} ${SERVER}::opensuse-updates/leap/${VERSION}/sle/     /srv/ftp/akt/updates-sle/
" > /etc/cron.weekly/crx.openSUSE-updates
    fi

    #Download Education Packages
    if [ "$EDUCATION" = "yes" ]; then
    	rsync ${RSYNC_PARAMS} ${SERVER}::buildservice-repos-main/Education/${VERSION}/     /srv/ftp/akt/education/
        echo "#!/bin/bash
/usr/bin/rsync ${RSYNC_PARAMS} ${SERVER}::buildservice-repos-main/Education/${VERSION}/     /srv/ftp/akt/education/
    " > /etc/cron.weekly/crx.Education-update
    fi
    
    #Dowload PACMAN
    if [ "$PACKMAN" = "yes" ]; then
        rsync ${RSYNC_PARAMS} rsync://ftp.halifax.rwth-aachen.de/packman/suse/openSUSE_Leap_$VERSION/ /srv/ftp/akt/packman/
        echo "#!/bin/bash
/usr/bin/rsync ${RSYNC_PARAMS} rsync://ftp.halifax.rwth-aachen.de/packman/suse/openSUSE_Leap_$VERSION/ /srv/ftp/akt/packman/
    " > /etc/cron.weekly/crx.PACKMAN-update
    fi
    
    #Make updte cron jobs executable:
chmod 755 /etc/cron.weekly/crx.*
fi

grep -q 'Install Cranix Client'  /srv/tftp/efi/grub.cfg ||  {
echo "
menuentry 'Install Cranix Client' {
  set timeout=120
  echo 'Loading kernel ...'
  linuxefi boot/linux ip=dhcp root=live:ftp://install/agama/squashfs.img inst.auto=ftp://install/agama/cranix-client.jsonnet inst.auto_insecure ${INSTALL_URL}
  echo 'Loading initial ramdsik ...'
  initrdefi boot/initrd
}
" >> /srv/tftp/efi/grub.cfg
}

grep -q 'Install Cranix Client'  /srv/tftp/pxelinux.cfg/default || {
echo "
LABEL Install Cranix Client
        kernel boot/linux
        append initrd=boot/initrd ip=dhcp root=live:ftp://install/agama/squashfs.img inst.auto=ftp://install/agama/cranix-client.jsonnet inst.auto_insecure ${INSTALL_URL}
" >> /srv/tftp/pxelinux.cfg/default
}


zypper -n install nfs-kernel-server

grep -q '^/home' /etc/exports || {
echo "/home   ${CRANIX_NETWORK}/${CRANIX_NETMASK}(rw,async,no_subtree_check,no_root_squash,insecure)" >> /etc/exports
}

systemctl start nfs-server
systemctl enable nfs-server
