#!/bin/bash
#if [ -z $(grep xhost /home/oem/.config/autostart/oem-setup.desktop) ]; then
#    sudo -u oem xhost +
#fi

export breite=450
reso=$(xrandr | grep -m1 "*+" | awk '{ print $1 }' | cut -c -4)
if [ "$reso" -ge 3840 ]; then
    export GDK_DPI_SCALE=2
    export breite=850
elif [ "$reso" -ge 2048 ]; then
    export GDK_DPI_SCALE=1.5
    export breite=650
fi

LANG1=$(grep LANG /etc/locale.conf | awk -F= '{ print $2 }' | cut -d _ -f 1)
crhd=$(lsblk | grep -m1 crypt_dev | awk -F_ '{print $3}' | awk '{print $1}')

if [ $crhd ]; then
    case $LANG1 in
	de*)
            text1='\n\nFast fertig!\n\nDie Konfiguration und Einrichtung ist fast abgeschlossen\.\n\nIhr Gerät muss nun nur noch einmal heruntergefahren werden\,\ndamit Ihnen alle Funktionen in vollem Umfang zur Verfügung stehen\!\n\nBitte klicken Sie auf OK, geben im folgenden Fenster Ihr Passwort ein\nund schalten das Gerät danach wieder ein\.\n\n\nWir wünschen Ihnen viel Spaß mit Ihrem neuen TUXEDO\.'
            title2="Passwort?"
            text2="\n\nBitte geben Sie Ihr Passwort ein:\n\n"
            title3="Fortschritt"
            text3="Fortschritt ..."
            text4="Falsches Passwort eingegeben!"
            title5="Netzwerk"
            labok="Weiter"
	    labca="Abbrechen"
            text5='\n\nKein Netzwerk verfügbar!\n\nWährend der gesamten Ersteinrichtung ist eine Internetverbindung notwendig!\n\nBitte Verbindung herstellen und "weiter" drücken.'
            textcra="<h4>Bitte verwenden Sie für Ihr neues Verschlüsselungspasswort mindestens einen Großbuchstaben sowie ein Sonderzeichen!</h4>\nSollte das Passwort zu unsicher sein, wird es nicht geändert und es erfolgt keine Rückmeldung.\nEs gilt dann weiterhin das Standardpasswort.\n\nEine Anleitung zur nachträglichen Änderung des Verschlüsselungspassworts finden Sie auf folgender Seite:\n\n<a href='https://www.tuxedocomputers.com/de/Infos/Hilfe-Support/Anleitungen/LUKS-Verschluesselungskennwort-aendern.tuxedo'>tuxe.do/howto-change-crypt</a>"
	    textcr1="Bitte ein neues Passwort für die Verschlüsselung eingeben:"
	    textcr2="Bitte das neue Passwort für die Verschlüsselung wiederholen:"
	    textcr3="Verschlüsselungspasswort setzen"
	    textcr4="Verschlüsselungspasswort wird geändert ..."
	    textcr5="Verschlüsselungspasswort"
	    textcr6="Passwort ist nicht gleich!"
	    textcr7="LUKS Passwort wird geändert"
	    textcr8="Räume System auf"
	    pwerror="Kein Passwort eingegeben. Das Passwort wird nicht geändert!";;
	*)
    	    text1='\n\nAlmost ready!\n\nThe installation and configuration is nearly done\.\n\nYour are only one shutdown away from using your laptop at full power\!\n\nPlease press the OK button, enter your password on the next page\n and power-on your notebook after the following shutdown\.\n\n\nWe whish you a lot of fun with your new TUXEDO\.'
    	    title2="Password?"
    	    text2="\n\nPlease enter your password:\n\n"
    	    title3="Progress"
    	    text3="Progress ..."
    	    text4="Wrong password entered!"
    	    title5="network"
    	    labok="Continue"
    	    labca="Cancel"
    	    text5='\n\nNo network available!\n\nAn internet connection is required throughout the initial setup!\n\nPlease establish a connection and press "continue".'
    	    textcra="<h4>Please use at least one capital letter and one special character for your new crypt password!</h4>\n If your new password is to weak,\nit won't be changed without any feedback. You will be still able to decrypt with the standard password.\n\nInstructions on how to change the encryption password afterwards can be found on the following page:\n\n<a href='https://www.tuxedocomputers.com/en/Infos/Help-Support/Instructions/Change-LUKS-encryption-password.tuxedo'>tuxe.do/howto-change-crypt</a>"
    	    textcr1="Please enter a new password for the encryption:"
	    textcr2="Please repeat the new password for the encryption:"
    	    textcr3="Set encryption password"
    	    textcr4="Encryption password is changed ..."
    	    textcr5="Encryption password"
    	    textcr6="Password is not equal!"
	    textcr7="changing LUKS password"
	    textcr8="clening system"
	    pwerror="No password given. Password will not changed!";;
    esac

#    exec 3> >(zenity --width=$breite  --progress --text "$text3" --title "$title3" --no-cancel --pulsate --auto-close)
#    CRHDD=$(lsblk | grep crypt_dev | awk -F_ '{print $3}' | awk '{print $1}')
#    exec 3>&-

    if ! kdialog --msgbox "$textcra"; then
	exit;
    fi

    ERC=1
    until [ $ERC -ne 1 ]
	do
    if ! CPASSW1=$(kdialog --password "$textcr1" --title "$textcr3"); then
	kdialog --msgbox "$pwerror"
        exit 0;
    fi
    if ! CPASSW2=$(kdialog --password "$textcr2" --title "$textcr3"); then
	kdialog --msgbox "$pwerror"
        exit 0;
    fi
    sleep 1
    if [ "$CPASSW1" == "$CPASSW2" ]; then
	set +H
	ERC=0
	for platte in $(blkid -t TYPE=crypto_LUKS | awk '{print $1}' | awk -F: '{print $1}'); do
	printf '%s\n' "86343" "$CPASSW2" "$CPASSW2" | /sbin/cryptsetup -q luksAddKey --force-password $platte
	sleep 1
	printf '%s\n' "86343" | /sbin/cryptsetup -q luksRemoveKey $platte
	sleep 1
	done | tee >(zenity --width=$breite --title="working ..." --no-cancel --progress --pulsate --auto-close)
	break;
    else
	ERC=1
	kdialog  --error "$textcr6"
    fi
    done
    apt-mark manual cryptsetup cryptsetup-bin lvm2
fi
cleanup() {
    dpkg-reconfigure -f noninteractive keyboard-configuration
    cp /etc/default/grub /tmp/grub.default
    apt-get -y purge $(dpkg -l | awk '/^rc/{print $2}')
    if [ ! -f /etc/default/grub ]; then
	cp /tmp/grub.default /etc/default/grub
    fi
    systemctl unmask packagekit.service
    systemctl enable packagekit.service
}
cleanup | zenity --width=$breite --title="working ..." --no-cancel --progress --pulsate --auto-close
