#!/bin/sh -e

# Ensures the script is run with sudo
if [ $(/usr/bin/id -u) -ne 0 ]; then
    echo "Please run this script with sudo:"
    echo "sudo $0 $*"
    exit 1
fi

# Including helpers
. /home/lacaisse/kioskserver/helpers.sh

# Checks the installation stage and run the required script
if [ -f /home/lacaisse/kioskserver/kiosk-install-done ]; then
  exit 0  
fi

if [ ! -f /home/lacaisse/kioskserver/kiosk-install-libs ]; then
    log "Installing libs ..."

    /home/lacaisse/kioskserver/install-libs.sh
    touch /home/lacaisse/kioskserver/kiosk-install-libs
    cp /home/lacaisse/kioskserver/install-on-boot.sh /etc/init.d
    update-rc.d install-on-boot.sh defaults
    reboot

    log "Installing libs ... Done"
else
    log "Configuring ..."

    /home/lacaisse/kioskserver/install-conf.sh
    rm /home/lacaisse/kioskserver/kiosk-install-libs
    update-rc.d install-on-boot.sh remove
    rm -f /etc/init.d/install-on-boot.sh
    touch /home/lacaisse/kioskserver/kiosk-install-done

    log "Configuring ... Done"
    exit 0
fi
