#!/bin/sh
# Author: Blake, Kuo-Lien Huang
# License: GPL
# Description:
#   1. use fvwm95 as the window manager and auto login enable
#   2. as a WWW appliance, use wm2 and kiosk.html

imagehost="/var/lib/diskless/default"

# must be root
ID=`id -u`
if [ "$ID" != "0" ]; then
  echo "You must be root to use this script."
  echo "Use sudo to enable it for users."
  exit 1
fi

start_fvwm95() {

  if [ "$(chroot $imagehost/root dpkg -l fvwm95 | grep "fvwm95" | cut -d' ' -f1)" != "ii" ]; then
    echo "fvwm95 is not installed..Abort!!"
    exit 0
  fi

  if [ "$FORCE_YES" = "" ]; then
    echo "This script will replace KDE with fvwm95 and enbale 'autologin' feature."
    echo "fvwm95 use lower computing resoureces than KDE, but the language support,"
    echo "such as chinese (zh_TW.Big5), is not as good as KDE."
    echo -n "Are your sure to continue [Y|n]? "
    read ANS
  else
    ANS="Y"
  fi

  if [ "$ANS" = "N" -o "$ANS" = "n" ]; then exit; fi

  echo "Enable autologin feature..."
  if [ -f $imagehost/root/diskless-image/autologin ]; then
    echo "autologin is enabled before you execute the script" > $imagehost/root/diskless-image/thinclient
  elif [ -f /opt/drbl/sbin/drbl-autologin ]; then
    /opt/drbl/sbin/drbl-autologin start
  else
    echo "Cannot find the script 'drbl-autologin' in /opt/drbl/sbin."
    echo "Please use 'drbl-get install drbl' to get the latest one"
    exit
  fi

  echo "Replace KDE with fvwm95.."
  for host in `ls $imagehost`
  do
    ## skip these $host
    if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
    skip=0
    for netdev in `cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}'`
    do
      ip=`/sbin/ifconfig $netdev | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
      if [ "$ip" != "" -a "$host" = "$ip" ]; then skip=1; fi
    done
    if [ $skip -eq 1 ]; then continue; fi

    username=`grep -e "AutoLoginUser=.*" $imagehost/$host/etc/kde3/kdm/kdmrc | cut -d= -f2`
    if [ -d /home/$username ]; then
      echo "fvwm95" > /home/$username/.wmrc
    fi
  done

  touch $imagehost/root/etc/diskless-image/thinclient
  echo "done."
}

## sapwn mozilla in kiosk mode
start_kiosk() {
  if [ "$FORCE_YES" = "" ]; then
    echo "This script will setup your DRBL clients as WWW appliances."
    echo "No more X applications except Mozilla will be running in your DRBL clients."
    echo -n "Are your sure to continue [Y|n]? "
    read ANS
  else
    ANS="Y"
  fi

  if [ "$ANS" = "N" -o "$ANS" = "n" ]; then exit; fi

  echo "Enable autologin feature..."
  if [ -f $imagehost/root/diskless-image/autologin ]; then
    echo "autologin is enabled before you execute the script" > $imagehost/root/diskless-image/thinclient
  elif [ -f /opt/drbl/sbin/drbl-autologin ]; then
    /opt/drbl/sbin/drbl-autologin start
  else
    echo "Cannot find the script 'drbl-autologin' in /opt/drbl/sbin."
    echo "Please use 'drbl-get install drbl' to get the latest one"
    exit
  fi

# 2004/03/18, a better way to do kiosk
#  echo "Only Mozilla..."
#  mkdir --mode=755 /home/kiosk/
#  kiosk_page=$1
#  if [ "$kiosk_page" = "" ]; then kiosk_page="http://drbl.nchc.org.tw/"; fi
#  cat <<-EOF > /home/kiosk/kiosk.html
#<html>
#<head>
#<script language="javascript">
#<!--
#function OpenWin(){
#  window.open("$kiosk_page","",
#              "toolbar=yes,scrollbars=yes,location=yes"+
#              ",width="+screen.width+
#              ",height="+screen.height);
#}
#//-->
#</script>
#</head>
#<body onLoad="OpenWin()">
#</body>
#</html>
#EOF
  trap 'rm -f thinclient.zip; rm -rf thinclient; exit 1' HUP INT QUIT TERM
  tail +${LINENO} $0 > thinclient.zip
  unzip thinclient.zip > /dev/null
  # Blake, 2004/03/18, a better way to do kiosk
  #dpkg --root $imagehost/root --install thinclient/wm2_4-7_i386.deb
  #deb=`ls thinclient/wm2*.deb`
  #dpkg --root $imagehost/root --install $deb
  if [ -d $imagehost/root/usr/lib/mozilla/chrome/kiosk ]; then
    rm -rf $imagehost/root/usr/lib/mozilla/chrome/kiosk
  fi
  mkdir -p $imagehost/root/usr/lib/mozilla/chrome/kiosk
  cp -r thinclient/kiosk/content $imagehost/root/usr/lib/mozilla/chrome/kiosk
  kiosk_page=$1
  if [ "$kiosk_page" = "" ]; then kiosk_page="http://drbl.nchc.org.tw/"; fi
  cat <<-EOF >> $imagehost/root/usr/lib/mozilla/chrome/kiosk/content/kiosk.dtd
<!ENTITY base.uri           "$kiosk_page">
EOF
  # update-mozilla-chrome
  cp thinclient/kiosk/60kiosk $imagehost/root/var/lib/mozilla/chrome.d/
  chroot $imagehost/root /usr/sbin/update-mozilla-chrome
  # for .wmrc
  cat <<-EOF > $imagehost/root/usr/bin/mozilla-kiosk
#!/bin/sh
mozilla -chrome chrome://kiosk/content
EOF
  chmod 755 $imagehost/root/usr/bin/mozilla-kiosk

  rm -rf thinclient
  rm -f thinclient.zip 
  for host in `ls $imagehost`
  do
    ## skip these $host
    if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
    skip=0
    for netdev in `cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}'`
    do
      ip=`/sbin/ifconfig $netdev | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
      if [ "$ip" != "" -a "$host" = "$ip" ]; then skip=1; fi
    done
    if [ $skip -eq 1 ]; then continue; fi

    username=`grep -e "AutoLoginUser=.*" $imagehost/$host/etc/kde3/kdm/kdmrc | cut -d= -f2`
    if [ -d /home/$username ]; then
      #echo "wm2" > /home/$username/.wmrc
      echo "mozilla-kiosk" > /home/$username/.wmrc
    fi

    rm -rf $imagehost/$host/var/lib/mozilla/*
    cp -r $imagehost/root/var/lib/mozilla/* $imagehost/$host/var/lib/mozilla
  done

  touch $imagehost/root/etc/diskless-image/thinclient
  echo "done."
}

stop() {

#  if [ "$FORCE_YES" = "" ]; then
#    echo -n "Do you want to disable 'autologin' feature [Y|n]? "
#    read ANS
#  else
#    ANS="Y"
#  fi
#
#  if [ "$ANS" = "" -o "$ANS" = "Y" -o "$ANS" = "y" ]; then
#    /opt/drbl/sbin/drbl-autologin stop
#  fi
  if [ ! -f $imagehost/etc/diskless-image/thinclient ]; then
    echo "thinclient mode is not enabled! abort!!"
    exit
  fi

  if [ "$(cat $imagehost/etc/diskless-image/thinclient | grep "autologin is enabled before you execute the script")" != "" ]; then
    /opt/drbl/sbin/drbl-autologin stop
  fi

  if [ -f $imagehost/root/var/lib/mozilla/chrome.d/60kiosk ]; then
    rm $imagehost/root/usr/bin/mozilla-kiosk
    rm $imagehost/root/var/lib/mozilla/chrome.d/60kiosk
    chroot $imagehost/root /usr/sbin/update-mozilla-chrome

    for host in `ls $imagehost`
    do
      ## skip these $host
      if [ "$host" = "." -o "$host" = ".." -o "$host" = "root" ]; then continue; fi
      skip=0
      for netdev in `cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}'`
      do
        ip=`/sbin/ifconfig $netdev | grep "inet addr" | cut -d: -f2 | awk '{ print $1; }'`
        if [ "$ip" != "" -a "$host" = "$ip" ]; then skip=1; fi
      done
      if [ $skip -eq 1 ]; then continue; fi

      rm -rf $imagehost/$host/var/lib/mozilla/*
      cp -r $imagehost/root/var/lib/mozilla/* $imagehost/$host/var/lib/mozilla
    done
  fi
  #has_wm2=$(chroot $imagehost/root dpkg -l | grep "wm2")
  #if [ "$has_wm2" != "" ]; then 
  #  chroot $imagehost/root dpkg -r wm2
  #  chroot $imagehost/root dpkg --purge wm2
  #fi
  #if [ -d /home/kiosk ]; then rm -rf /home/kiosk; fi

  rm -f $imagehost/root/etc/diskless-image/thinclient
  echo "done."
}

case "$1" in
  'start')
    ##
    case "$2" in
      'www')
        start_kiosk $3
        ;;
      *)
        start_fvwm95
	;;
    esac
    ##
    ;;
  'stop')
    stop
    ;;
  *)
    echo "Usage: $0 start [www [URL]]"
    echo "       $0 stop"
    ;;
esac
exit 0
