#!/usr/bin/env bash

. /usr/share/dosis2/bin/shell-support-functions

enforce_root

OS="`getos`"
WEBK="/usr/share/dosis2/bin/web-kiosk"
WHICH_KIOSK=""

echo_help() {
echo "set-ubuntu-kiosk [-h] [-f] [-c] [-d]
-h | --help        show help
-f | --firefox     set kiosk to firefox 
-c | --chromium    set kiosk to chromium
-d | --dosis-kiosk set kiosk to dosis-kiosk
"
}

if [ "$1" = "" ]; then
    echo "No kiosk selected"
    echo_help
    exit 1
else
    case $1 in 
        -f | --firefox )        shift
                                WHICH_KIOSK="f"
                                ;;
        -c | --chromium )       shift
                                WHICH_KIOSK="c"
                                ;;
        -d | --dosis-kiosk )    shift
                                WHICH_KIOSK="d"
                                ;;
        -h | --help )           echo_help
                                exit
                                ;;
        * )                     echo_help
                                exit 1
    esac
fi

function edit-web-kiosk {
    if [ -f "${WEBK}" ]; then
        # set to firefox
        if [ "${WHICH_KIOSK}" = "f" ]; then
            if grep -q "/usr/bin/chromium-browser" "${WEBK}"; then
                # chromium to firefox
                sed -i "s/\/usr\/bin\/chromium-browser.*/\/usr\/bin\/firefox -private --kiosk \"\${weburi}\"/" "${WEBK}"
                sed -i "/--disable-session-crashed-bubble.*/d" "${WEBK}"
            elif grep -q "/usr/bin/dosis-kiosk" "${WEBK}"; then
                # dosis-kiosk to firefox
                sed -i "s/\/usr\/bin\/dosis-kiosk.*/\/usr\/bin\/firefox -private --kiosk \"\${weburi}\"/" "${WEBK}"
            else
                abort 1 "Kiosk is already set to firefox"
            fi
        # set to chromium
        elif [ "${WHICH_KIOSK}" = "c" ]; then
            if grep -q "/usr/bin/firefox" "${WEBK}"; then
                # firefox to chromium
                sed -i "s/\/usr\/bin\/firefox.*/\/usr\/bin\/chromium-browser --noerrdialogs --allow-insecure-localhost --incognito --kiosk --ignore-certificate-errors --disable-session-crashed-bubble --disable-infobars \"\${weburi}\"/" "${WEBK}"
            elif grep -q "/usr/bin/dosis-kiosk" "${WEBK}"; then
                # dosis-kiosk to chromium
                sed -i "s/\/usr\/bin\/dosis-kiosk.*/\/usr\/bin\/chromium-browser --noerrdialogs --allow-insecure-localhost --incognito --kiosk --ignore-certificate-errors --disable-session-crashed-bubble --disable-infobars \"\${weburi}\"/" "${WEBK}"
            else
                abort 1 "Kiosk is already set to chromium "
            fi
        elif [ "${WHICH_KIOSK}" = "d" ]; then
            if grep -q "/usr/bin/firefox" "${WEBK}"; then
                # firefox to dosis-kiosk
                sed -i "s/\/usr\/bin\/firefox.*/\/usr\/bin\/dosis-kiosk/" "${WEBK}"
            elif grep -q "/usr/bin/chromium-browser" "${WEBK}"; then
                # chromium to dosis-kiosk
                sed -i "s/\/usr\/bin\/chromium-browser.*/\/usr\/bin\/dosis-kiosk/" "${WEBK}"
                sed -i "/--disable-session-crashed-bubble.*/d" "${WEBK}"
            else
                abort 1 "Kiosk is already set to dosis-kiosk"
            fi
        else
            echo_help
            abort 1 "no kiosk type selected use -h for help"
        fi
    else
        abort 1 "file: web-kiosk is missing"
    fi
}

if [ "${OS}" = "Ubuntu" ]; then
        edit-web-kiosk
else
    abort 1 "Unsupported OS: ${OS}"
fi

echo "Done"