#!/bin/bash

#/usr/sbin/lessdisks-setup

# this script takes input from user to gather values
# for hostname, initdefault, and other functionality such as X
# server.  these values are then used to modify
# /etc/lessdisks/terminals.config, and possibly
# /etc/lessdisks/terminals/$hostname and other
# terminal-specific configuration files (such as XF86Config)

# copyright 2004 vagrant@freegeek.org, distributed under the terms of the
# GNU General Public License version 2 or any later version.

case $1 in
        help|-h|--help|-help)
                echo "lessdisks-setup [options] [ipaddress]"
                echo "the only options are --help, -h, -help and help"
                echo "you can also specify an ip"
                exit 0
                ;;
        *) ipaddress="$1"
                shift
                ;;
esac

serverconfig="/etc/lessdisks/server.config"

# source config file...
if [ -e $serverconfig ]
then
  . $serverconfig
else
  echo "$serverconfig not found, exiting..."
  exit 1
fi

groupcheck=$(groups | egrep "$lessdisks_group|root")
if [ -z "$groupcheck" ]
then
  echo "user \"$USER\" not present in the group \"$lessdisks_group\""
  echo "exiting..."
  exit 1
fi

terminalconfig="/etc/lessdisks/terminals.config"
if [ ! -e $terminalconfig ]
then
  echo "$terminalconfig not found, exiting..."
  exit 1
fi

# source easydialog
PATH="/usr/local/lib/easydialog:/usr/share/easydialog:/usr/lib/easydialog:$PATH" . easydialog.sh

# determine ip address if not specified from "$1"
if [ -z "$ipaddress" ]
then
  if [ -z "$SSH_CLIENT" ]
  then
    # this assumes we're logged into X via XDMCP
    ipaddress=`echo $DISPLAY | cut -d ":" -f 1`
  else
    # this assumes we're logged in via SDM
    ipaddress=$(echo $SSH_CLIENT | awk '{print $1}' | sed 's/::ffff://')
  fi
fi

if [ "" = "$ipaddress" ]; then
  echo "no ip address configured."
  echo "exiting..."
  exit 1
fi

# TODO make other tempfile options such as mktemp..

tempdir=$(mktemp -d) || exit 1
cd $tempdir

# set umask to be group-writeable too
umask 002

wget http://$ipaddress:8080/config
if [ "0" != "$?" ]; then
  echo "couldn't get configuration from terminal: $ipaddress"
  echo "did lessdisks-export start the appropriate web server?"
  echo "exiting..."
  exit 1
fi

# source configuration values...
. config

# TODO set to reconfigure if hostname=default

if [ -z "$macaddress" ]; then
  echo "macaddress not found..."
  exit 1
fi

cd $lessdisks_path

if [ "" = "$1" ]; then
  script="new"
fi

while [ "default" = "$hostname" ] || [ -z "$hostname" ]
do
  inputBox "name machine" "What shall this lil machine be named?"  
  if [ "0" != "$?" ]
  then
    echo "exiting because cancel button was pressed..."
    exit 1
  fi
  hostname="$REPLY"

  hostnamecheck=`grep "^$hostname " $terminalconfig`
  if [ "" != "$hostnamecheck" ]; then
    booleanBox "reconfiguring $hostname?" "are you attempting to reconfigure $hostnamecheck?"

    if [ "0" = "$?" ]
    then
      script="reconfigure"
    else
      msgBox "$hostname already exists" \
        "well, you're either wrong or you typed the wrong thing, cuz a machine named $hostname already exists!"
      hostname=""
    fi
  fi
done

#TODO maybe implement clone process?

message="available runlevels are:"
if [ -r $lessdisks_path/etc/inittab ]; then
  message=`echo "$message" ; egrep "^# runlevel" /$lessdisks_path/etc/inittab`
else
  echo "could not find lessdisks inittab... exiting..."
  exit 1
fi

menuBox "choose runlevel" "$message" \
  2 "default, console-only" \
  3 "console-only" \
  4 "X, alternate mode" \
  5 "X, default mode"
  
runlevel="$REPLY"

#TODO need way to figure out weather terminal already exists...
# possibly get the mac address through reverse arp(rarp) with the ip address?

#TODO prompt for ok?

if [ -z "$runlevel" ]
then
  runlevel="2"
fi

mkdir -p /etc/lessdisks/terminals
touch /etc/lessdisks/terminals/$hostname
mv -vf /etc/lessdisks/terminals/$hostname /etc/lessdisks/terminals/$hostname.old
echo "runlevel=$runlevel" >> /etc/lessdisks/terminals/$hostname

macaddresstest=`egrep $macaddress $terminalconfig`
if [ "" != "$macaddresstest" ] && [ "reconfigure" != "$script" ]; then
  echo "mac address already configured as"
  echo "$macaddresstest"
  echo "exiting..."
  exit
fi

# configure terminal-types such as xterminal, hardware-tester, etc..
if [ -d $lessdisks_path/usr/share/lessdisks/terminal-types ]
then
  for configuretype in $lessdisks_path/usr/share/lessdisks/terminal-types/setup.*
  do
    . $configuretype
  done
fi

rm -rf $tempdir

if [ ! -d /etc/lessdisks/oldconfigs ]; then
  mkdir /etc/lessdisks/oldconfigs
fi

cd /etc/lessdisks
cp terminals.config oldconfigs/terminals.config.`date +%b_%d_%H_%M`

newconfig=`egrep -v "^$hostname " terminals.config`
echo -e "$newconfig" > terminals.config
echo -e "$hostname \t$macaddress" >> terminals.config
