#!/bin/bash
#
# Kanotix-Update-GUI - (pseudo) initscript
#
# written by Andreas Loibl (http://www.andreas-loibl.de)
#

# LOCALE setzen
if [ -f /etc/sysconfig/i18n ]; then
    . /etc/sysconfig/i18n
    [ -n "$LANG" ] || LANG="de_DE@euro"
    export LANG
    [ -n "$LC_CTYPE" ] && export LC_CTYPE || unset LC_CTYPE
    [ -n "$LC_COLLATE" ] && export LC_COLLATE || unset LC_COLLATE
    [ -n "$LC_MESSAGES" ] && export LC_MESSAGES || unset LC_MESSAGES
    [ -n "$LC_NUMERIC" ] && export LC_NUMERIC || unset LC_NUMERIC
    [ -n "$LC_MONETARY" ] && export LC_MONETARY || unset LC_MONETARY
    [ -n "$LC_TIME" ] && export LC_TIME || unset LC_TIME
    [ -n "$LC_ALL" ] && export LC_ALL || unset LC_ALL
    [ -n "$LANGUAGE" ] && export LANGUAGE || unset LANGUAGE
    [ -n "$LINGUAS" ] && export LINGUAS || unset LINGUAS
    [ -n "$_XKB_CHARSET" ] && export _XKB_CHARSET || unset _XKB_CHARSET

    if [ -n "$SYSFONTACM" ]; then
        case $SYSFONTACM in
            iso01*|iso02*|iso15*|koi*|latin2-ucw*)
                if [ "$TERM" = "linux" ]; then
                    if ls -l /proc/$$/fd/0 2>/dev/null | grep -- '-> /dev/tty[0-9]*$' >/dev/null 2>&1; then
                        echo -n -e '\033(K' > /proc/$$/fd/0
                    fi
                fi
                ;;
        esac
    fi
    unset SYSFONTACM
fi # <-- Ende LOCALE

export TEXTDOMAINDIR="/usr/share/locale"
export TEXTDOMAIN=kanotix-update-gui

# PATH setzen
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/X11R6/bin:/usr/local/sbin:/usr/local/bin"
export PATH

