#! /bin/sh
#
# ltsp-client	Script for LTSP client initialization
#
# Author:	Matt Zimmerman <mdz@ubuntu.com>
#

set -e

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="LTSP client setup"
NAME=ltsp-client-setup
SCRIPTNAME=/etc/init.d/$NAME

# Gracefully exit if the package has been removed.
test -x $DAEMON || exit 0

. /lib/lsb/init-functions
. /usr/lib/ltsp/ltsp_functions
. /usr/lib/ltsp/ltsp_config
test -f /etc/default/ltsp-client-setup && . /etc/default/ltsp-client-setup

load_modules() {
    for module in $(env | awk -F= '$1 ~ /^MODULE_/ { print $2 }'); do
        modprobe $module
    done
}

configure_console() {
    if [ -n "$CONSOLE_KEYMAP" ]; then
        loadkeys "$CONSOLE_KEYMAP"
    fi
}

configure_swap() {
    if [ -n "$SWAP_SERVER" -a -n "$NBD_PORT" ]; then
        cat <<EOF > /etc/nbd-client
NBD_DEVICE[0]=/dev/nbd0
NBD_TYPE[0]=s
NBD_HOST[0]=$SWAP_SERVER
NBD_PORT[0]=$NBD_PORT
EOF
    fi
}

configure_resolver() {
    hostname=$(hostname)
    cat <<EOF >> /etc/hosts
127.0.0.1 localhost
127.0.0.2 $hostname
$SERVER server
EOF

    if [ -n "$DNS_SERVER" -a -n "$SEARCH_DOMAIN" ]; then
        cat <<EOF > /etc/resolv.conf
search $SEARCH_DOMAIN
nameserver $DNS_SERVER
EOF
    fi
}

configure_network() {
    cat <<EOF > /etc/network/interfaces
auto lo
iface lo inet loopback
EOF
}

configure_syslog() {
    cat <<EOF > /etc/syslog.conf
*.* @${SYSLOG_HOST-$SERVER}
EOF
}

configure_fstab() {
  echo "/dev/root       /       unionfs defaults        0       0" > /etc/fstab
  echo "tmpfs           /tmp    tmpfs   defaults,nosuid,nodev 0 0" >> /etc/fstab
  mount /tmp
}

preseed() {
    package="$1"
    question="$2"
    value="$3"

    if [ -n "$value" ]; then
        echo -e "set $question $value\nfset $question seen true" | debconf-communicate $package
    fi
}

configure_x() {
    if [ -z "$xserver_package" ]; then
      # set default xserver
      if [ -d /usr/share/doc/xserver-xorg/ ]; then
        xserver_package="xserver-xorg"
      elif [ -d /usr/share/doc/xserver-xfree86/ ]; then
        xserver_package="xserver-xfree86"
      else
        echo "WARNING: no known xserver detected"
        return
      fi
    fi
    # set default configuration file
    case $xserver_package in
      xserver-xfree86) xserver_config="/etc/X11/XF86Config-4" ;;
      xserver-xorg) xserver_config="/etc/X11/xorg.conf" ;;
    esac
    if [ -n "$XF86CONFIG_FILE" ]; then
        # User has specified a custom config
        cp "$XF86CONFIG_FILE" "$xserver_config"
    else
      # Handle overrides of specific parameters
      if [ -n "$XSERVER" ] && [ "$XSERVER" != "auto" ]; then
        preseed $xserver_package $xserver_package/autodetect_video_card "false"
        preseed $xserver_package $xserver_package/config/device/driver "$XSERVER"
      fi
      if [ -n "$X_HORZSYNC" ] && [ -n "$X_VERTREFRESH" ]; then
        preseed $xserver_package $xserver_package/autodetect_monitor "false"
        preseed $xserver_package $xserver_package/config/monitor/selection-method "Advanced"
        preseed $xserver_package $xserver_package/config/monitor/vert-refresh "$X_VERTREFRESH"
        preseed $xserver_package $xserver_package/config/monitor/horiz-sync "$X_HORZSYNC"
      fi
        
      preseed $xserver_package $xserver_package/config/inputdevice/keyboard/layout "$XKBLAYOUT"
      preseed $xserver_package $xserver_package/config/inputdevice/keyboard/model "$XKBMODEL"

      # Autoconfigure
      dpkg-reconfigure -fnoninteractive $xserver_package
    fi
}

bind_mounts () {
  # set defaults
  test -z "$tmpfs_dir" && tmpfs_dir=/var/lib/ltsp-client-setup
  test -z "$rw_dirs" && rw_dirs="/var/cache/man /var/lib/xkb /var/lock /var/run /var/log /var/spool /var/tmp /tmp /var/lib/discover"
  test -z "$copy_dirs" && copy_dirs=""
  test -z "$bindfiles" && bindfiles="/etc/X11/xorg.conf /etc/X11/XF86Config-4"
  mount -t tmpfs -o mode=0755 tmpfs $tmpfs_dir
  # preserve directory structure
  for d in $rw_dirs ; do
    if [ -d "$d" ]; then
      cd $tmpfs_dir
      tar --no-recursion -cpf - $(find $d -type d 2> /dev/null) 2> /dev/null | tar xpf -
      mount --bind $tmpfs_dir/$d $d
    else
      echo "WARNING: $d does not exist"
    fi
  done  
  # copy contents into tmpfs
  for d in $copy_dirs $temp_copy_dirs; do
    if [ -d "$d" ]; then
      cd $tmpfs_dir
      tar -cpf - $d 2> /dev/null | tar xpf -
      mount --bind $tmpfs_dir/$d $d
    else
      echo "WARNING: $d does not exist"
    fi
  done
  # mount one file on top of another
  for f in $bindfiles ; do
    if [ -e "$f" ]; then
      mkdir -p "$(dirname $tmpfs_dir/$f)"
      cp $f $tmpfs_dir/$f
      mount --bind $tmpfs_dir/$f $f
    else
      echo "WARNING: $f does not exist"
    fi
  done
}

bind_unmounts() {
  for dir in $temp_copy_dirs; do
    umount $dir
    rm -rf $tmpfs_dir/${dir#/}
  done
}

case "$1" in
  start)
        log_begin_msg "Setting up LTSP client..."
        if [ "$root_write_method" = "bind_mounts" ]; then
          bind_mounts
        fi
        load_modules || true
        configure_console || true
        configure_swap || true
        configure_network || true
        configure_resolver || true
        configure_syslog || true
        configure_fstab || true
	configure_x || true

	if [ "$root_write_method" = "bind_mounts" ]; then
          bind_unmounts
        fi

        log_end_msg 0
	;;
  stop)
#	echo -n "Stopping $DESC: $NAME"
#	d_stop
#	echo "."
	;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: $NAME"
	d_stop
	sleep 1
	d_start
	echo "."
	;;
  *)
	# echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
