#!/bin/bash
SOURCE="$0"
source /usr/lib/elive-tools/functions
EL_REPORTS="1"
#el_make_environment



main(){
    local is_ssh_wanted_start
    # debug mode
    if grep -Fqs "debug" /proc/cmdline ; then
        export EL_DEBUG=3
        if grep -Fqs "completedebug" /proc/cmdline ; then
            set -x
        fi
    fi

    RAM_TOTAL_mb="$( el_ram_usage_show total )"

    # set keyboard configurations for console
    if [[ -e "/var/lib/dpkg/info/console-setup.config" ]] ; then
        sudo -H -n dpkg-reconfigure -fnoninteractive console-setup
        echo -e "console-setup" >> /tmp/.packages-to-reconfigure
    fi

    # report possible errors from boot:
    for file in drivers-install-err persistence-elive-err persistence-elive-shutdown-err
    do
        if [[ -s "/root/.${file}.log" ]] ; then
            # fixes
            if [[ "$file" = *"drivers-install"* ]] ; then
                # we always have the "exec" error line, so skip it if only we have that:
                if [[ "$( cat "/root/.${file}.log" | wc -l )" -le 3 ]] ; then
                    continue
                fi
            fi
            # fixes
            if [[ "$file" = *"persistence-elive"* ]] ; then
                sudo -H sed -i -e '/^\/bin\/echo: /d' "/root/.${file}.log"
            fi

            # other error messages:
            if [[ -s "/root/.${file}.log" ]] ; then
                el_error "/root/.$file.log: $( cat "/root/.${file}.log" )"
                sudo -H rm -f "/root/.${file}.log"
            fi
        fi
    done


    # reconfigure SSH keys
    # note: we do this here instead of boot step (live-config) because it really slows down the startup almost hanging it!
    case "$( cat /etc/debian_version )" in
        7.*|"wheezy/"*)
            _PROTOCOLS="dsa rsa ecdsa"
            ;;
        10.*|"buster/"|11.*|"bullseye"*|12.*|"bookworm"*)
            _PROTOCOLS="dsa rsa ecdsa ed25519"
            ;;
        *)
            _PROTOCOLS="dsa rsa ecdsa ed25519"
            # el_warning "unknown debian version in $0"
            ;;
    esac

    # regenerate ssh seeds, always
    for _PROTOCOL in $_PROTOCOLS
    do
        if [ ! -e /etc/ssh/ssh_host_${_PROTOCOL}_key ] &&
           grep -Fqs "ssh_host_${_PROTOCOL}_key" /etc/ssh/sshd_config
        then
            sudo -H rm -f /etc/ssh/ssh_host_rsa_key* || true
            yes | sudo -H ssh-keygen -q -f /etc/ssh/ssh_host_${_PROTOCOL}_key -N "" -t ${_PROTOCOL}
        fi
    done

    # reload, only for secure cases (otherwise the machine is exposed to have a known-default-password on the network)
    source /etc/elive/machine-profile 1>/dev/null 2>&1 || true
    if [[ "$MACHINE_VIRTUAL" = "yes" ]] || ((is_thanatests)) ; then
        is_ssh_wanted_start=1
    fi
    #     if [ -e /var/lib/dpkg/info/systemd.list ] ; then
    #         sudo -H systemctl start ssh.service
    #     else
    #         sudo -H service ssh restart
    #     fi
    # else
    #     sudo -H systemctl stop ssh.service
    # fi

    # always unmask so the user can use it
    if [[ "$RAM_TOTAL_mb" -gt 1400 ]] ; then
        is_ssh_wanted_start=1
    fi

    if ((is_ssh_wanted_start)) ; then
        sudo -H killall -9 sshd 2>/dev/null || true
        sudo -H systemctl unmask ssh.service
        sudo -H systemctl enable ssh.service
        sudo -H systemctl start ssh.service
    fi


    # if we are debugging give it a little pause to see what is going on
    #if grep -Fqs "debug" /proc/cmdline ; then
        #echo -e "debug: sleep 2" 1>&2
        #sleep 2
    #fi
}

#
#  MAIN
#
main "$@"

# vim: set foldmethod=marker :