function kernelUpdate()
{
    # Das Script wird nach einem Kernelupdate ausgefhrt

    echo
    echo -e $"Kanotix-Update-GUI is now committing the changes necessary after a kernel update, please stand by..."
    echo

    TEMP_FILE="/tmp/temp.$RANDOM"

    while true
    do
        dialog --yesno $"Kanotix-Update-GUI is now committing the changes necessary after a kernel update.\n\nDo you want to proceed?" 0 0
    
        if [ $? -ne 0 ]; then
            # Der User will nicht automatisch fortfahren
            dialog --default-item " " --no-cancel --menu $"What do you want to do next?" 0 0 7 " " $"Back (prev. menu)" "1" $"Console (runlevel 3)" "2" $"Graphical interface (aka X)" "3" $"reboot computer" "4" $"shut down computer" 2>"$TEMP_FILE"
            SELECT=$(cat "$TEMP_FILE")
            case "$SELECT" in
            1)
                update-rc.d -f kanotix-update-gui remove # Das Script vom Bootprozess austragen
                rm "$TEMP_FILE" # die temporre Datei lschen
                echo
                echo $"Press <Enter> to log on to a console..."
                exit 0
                ;;
            2)
                dialog --yesno $"Note: The recommended changes have NOT been committed yet.\n\nDo you really want to switch back to the graphical interface?" 0 0
                if [ $? -eq 0 ]; then
                    update-rc.d -f kanotix-update-gui remove # Das Script vom Bootprozess austragen
                    rm "$TEMP_FILE" # die temporre Datei lschen
                    init 5
                    exit 1
                fi
                ;;
            3)
                reboot
                ;;
            4)
                halt
                ;;
            esac
        else
            break
        fi
    done
    
    # Folgende Befehle mssen nach einem Update ausgefhrt werden
    # (steht so im Wiki; hier noch erweitert)
    update-modules                  # /etc/modules-$(uname -r) updaten
    fix-cdrom || fix-cdrom.bash     # fix-cdrom (oder fix-cdrom.bash auf alten Kanotixen)
    fix-fonts || fix-fonts.sh       # fix-fonts (oder fix-fonts.sh auf alten Kanotixen)
    /etc/init.d/autofs restart      # autofs neustarten
    alsaconf                        # Alsa-Karten neu erkennen und die Treiber laden
    detect-fc.bash                  # Fritz!Card erkennen
    update-modules                  # /etc/modules-$(uname -r) nochmal updaten (z.B. wegen alsaconf (???))

    # den Bootsplash wieder installieren
    if ( dpkg -L bootsplash 2>&1 >/dev/null && which install-bootsplash-acritox.sh >/dev/null ); then
        # Paket 'bootsplash' ist installiert
        dialog --defaultno --yesno $"Do you want to enable your boot splash in grub? [experimental]" 0 0
        if [ $? -eq 0 ]; then
            # Ja, der User will es installieren
            install-bootsplash-acritox.sh
        fi
    fi

    # Hier muss jetzt noch der Grafiktreiber installiert werden
    # (falls man die NVidia- oder ATI-3D-Beschleunigung will)
    dialog --menu $"Do you want to install graphics drivers for NVidia or ATI adapters?" 0 0 0 1 $"No" 2 $"install NVidia driver" 3 $"install ATI driver" 2>"$TEMP_FILE"
    if [ $? -eq 0 -a "$(cat "$TEMP_FILE")" != "1" ]; then
        SELECT=$(cat "$TEMP_FILE")
        # Bugfix: capiinit starten
        capiinit start
        # Bugfix: erst auf Internet-Verbindung prfen und dann weitermachen
        while true
        do
            ping -c5 -W1 kanotix.com >/dev/null 2>&1 # kanotix.com anpingen
            if [ $? -ne 0 ]; then
                # Nicht im Internet
                dialog --menu $"Connection to kanotix.com could not be established (ping failed)\n\nWhat do you want to do next?" 0 0 0 1 $"Try again" 2 $"Ignore error and start installation anyway" 3 $"execute commend (e.g. pon <profile>)" 4 $"Abort driver installation" 2>"$TEMP_FILE"
                SELECT_TODO=$(cat "$TEMP_FILE")
                case "$SELECT_TODO" in
                2)
                    break
                    ;;
                3)
                    [ -f "/etc/ppp/peers/provider" ] && PROFIL=provider
                    [ -f "/etc/ppp/peers/dsl-provider" ] && PROFIL=dsl-provider
                    [ -f "/etc/ppp/peers/eagle" ] && PROFIL=eagle
                    [ -f "/etc/ppp/peers/fcdsl" ] && PROFIL=isdn
                    [ -f "/etc/ppp/peers/capi-isdn" ] && PROFIL=capi-isdn
                    [ -f "/etc/ppp/peers/fcdsl" ] && PROFIL=fcdsl
                    dialog --inputbox $"Enter commend to execute" 0 0 "pon $PROFIL" 2>"$TEMP_FILE"
                    eval "$(cat "$TEMP_FILE")"
                    ;;
                4)
                    SELECT=0 # Auswahl des zu installierenden Scripts zurcksetzen und damit hernach keine Treiber installieren
                    break
                    ;;
                esac
            else
                break
            fi
        done

        case "$SELECT" in
        2)
            if [ $(ls /usr/local/bin/install-nvidia-* | wc -l) -gt 1 ]; then
                COMMAND="dialog --default-item \"install-nvidia-debian.sh\" --menu \""$"Please select:""\" 0 0 0"
                for IT_DRIVER in $(ls /usr/local/bin/install-nvidia-*)
                do
                    IT_SHORT="$(basename "$IT_DRIVER")"
                    COMMAND="$COMMAND \"$IT_SHORT\" \"\""
                done
    
                eval "$COMMAND" 2>"$TEMP_FILE"
                SELECT=$(cat "$TEMP_FILE")
                [ "$SELECT" ] && eval "$SELECT"
            else
                /usr/local/bin/install-nvidia-debian.sh
            fi
            ;;
        3)
            /usr/local/bin/install-radeon-debian.sh
            ;;
        esac
    fi

    # Die temporren nderungen im Bootprozess rckgngig machen
    update-rc.d -f kanotix-update-gui remove # Das Script vom Bootprozess austragen
    rm "$TEMP_FILE" # die temporre Datei lschen

    # Danach die grafische Oberflche starten
    init 5
    
    exit 0;
}

