#!/bin/bash

#########Tested on#########
##     Debian 9(i386)    ##
# Hp t5740e               #
##     Debian 9(amd64)   ##
# Dell Optiplex Fx160     #
###########################


echo "Debian 9 Thin client install script."
echo "Full url including http(s)://:"
read url


apt update

# get software
apt install xorg chromium openbox lightdm nedit locales spacefm sudo gmessage unattended-upgrades plymouth plymouth-themes -y

# dir
mkdir -p /home/kiosk/.config/openbox

mkdir -p /home/kiosk/Bureaublad
# create group
groupadd kiosk

# create user if not exists
id -u kiosk &>/dev/null || useradd -m kiosk -g kiosk -s /bin/bash 

# rights
chown -R kiosk:kiosk /home/kiosk

# create config
echo oldurl=${url} > /var/log/browserurl.log

sed -i 's/#PermitRootLogin prohibit-password/PermitRootLogin yes/g' /etc/ssh/sshd_config

wget https://git.ictmaatwerk.com/bprieshof/Linux-scripts/raw/branch/master/tc/config/gui/grub-4x3.png -O /usr/share/desktop-base/softwaves-theme/grub/grub-4x3.png
wget https://git.ictmaatwerk.com/bprieshof/Linux-scripts/raw/branch/master/tc/config/gui/grub-16x9.png -O /usr/share/desktop-base/softwaves-theme/grub/grub-16x9.png
wget https://git.ictmaatwerk.com/bprieshof/Linux-scripts/raw/branch/master/tc/config/gui/joy-ictm.tar.gz -O /tmp/joy-ictm.tar.gz
tar -zxf /tmp/joy-ictm.tar.gz -C /usr/share/plymouth/themes/

sed -i 's|GRUB_TIMEOUT=5|GRUB_TIMEOUT=1|g' /etc/default/grub
sed -i 's|GRUB_CMDLINE_LINUX_DEFAULT="quiet"|GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"|g' /etc/default/grub
update-grub
plymouth-set-default-theme -R joy-ictm

wget https://git.ictmaatwerk.com/bprieshof/Linux-scripts/raw/branch/master/tc/config/20auto-upgrades -O /etc/apt/apt.conf.d/20auto-upgrades
wget https://git.ictmaatwerk.com/bprieshof/Linux-scripts/raw/branch/master/tc/config/50unattended-upgrades -O /etc/apt/apt.conf.d/50unattended-upgrades

if [ -e "/etc/lightdm/lightdm.conf" ]; then
  mv /etc/lightdm/lightdm.conf /etc/lightdm/lightdm.conf.backup
fi
cat > /etc/lightdm/lightdm.conf << EOF
[SeatDefaults]
autologin-user=kiosk
EOF

# create autostart
if [ -e "/home/kiosk/.config/openbox/autostart" ]; then
  mv /home/kiosk/.config/openbox/autostart /home/kiosk/.config/openbox/autostart.backup
fi

cat > /home/kiosk/.config/openbox/autostart << EOF
#!/bin/bash
sleep 1; spacefm --desktop &
  chromium \
    --no-first-run \
    --disable \
    --disable-translate \
    --disable-infobars \
    --disable-suggestions-service \
    --disable-save-password-bubble \
    --disable-session-crashed-bubble \
    --incognito \
	"${url}"
EOF

#creating app files

cat > /home/kiosk/Bureaublad/chromium.desktop << EOF
[Desktop Entry]
Version=1.0
Name=Chromium 
GenericName=Web Browser
GenericName[nl]=Webbrowser
Comment=Access the Internet
Comment[nl]=Verbinding maken met internet
Exec=/usr/bin/chromium --no-first-run --disable --disable-translate --disable-infobars --disable-suggestions-service --disable-save-password-bubble --disable-session-crashed-bubble --incognito ${url}
Terminal=false
X-MultipleArgs=false
Type=Application
Icon=chromium.png
Categories=Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml_xml;application/x-mimearchive;x-scheme-handler/http;x-scheme-handler/https;
StartupWMClass=chromium
StartupNotify=true
EOF

cat > /home/kiosk/Bureaublad/nedit.desktop << EOF
[Desktop Entry]
Version=1.0
Name=NEdit
Name[en]=NEdit 
Name[nl]=NEdit
Exec=nedit-nc %F
Icon=nedit
Terminal=false
Type=Application
MimeType=text/plain;
Categories=Motif;Utility;TextTools;
Keywords=Customizable;Scripts;Powerful;
GenericName=Text Editor
GenericName[en]=Text Editor
GenericName[nl]=Tekstverwerker
EOF

cat > /home/kiosk/Bureaublad/shutdown.desktop << EOF
[Desktop Entry]
Encoding=UTF-8
Version=1.0
Type=Application
Terminal=false
Exec=shutdown-menu
Name=Shutdown
Icon=/usr/share/icons/Adwaita/64x64/actions/system-shutdown-symbolic.symbolic.png
EOF

cp -r /home/kiosk/Bureaublad/ /home/kiosk/Desktop

# rights for apps folder
chown -R kiosk:kiosk /home/kiosk/Bureaublad
chown -R kiosk:kiosk /home/kiosk/Desktop


# setting user power privileges
cat > /etc/sudoers.d/powerctl << EOF
# Cmnd alias specification
Cmnd_Alias      SHUTDOWN = /sbin/shutdown
Cmnd_Alias      REBOOT = /sbin/reboot

# User privilege specification
kiosk ALL=SHUTDOWN
kiosk ALL=NOPASSWD: SHUTDOWN
kiosk ALL=REBOOT
kiosk ALL=NOPASSWD: REBOOT
EOF

cat > /usr/bin/shutdown-menu << EOF
gmessage "Weet u zeker dat u de computer wilt afsluiten?" -center -title "Shutdown" -font "Sans bold 10" -default "Cancel" -buttons "_Annuleren":1,"_Opnieuw opstarten":3,"_Afsluiten":4 >/dev/null

case \$? in
1)
echo "Exit";;
3)
pkill spacefm
sudo shutdown -r now;;
4)
pkill spacefm
sudo shutdown -h now;;
esac
EOF
chmod 775 /usr/bin/shutdown-menu

wget https://git.ictmaatwerk.com/bprieshof/Linux-scripts/raw/branch/master/tc/update-url.sh -O /root/update-url.sh 

echo "Done!"