#!/bin/sh
# drblthincli    This shell script takes care of DRBL thin client
#
# chkconfig: 2345 99 1
# description: drblthincli is a script to let DRBL client run server's gdm/kdm/xdm, i.e. remote display
#
# Author: Steven Shiau <steven _at_ nchc org tw>
# License: GPL

# Source function library.
. /etc/init.d/functions

# The mode assigned by server
[ ! -f "/etc/sysconfig/drblthincli" ] && exit 1
. /etc/sysconfig/drblthincli

# The mode assigned when client boots, if this is assigned, 
# it will overide the mode assigned by server
if grep -i -q "drblthincli=on" /proc/cmdline; then
  DRBL_THIN_CLIENT=on
elif grep -i -q "drblthincli=off" /proc/cmdline; then
  DRBL_THIN_CLIENT=off
fi

# Load config file
[ -f /etc/diskless-image/config ] && . /etc/diskless-image/config

#
start() {
  echo -n $"Starting drblthincli: "
  # try to get the server IP address
  NETDEVICES="$(cat /proc/net/dev | awk -F: '/eth.:|tr.:/{print $1}')"
  for DEVICE in $NETDEVICES; do
    IP_tmp=`LC_ALL=C /sbin/ifconfig | grep -A1 $DEVICE | grep -v $DEVICE | grep "inet addr" | sed 's/^.*inet addr:\([0-9\.]\+\).*$/\1/'`
    if [ -n "$IP_tmp" ]; then
      # Got the IP address, stop to get from other port, so break
      IP=$IP_tmp
      initlog -n drblthincli -s $"Client IP address is $IP (got from [$DEVICE])"
      break
    else
      initlog -n drblthincli -s $"Failed to get IP from [$DEVICE]!"
      initlog -n drblthincli -s $"Try next NIC if available..."
    fi
  done
  
  IP_prefix=`echo $IP |cut -d"." -f1-3`
  if [ -n "`echo "$NFSSERVER_LIST" |grep -o "$IP_prefix"`" ]; then
      for i in $NFSSERVER_LIST; do
  	if [ "`echo $i |cut -d"." -f1-3`" = $IP_prefix ]; then
  	  nfsserver=$i
  	  initlog -n drblthincli -s $"NFS server is $nfsserver"
  	fi
      done 
  else
      initlog -n drblthincli -s $"Can not find the right NFSSERVER, using default one"
      nfsserver=$nfsserver_default
  fi
  
  if [ "$DRBL_THIN_CLIENT" = "on" ]; then
     # if XF86Config is not available, run firstboot to setup it
     if  [ ! -f "/etc/X11/XF86Config-4" -a ! -f "/etc/X11/XF86Config" -a ! -f "/etc/X11/xorg.conf" ]; then
       # For RedHat 8.0/9 Fedora Core 1
       [ -x "/usr/bin/redhat-config-xfree86" ] && /usr/bin/redhat-config-xfree86
       # For Fedora Core 2
       [ -x "/usr/bin/system-config-display" ] && /usr/bin/system-config-display
       # For Mandrake
       if [ -x "/usr/sbin/XFdrake" ]; then
         chvt 7
         TERM=linux XFdrake --noauto < /dev/tty7 > /dev/tty7
       fi
     fi
     # set the init to 3 so that we can run X -query... We do not want the
     # local gdm (/usr/bin/gdmgreeter) to run...
     telinit 3; sleep 2
     # How about in Mandrake ? Will dm run ?
     X -query $nfsserver &>/dev/null &
     RETVAL=$?
  fi
  [ "$RETVAL" = 0 ] && touch /var/lock/subsys/drblthincli
  echo
  return $RETVAL
}

stop() {
  echo -n $"Shutting down drblthincli: "
  killproc X
  RETVAL=$?
  [ "$RETVAL" = 0 ] && rm -f /var/lock/subsys/drblthincli
  echo
  return $RETVAL
}

###
case "$1" in
  "start")
          start
          ;;
   "stop")
          stop
          ;;
   "restart")
          $0 stop
          $0 start
          ;;
   *)
    echo $"Usage: $0 {start|stop|restart}"
          exit 1
esac
