#!/bin/sh
set -e

case "$1" in
    install|configure)
        ajcfg="/etc/ajenti/config.yml"
        wucfg="/etc/linuxmuster/webui/config.yml"
        webuietc="/usr/lib/linuxmuster-webui/etc"
        python_version=$(basename $(readlink -f /usr/bin/python3))

        # circumvent missing .installed files in package. Can be removed later
        if [ -f /etc/linuxmuster/.secret/administrator ]; then
            touch $webuietc/.installed
        fi

        if [ ! -f $webuietc/.installed ];
        then
            echo "---------------------------------------------------------------"
            echo "Installing linuxmuster-webui"
            echo "---------------------------------------------------------------"
            # do first install
            /usr/bin/python3 -m pip install -U pip wheel setuptools #distribute
            hash -r
            /usr/bin/python3 -m pip install -r $webuietc/requirements.txt
            echo "Copy default config files for webui"
            mkdir -p /etc/linuxmuster/webui/
            #
            if [ -f $ajcfg ];then
                echo "Backing up config.yml"
                cp -a $ajcfg $ajcfg.bak
            fi
            echo "Writing linuxmuster default ajenti config $ajcfg"
            cp -rf /usr/lib/linuxmuster-webui/etc/ajenti_default.yml $ajcfg
            if [ ! -f $wucfg ]; then
                # cp -n would be silent, but it's important to inform the user
                cp /usr/lib/linuxmuster-webui/etc/webui_default.yml  $wucfg
            else
                echo "$wucfg already exists ... not overwriting with the default config file."
            fi
            # systemctl einrichten und starten
            echo "Configure systemctl.."
            systemctl daemon-reload
            systemctl enable linuxmuster-webui
            systemctl restart linuxmuster-webui
            touch $webuietc/.installed
            hostname=$(hostname --fqdn)
            ipaddress=$(hostname --ip-address)
            echo "---------------------------------------------------------------"
            echo "linuxmuster-webui is now installed but not initialised!  http://$ipaddress"
            echo "---------------------------------------------------------------"
        else
            # do update
            echo "---------------------------------------------------------------"
            echo "Updating linuxmuster-webui"
            echo "---------------------------------------------------------------"
            echo "Stop linuxmuster-webui"
            systemctl stop linuxmuster-webui
            
            # Always backup config file if things are going bad
            echo "Backing up config.yml"
            cp -a $ajcfg $ajcfg.bak
            
            # Requirements must always be checked
            /usr/bin/python3 -m pip install -r $webuietc/requirements.txt
            
            # For an upgrade Python2 --> Python3 the new installation of Ajenti will overwrite config.yml
            if ! /usr/bin/python3 -c "import aj" &> /dev/null ; then
                # Module aj not present, so upgrade to Python3
                cp -a $ajcfg.bak $ajcfg
            fi
            # Add fqdn_certificate in config.yml if not present - for update ajenti 2.1.33
            if [ "$(grep -e '^\s\{2,\}fqdn_certificate' /etc/ajenti/config.yml | wc -l)" -eq 0 ] ; then
                echo "Add FQDN certificate parameter to /etc/ajenti/config.yml"
                sed -i '/^\s\{2,\}certificate:/p;0,/^\s\{2,\}certificate:/ s/certificate/fqdn_certificate/' $ajcfg
            fi
            
            echo "Set default sophomorix webui rights"
            /usr/sbin/sophomorix-ui


            echo "Create webuiUploadfolder"
            if [ "$(grep -e 'active directory domain controllers' /etc/samba/smb.conf | wc -l)" -ne 0 ] ; then
                    installed=$(grep -e 'active directory domain\ controller' /etc/samba/smb.conf)
            fi
            mkdir -p /srv/webuiUpload/
            if [ -z "$installed" ];then
                echo "not installed skip setting acl on webuiUpload"
            else
                echo "setting rights"
                workgroup=$(cat /etc/samba/smb.conf | grep workgroup | awk '{print $3}')
                sed -i "s/LINUXMUSTER/$workgroup/" /usr/lib/linuxmuster-webui/etc/webuiUpload.ntacl
                setfacl --set-file /usr/lib/linuxmuster-webui/etc/webuiUpload.ntacl /srv/webuiUpload
            fi

            
            # start webui
            echo "Start linuxmuster-webui"
            systemctl daemon-reload
            systemctl start linuxmuster-webui
            echo "---------------------------------------------------------------"
            echo "Updating linuxmuster-webui ..done"
            echo "---------------------------------------------------------------"
        fi
    ;;
    upgrade|abort-upgrade)
    ;;
    
    *)
        echo "postinst called with unknown argument \`$1'" >&2
        exit 0
    ;;
esac

#DEBHELPER#

exit 0
