#!/bin/sh
# Script that will dispatch requests of getltscf
#
#
# In Mille-Xterm, we get the lts.conf in two steps:
# 1- The file /etc/config-server is read and inform for the next step
# 2- Do a wget with the adresse in step one to get our lts.conf
# A query could be like this:
# mille-xterm/?nodeIP=192.168.2.168/nodeMac=tata/nfsIP=neo/code=1
# http://newton/mille-xterm/?mac=00:02:3F:BB:69:33/ip=10.0.1.2/
# 


# See if config server is overriden in /proc/cmdline
#if [ "${configserver}" != "" ]; then
#  echo "${configserver}" > /tmp/config-server
#fi
	

# See if running on a live CD
#if [ "${media}" = "livecd" ]; then
#  touch /tmp/xdm.conf
#fi

setEnv(){
    if [ -r /etc/config-server ] ; then
        if [ ! -r /tmp/getltscfg.info ] ; then
		echo "Using configurator: `cat /etc/config-server`" 1>&2
		# Find the config server
		MY_CONFIG_SERVER=`cat /etc/config-server`
		# Get the MAC address, used to fetch the right config file
		MY_NODE_MAC=`/sbin/ifconfig | grep eth0 | cut -f11 -d" "`
		# Get the IP address of the terminal
		MY_NODE_IP=`ifconfig eth0 | grep inet | cut -f2 -d":" | cut -f1 -d" "`
		# Get the IP address of the NFS root server
		MY_NFSROOT_IP=`busybox mount | grep -v unionfs | grep nfs | cut -f1 -d":"`
		echo "MY_CONFIG_SERVER="$MY_CONFIG_SERVER >  /tmp/getltscfg.info
		echo "MY_NODE_MAC="$MY_NODE_MAC >> /tmp/getltscfg.info
		echo "MY_NODE_IP="$MY_NODE_IP >> /tmp/getltscfg.info
		echo "MY_NFSROOT_IP="$MY_NFSROOT_IP >> /tmp/getltscfg.info
	else
		. /tmp/getltscfg.info	
	fi
    else
#        echo "*********************************************************************" 1>&2
#        echo 1>&2
#        echo "No configurator set" 1>&2
#	echo "If you want to use the configurator, fill the file /etc/config-server" 1>&2
	# Get the IP address of the NFS root server
	MY_NFSROOT_IP=`busybox mount | grep -v unionfs | grep nfs | cut -f1 -d":"`
    fi
}


fetchLtsconf(){
        # Fetch the config file
        if [ ! -f /tmp/lts.conf ] ; then
                if [ -n $MY_CONFIG_SERVER ] ; then
                        echo "Getting inventory..." 1>&2
                        dmidecode | inventory.sh -d /tmp/inventory.txt
                        echo "hwlist=" > /tmp/post_inventory.txt
                        cat /tmp/inventory.txt | sed -e "s/%/%25/g" | sed -e "s/\//%2F/g" | sed -e "s/&/%26/g" | sed -e "s/=/%3D/g" >> /tmp/post_inventory.txt
                        echo "wget --no-check-certificate -O /tmp/lts.conf $MY_CONFIG_SERVER\?mac=$MY_NODE_MAC/ip=$MY_NODE_IP/bootservip=$MY_NFSROOT_IP/code=1" 1>&2
                        wget --no-check-certificate -O /tmp/lts.conf $MY_CONFIG_SERVER\?mac=$MY_NODE_MAC/ip=$MY_NODE_IP/bootservip=$MY_NFSROOT_IP/code=1 --post-file=/tmp/post_inventory.txt 1>&2
                fi
        fi
}

fetchXdmServer(){
    # Fetch the xdm server and
    if [ ! -f /tmp/xdm.conf ] ; then
        LOADBALANCER_URL="http://$MY_NFSROOT_IP:8008"
        if [ -e /etc/loadbalancer-url ]; then
          LOADBALANCER_URL=`cat /etc/loadbalancer-url`
        fi
        # Use LBGROUP attribute if exists
        eval `grep LBGROUP /tmp/lts.conf`
        if [ -n $LBGROUP ]; then
            LOADBALANCER_URL="$LOADBALANCER_URL?group=$LBGROUP"
        fi
        echo "wget --no-check-certificate -O /tmp/xdm.conf $LOADBALANCER_URL" 1>&2
        wget --no-check-certificate -O /tmp/xdm.conf $LOADBALANCER_URL 1>&2
    fi
    
}

verifylts(){
    if [ ! -s /tmp/lts.conf ];then
	    cp -f /etc/lts.conf /tmp 2>/dev/null
    fi
    if [ ! -s /tmp/lts.conf ];then
	    echo "[Default]" > /tmp/lts.conf
	    echo "        SCREEN_01 = startx" >> /tmp/lts.conf
	    echo "        SCREEN_02 = shell" >> /tmp/lts.conf
    fi
}

setEnv

PARENT=`cat /proc/$PPID/cmdline`
if [ "${PARENT}" = "/bin/sh/etc/screen.d/startx" ]; then
  if [ -r /etc/config-server ] ; then
      fetchLtsconf
  fi

  unset XDM_SERVER
  eval `grep XDM_SERVER /tmp/lts.conf`
  #Only use loadbalancer if the configurator doesn't provide
  #an xdm server
  if [ "x$XDM_SERVER" = "x" ]; then
    fetchXdmServer $@
  fi

  # Inform the configurator if it exists
  if [ -f /etc/config-server ] ; then
    XDM_SERVER=`cat /tmp/xdm.conf`
    #echo "wget --no-check-certificate -O /dev/null $MY_CONFIG_SERVER?ip=$MY_NODE_IP/appservip=$XDM_SERVER/display=0/code=2"
    wget --no-check-certificate -O /tmp/code2 $MY_CONFIG_SERVER\?ip=$MY_NODE_IP/appservip=$XDM_SERVER/code=2 1>&2
  fi
  cat /tmp/xdm.conf >> /tmp/xdm.history
  cp -f /tmp/xdm.conf /tmp/xdm.conf.current
  # rm this file in order to force the query to the X loadbalancer next time
  rm -f /tmp/xdm.conf
else
  if [ -r /etc/config-server ] ; then
      fetchLtsconf
  fi
fi


verifylts
/bin/getltscfg.real -c /tmp/lts.conf $@

    if [ -s /tmp/xdm.conf.current ]; then
	# If the caller want only one variable, we don't have to give the XDM_SERVER value
	if [ `echo "$@" | grep -- "-a" |wc -l` = 1 ] ; then
	    echo "#getltscfg: fetchXdmServer: Option -a received" 1>&2
	    XDM_SERVER=`cat /tmp/xdm.conf.current`
	    echo "XDM_SERVER=$XDM_SERVER"
	    echo "export XDM_SERVER"
	fi
	if [ `echo "$@" | grep -- "XDM_SERVER" |wc -l` = 1 ] ; then
	    echo "#getltscfg: fetchXdmServer: Option XDM_SERVER recived" 1>&2
	    XDM_SERVER=`cat /tmp/xdm.conf.current`
	    echo "XDM_SERVER=$XDM_SERVER"
	    echo "export XDM_SERVER"
	fi
    fi
