#!/bin/bash

#    kiosk-setup.sh
#    OpenKiosk Browser Terminal Setup
#    Copyright (C) 2024  Rod Wright

#    SPDX-License-Identifier: GPL-2.0

OK_VERSION="1.0.0"

# Test for superuser
current_user=`whoami`
current_login=`who am i | awk '{print $1}'`
if [ "$current_user" != "root" ]
then
	echo "You must have root privileges to run this setup."
	echo "Try 'sudo ./kiosk-setup.sh'"
	exit 1
fi

echo ""
echo "                  Welcome to OpenKiosk $OK_VERSION"
echo ""
echo ""
echo "This script will configure a Debian based system to be a Kiosk Browser terminal."
echo ""
echo ""
echo "To continue with the setup of OpenKiosk, press enter. Otherwise,"
echo -n "press ctrl-c to quit."
read continueok
echo ""
echo ""

# Determine OS Release version
echo -n "OS information: "
echo ""
cat /etc/os-release
echo ""
sleep 5

# Copy in the needed files 
echo "Copying files..."
chmod +x usr/local/bin/*
cp -Rv etc/* /etc/
cp -v usr/local/bin/* /usr/local/bin
cp -v lib/systemd/system/getty@tty1.service /lib/systemd/system
rm /lib/systemd/system/ctrl-alt-del.target
ln -s /dev/null /lib/systemd/system/ctrl-alt-del.target
echo "...done."

# Make sure OS is up to date
echo "Updating Operating System..."
sleep 5
apt update && apt dist-upgrade -y
echo "...done."
echo ""

# Remove snap if installed
if snap list >/dev/null 2>&1; then
    echo "Removing snap..."
    snap remove lxd
    snap remove core20
    snap remove snapd
    systemctl stop snapd
    systemctl disable snapd
    systemctl mask snapd
    apt purge snapd -y
    apt-mark hold snapd
    rm -rf /home/*/snap/
    rm -rf /root/snap/
    rm -rf /snap
    rm -rf /var/snap
    rm -rf /var/lib/snapd
    echo "...done."
    # Add the firefox deb repository since we won't be getting snap
    add-apt-repository -y ppa:mozillateam/ppa
fi

# Install packages
echo ""
echo ""
echo "Installing required kiosk packages..."
sleep 5
echo ""

# If we are on a Raspberry Pi 5, we'll need to install this for the
# xorg server to start
if cat /proc/cpuinfo|grep -q "Raspberry Pi 5" >/dev/null 2>&1
then
    apt install -y gldriver-test
fi

# If we are on a Raspberry Pi running Raspbian we use chromium-browser.
# If we are on anything else, we use firefox.
if cat /etc/os-release|grep -q "Raspbian" >/dev/null 2>&1
then
    browser_pkg="chromium-browser"
    ln -sf /etc/openkiosk.conf-chromium /etc/openkiosk.conf
else
    browser_pkg="firefox"
    ln -sf /etc/openkiosk.conf-firefox /etc/openkiosk.conf
fi

# Install required packages
apt update
apt install -y --no-install-recommends xorg openbox $browser_pkg
apt install -y cups libcups2-dev cmake
systemctl enable getty@tty1.service
cd brlaser-master && cmake . && make && make install && cd ..
myhn=`hostname`
infotimer=10
echo ""
echo ""
echo ""
echo "The cups print server has been installed."
echo "After the kiosk is set up and working, you can use another network"
echo "computer to point a web browser to $myhn:631 to add a printer"
echo "of your choosing."
while [ $infotimer -gt 0 ]
do
    sleep 1
    ((infotimer-=1))
done
echo ""

# Download and install webmin
echo "Installing webmin..."
skipwebmin="false"
curl -o setup-repos.sh https://raw.githubusercontent.com/webmin/webmin/master/setup-repos.sh
if [ $? -ne 0 ]
then
	echo "Warning: failed to download Webmin repo setup script."
	echo -n "Ignore and [c]ontinue or [A]bort? [c/A]: "
	read wmfailchoice
	if [ "$wmfailchoice" = "C" -o "$wmfailchoice" = "c" ]
	then
		skipwebmin="true"
		echo "Continuing without installing Webmin. You should investigate the failure"
		echo "and install Webmin manually after the kiosk is set up if you would like"
        echo "to be able to manage the kiosk from another computer with a GUI instead"
        echo "of through ssh only."
		echo -n "Press enter to continue."
		read continueok
		echo ""
	else
		echo "Aborting kiosk setup."
		exit 1
	fi