function distUpgrade()
{
    chvt 7 || chvt -t 7
    
    echo
    echo $"Kanotix-Update-GUI is now doing the dist-upgrade, please stand by..."
    echo
    
    dialog --yesno $"Kanotix-Update-GUI is now doing the dist-upgrade.\n\nDo you want to proceed?\n(selecting 'No' brings you back to the graphical interface)" 0 0
    
    if [ $? -eq 0 ]; then
        REUNFREEZE=
        [ ! -e "/etc/frozen-rc.d" ] && REUNFREEZE=1 && freeze-rc.d
        # Log-Dateien leeren
        > /tmp/distUpgrade.log
        apt-get dist-upgrade 2>&1 | tee /tmp/distUpgrade.log
        if [ ${PIPESTATUS[0]} -ne 0 ]; then
            echo -e "\a" # Piepen
            echo -e "\n\n"
            echo $"An error occurred during the dist-upgrade. Press <Enter> to display Kanotix-Update-GUI."
            echo $"It will present you with a choice as to how to proceed."
            read # auf Enter warten
            
            TEMP_FILE="/tmp/temp.$RANDOM" # temporre Datei anlegen (fr das Men)
            # Men anzeigen
            while true
            do
                dialog --no-cancel --menu $"An error occurred,\nwhat do you want to do?" 0 0 0 "1" $"Display log" "2" $"Repair by running 'apt-get -f install'" "3" $"Open console (in runlevel 3)" "4" $"Restart dist-upgrade" "5" $"Nothing" 2>"$TEMP_FILE"
                
                SELECT="$(cat "$TEMP_FILE")"
                case "$SELECT" in
                1)
                    [ "$PAGER" ] || PAGER="less"
                    $PAGER "/tmp/distUpgrade.log"
                    ;;
                2)
                    apt-get -f install
                    break
                    ;;
                3)
                    echo
                    echo $"Bash will now be started. You can return to Kanotix-Update-GUI by pressing <Ctrl>+<D> or entering 'exit'."
                    bash
                    ;;
                4)
                    apt-get dist-upgrade
                    break;
                    ;;
                5)
                    dialog --yesno $"Do you really want to leave it at that? Due to the error the dist-upgrade aborted. In virtually all cases this results in packages not being properly installed.\n\nIf you haven't done anything about this (options 2, 3 or 4), this may have serious consequences for further package management." 0 0
                    [ $? -eq 0 ] && break
                    ;;
                esac
            done
            rm "$TEMP_FILE" # die temporre Datei lschen
        fi
        apt-get -f install
        [ $REUNFREEZE ] && unfreeze-rc.d
    
        sleep 1
    fi

    # Die temporren nderungen am Bootprozesses wieder lschen
    update-rc.d -f kanotix-update-gui remove # Das Script vom Bootprozess austragen
    
    # Danach die grafische Oberflche starten
    init 5
    
    exit 0;
}

if [ ! -e /usr/share/kanotix-update-gui/type ]; then
    echo "Error: /usr/share/kanotix-update-gui/type does not exist!" >&2
    update-rc.d -f kanotix-update-gui remove >/dev/null 2>/dev/null
    exit 0
fi

UPDATE_TYPE="$(</usr/share/kanotix-update-gui/type)"

# Nur ausfhren wenn entweder kein Parameter oder der Parameter start bergeben wurde
if [ "$UPDATE_TYPE" = "kernelUpdate" -a "$1" = "start" ]; then
    > /usr/share/kanotix-update-gui/type
    kernelUpdate
elif [ "$UPDATE_TYPE" = "distUpgrade" -a "$1" = "stop" ]; then
    > /usr/share/kanotix-update-gui/type
    distUpgrade
fi

exit 0
