#!/bin/sh

mount $1 /target

chooseinterface() {
  export INTERFACE=$(zenity --title "Edubuntu LTSP Installation" --entry --text="This script will set up a Terminal Server on the installed Edubuntu system.\nPlease select an interface it should run on, for example 'eth0', 'eth1', etc.\nEnter 'none' if you wish to configure networking manually." --entry-text="eth1")
  if [ "$?" = "1" ]; then
    exit
  fi
}

confirmation() {
  LIVESTATUS="`LANG=C ifconfig $INTERFACE | grep '192.168.0.254' | wc -l`"
  if [ "$LIVESTATUS" = "1" ]; then
    LIVECONFIRMATION=`zenity --question --title "LTSP Live" --text="Please note: Installation of LTSP will end your LTSP Live session." --ok-label="Ok" --cancel-label="Cancel"`
    if [ "$?" = "1" ]; then
      exit
    else
      echo " * Stopping LTSP..."
      stop ssh
      ifconfig $INTERFACE 0.0.0.0
    fi
  fi
  STATUS="`LANG=C ifconfig $INTERFACE | grep 'inet addr' | wc -l`"
  if [ "$STATUS" = "1" ]; then
    CONFIRMATION=`zenity --question --title "Please confirm" --text="The interface you have selected is already configured for another network. Are you sure you want to run LTSP on this interface?" --ok-label="Yes" --cancel-label="No"`
    if [ "$?" != "0" ]; then
      chooseinterface
    fi
  fi
}

configure() {
  (echo 1 && sleep 99999) | zenity --auto-kill --auto-close --title "Configuring LTSP" --text \
    "Installing and configuring LTSP, this usually takes about 10 minutes." --progress --pulsate&
  ZEN_PID=$!

  # Actually install LTSP
  ltsp-install

  kill $!

  umount /target
  zenity --info --title "Finished" --text \
    "Installation complete."
  rm /home/ubuntu/Desktop/ltsp.desktop
}

chooseinterface
confirmation
configure
