#! /bin/sh
set -e

# Hide root warning for pip
export PIP_ROOT_USER_ACTION=ignore
export PIP_BREAK_SYSTEM_PACKAGES=1

msg()
{
    message=$1
    echo
    # Bold and orange font for linuxmuster.net
    echo "\e[1m\e[38;5;214m$message\e[39m\e[0m"
    echo
}

create_linuxmuster_venv()
{
    msg "Setting up virtual env in /opt"
    /usr/bin/python3 -m venv /opt/linuxmuster --system-site-packages
}

migrate_to_venv()
{
    /usr/bin/python3 -m pip uninstall -y aj ajenti-panel ajenti.plugin.ace ajenti.plugin.core ajenti.plugin.dashboard ajenti.plugin.filesystem ajenti.plugin.plugins ajenti.plugin.settings ajenti.plugin.session-list

    # Checking if the linuxmuster's venv  is already installed
    if [ -f /opt/linuxmuster/bin/activate ] ; then
      return
    fi

    create_linuxmuster_venv
}

case "$1" in
    install|configure)
        ajcfg="/etc/ajenti/config.yml"
        wucfg="/etc/linuxmuster/webui/config.yml"
        webuietc="/usr/lib/linuxmuster-webui/etc"
        update_scripts="$webuietc/update_scripts"
        install_scripts="$webuietc/install_scripts"
        config_templates="$webuietc/config_templates"

        # 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
            msg "--------------------------------------------------------------------
Installing linuxmuster-webui
--------------------------------------------------------------------"

            create_linuxmuster_venv
            . /opt/linuxmuster/bin/activate
            PYTHON3=/opt/linuxmuster/bin/python3

            # do first install
            $PYTHON3 -m pip install -U pip wheel #setuptools #distribute
            hash -r
            $PYTHON3 -m pip install -r $webuietc/requirements.txt

            msg "Copy default config files for webui"
            mkdir -p /etc/linuxmuster/webui/
            #
            if [ -f $ajcfg ];then
                msg "Backing up config.yml"
                cp -a $ajcfg $ajcfg.bak
            fi

            mkdir -p /srv/.webdav
            chmod 777 /srv/.webdav

            # Update 7.1.11, same scheme for all directories, prefer underscore to hyphen
            if [ -d "/etc/linuxmuster/webui/email-templates" ] ; then
                mv /etc/linuxmuster/webui/email-templates /etc/linuxmuster/webui/email_templates
            fi
            
            # Email templates will always be overwritten
            cp -a $webuietc/email_templates /etc/linuxmuster/webui/

            msg "Writing linuxmuster default ajenti config $ajcfg"
            
            mkdir -p /etc/ajenti
            cp -rf $config_templates/ajenti_default.yml $ajcfg
            
            if [ ! -f $wucfg ]; then
                # cp -n would be silent, but it's important to inform the user
                cp $config_templates/webui_default.yml  $wucfg
            else
                msg "$wucfg already exists ... not overwriting with the default config file."
            fi

            # Avoid empty smtp config file until Ajenti 2.2.1
            if  [ ! -f /etc/ajenti/smtp.yml ] ; then
              cat << EOF > /etc/ajenti/smtp.yml
smtp:
  password:
  port:
  server:
  user:
EOF
            fi

            # enable systemctl service
            msg "Configure systemctl.."
            systemctl daemon-reload
            systemctl enable linuxmuster-webui
            systemctl restart linuxmuster-webui

            # generate permissions file
            $PYTHON3 $update_scripts/merge-permissions.py

            touch $webuietc/.installed
            # No need to check the parents groups for the first install
            echo "parents_groups_checked" >> $webuietc/.installed

            ipaddress=$(hostname --ip-address)
            msg "--------------------------------------------------------------------
linuxmuster-webui is now installed but not initialised!

Please open http://$ipaddress in a browser to end the configuration
--------------------------------------------------------------------"

        else
            # do update
            msg "--------------------------------------------------------------------
Updating linuxmuster-webui
--------------------------------------------------------------------"

            msg "Stop linuxmuster-webui"
            systemctl stop linuxmuster-webui

            # Remove old lmn_session plugin
            rm -rf /usr/lib/linuxmuster-webui/plugins/lmn_session/

            migrate_to_venv
            . /opt/linuxmuster/bin/activate
            PYTHON3=/opt/linuxmuster/bin/python3

            msg "Upgrade pip to latest version"
            $PYTHON3 -m pip install pip -U

            msg "Remove deprecated plugins"
            $PYTHON3 -m pip uninstall -y ajenti.plugin.auth-users

            # Always backup config file if things are going bad
            msg "Backing up and updating config.yml"
            cp -a $ajcfg $ajcfg.bak
            cp -a $wucfg $wucfg.bak

            # Add new email parameters if needed ( Ajenti 2.1.44 )
            $PYTHON3 $update_scripts/update_ajenti_config.py

            # Split custom fields to /etc/linuxmuster/sophomorix/default-school (multischool-7.1.11)
            $PYTHON3 $update_scripts/clean_webui_config.py

            # Eventually move existing holiday file to /etc/linuxmuster/sophomorix/default-school (multischool-7.1.11)
            if [ -f /etc/linuxmuster/holidays.yml ] ; then
                mv /etc/linuxmuster/holidays.yml /etc/linuxmuster/sophomorix/default-school/holidays.yml
            fi

            msg "Check requirements"
            # Update Ajenti 2.1.44 : remove deprecated socketio library to avoid conflicts
            $PYTHON3 -c 'from socketio import mixins' 2>/dev/null && $PYTHON3 -m pip uninstall -y gevent-socketio-hartwork

            # Requirements must always be checked
            $PYTHON3 -m pip install -r $webuietc/requirements.txt

            msg "Set default sophomorix webui rights"
            $PYTHON3 $update_scripts/merge-permissions.py
            /usr/sbin/sophomorix-ui

            if grep -Fxq "parents_groups_checked" $webuietc/.installed ; then
                msg "Parents groups already checked!"
            else
                /usr/sbin/lmncli check_parents && echo "parents_groups_checked" >> $webuietc/.installed
            fi

            # Temporary folder for webdav downloads
            mkdir -p /srv/.webdav
            chmod 777 /srv/.webdav

            # Avoid empty smtp config file until Ajenti 2.2.1
            if  [ ! -f /etc/ajenti/smtp.yml ] ; then
              cat << EOF > /etc/ajenti/smtp.yml
smtp:
  password:
  port:
  server:
  user:
EOF
            fi

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

#DEBHELPER#

exit 0