fi
if [ "$skipwebmin" = "false" ]
then
	sh setup-repos.sh -f
	apt install -y --install-recommends webmin
fi
echo "...done."

# Install user config
echo ""
echo "Creating default user..."
cloneuser `who am i | awk '{print $1}'` customer
mkdir -p /home/customer/.config/openbox
cp home/customer/.profile /home/customer
cp home/customer/.config/openbox/rc.xml /home/customer/.config/openbox
cp home/customer/openkiosk_mozilla_profile.tar.gz /home/customer
chown customer:customer /home/customer/openkiosk_mozilla_profile.tar.gz
chown -R customer:customer /home/customer/.config /home/customer/.profile
echo "...done."
echo ""

# Prompt for URL and kiosk type
echo ""
echo "This web browser based kiosk will need to know the site you would"
echo "like to load. This can be a normal website or a file to be displayed"
echo "in the browser, but must be available as a web URL."
echo ""
echo "Note that in kiosk mode, your normal navigation buttons and mouse"
echo "right-click menus are not available, so you will have no way to go"
echo "back or forward in a page unless the page itself provides that"
echo "capability."
url_ok=false
server_url="https://localhost:10000"
while ! $url_ok
do
    echo ""
    echo "Please enter complete URL."
    echo -n "[https://localhost:10000] :"
    read server_url
    if [[ $server_url == "" ]]
    then
        server_url="https://localhost:10000"
    fi
    if ! `wget --no-check-certificate -q -o /dev/null -O /dev/null $server_url`
    then
        echo ""
        echo "There was an error retrieving the URL you entered. The URL must"
        echo "be a complete URL of the format \"http://somesite.com/somepage\"."
        echo "If this URL is on the local network, try using the IP address or"
        echo "the local domain. For example, instead of \"http://myserver/mypage\","
        echo "try \"http://192.168.1.1/mypage\" or \"http://myserver.local/mypage\"."
    else
        url_ok=true
    fi
done
sed -i 's,'TGT_URL=.*','TGT_URL="\"$server_url\""',' /etc/openkiosk.conf-firefox
sed -i 's,'TGT_URL=.*','TGT_URL="\"$server_url\""',' /etc/openkiosk.conf-chromium
echo ""
type_choice="none"
while [ "$type_choice" = "none" ]
do    
    echo "Will this kiosk be an interactive kiosk or a display-only kiosk?"
    echo -n "[I]nteractive or [D]isplay-only? [I] :"
    read kiosk_type
    echo ""
    if [ "$kiosk_type" = "D" -o "$kiosk_type" = "d" ]
    then
        echo "Configuring for display-only kiosk."
        echo "How often would you like to check for changes to the page being"
        echo "displayed? Specify the number of seconds. Enter 0 to disable"
        echo "periodic checking."
        echo -n "Seconds between checks? [60] :"
        read poll_interval
        echo ""
        if [ "$poll_interval" = "" ]; then poll_interval=60; fi
        sed -i 's,'POLL_INTERVAL=.*','POLL_INTERVAL="\"$poll_interval\""',' /etc/openkiosk.conf-firefox
        sed -i 's,'POLL_INTERVAL=.*','POLL_INTERVAL="\"$poll_interval\""',' /etc/openkiosk.conf-chromium
        echo "Disabling screen blanking."
        sed -i 's,'INHIBIT_BLANK=.*','INHIBIT_BLANK="\"1\""',' /etc/openkiosk.conf-firefox
        sed -i 's,'INHIBIT_BLANK=.*','INHIBIT_BLANK="\"1\""',' /etc/openkiosk.conf-chromium
        type_choice="D"
    elif [ "$kiosk_type" = "I" -o "$kiosk_type" = "i" ]
    then
        echo "Configuring for interactive kiosk."
        type_choice="I"
    else
        echo "Please enter I for interactive or D for display-only."
    fi
done
echo ""
echo "Kiosk configuration is now complete. "
echo ""
echo "The system must be rebooted for the configuration to take effect."
echo -n "Enter R to reboot now, or hit enter to return to shell.  "
read rebootok
if [ "$rebootok" = "R" -o "$rebootok" = "r" ]
then
    reboot
else
    exit 0
fi
