#! /bin/sh
. $TS_GLOBAL

if [ -z "$SETNETWORKUP" ] ; then
	NETWORKUP=FALSE
fi

if [ -n "$BASENAME" ] ; then
	CONFNET="$BASENAME.conf."
	CONFFILE="$BASENAME.conf-"
	CONFGROUP="$BASENAME.conf.group-"
	CONFHOSTS="$BASENAME."
else
	CONFNET=""
	CONFFILE=""
	CONFGROUP=""
	CONFHOSTS=""
fi

if [ -n "$DEBUG_NETWORK" ] ; then
	debug="-d"
fi

# Determine what transport method to download config files
transport()
{
	local spath=$1 cpath=$2 server=$3

	# Set default if variable not set
	if [ -z "$NET_FILE_METHOD" ] ; then
		NET_FILE_METHOD=tftp
	fi

	case `make_caps $NET_FILE_METHOD` in
	TFTP)
		echo "tftp -g -r $spath -l $cpath $server > /dev/null 2>&1"
	;;
	SCP)
		echo "scp -BC $NET_FILE_USER@$server:$spath $cpath"
	;;
	WGET)
		echo "wget -P /tmp $BASEURL"
	;;
	esac
}

get_net_hostname()
{
  if [ -n "`echo $NET_HOSTNAME | sed -n '/\*/p'`" ]; then
    CLIENT_NAME=`echo $NET_HOSTNAME | sed "s/\*/$CLIENT_MAC/"`
  else
    CLIENT_NAME=$NET_HOSTNAME
  fi
}

get_hostname()
{
  . $TS_GLOBAL

  if [ -z "$CLIENT_NAME" ] ; then
	if [ -z "$NET_HOSTNAME" ] ; then
		hostname $CLIENT_MAC
		CLIENT_NAME=$CLIENT_MAC
	else
		get_net_hostname
		restart_dhcp $CLIENT_NAME
	fi
	echo "CLIENT_NAME=$CLIENT_NAME" >> $TS_RUNTIME
  else
	restart_dhcp $CLIENT_NAME
  fi

}

# Get global config file

get_global_file()
{

. $TS_GLOBAL

echo_log "Getting global configuration from $SERVER_NAME ($SERVER_IP)" -n $debug
if `transport $BASEPATH/"$CONFNET"network /tmp/"$CONFNET"network $SERVER_IP` ; then
    cat /tmp/"$CONFNET"network >> $TS_NETWORK 2> /dev/null
    # Fixes any lines which haven't a CR/LF at end
    echo >> $TS_NETWORK
    echo_log "Ok" $debug
    cleanup
else
    echo_log "Not Found" $debug
    echo_log "Can't get global configuration file! Using default config" $debug
    if [ -e /tmp/"$CONFNET"network ] ; then
	    rm /tmp/"$CONFNET"network
    fi
    echo_log "Will continue boot in 5 seconds....." $debug
    sleep 5
fi
}


get_hosts_file()
{

. $TS_GLOBAL

HOSTFILE="$CONFHOSTS"hosts

if [ -n "$NET_HOSTNAME" ] ; then
  get_net_hostname
fi

if `transport $BASEPATH/$HOSTFILE /tmp/$HOSTFILE $SERVER_IP` ; then
  sed -e 's/\
//g' /tmp/$HOSTFILE > /tmp/$HOSTFILE.tmp
  cat /tmp/$HOSTFILE.tmp | sed "s/[[:blank:]][[:blank:]]*/ /g" | grep "^#" -v > $TS_HOSTS
  rm /tmp/$HOSTFILE
  rm /tmp/$HOSTFILE.tmp
  clientname=`cat $TS_HOSTS | grep -i $CLIENT_MAC | cut -d" " -f 1`
  if [ -n "$clientname" ] ; then
	  CLIENT_NAME=$clientname
  fi
fi

echo "CLIENT_NAME=$CLIENT_NAME" >> $TS_RUNTIME
}


get_ssh_known_hosts()
{
  . $TS_GLOBAL

  if `transport $BASEPATH/ssh_known_hosts /tmp/ssh_known_hosts $SERVER_IP` ; then
	if [ ! -d $HOME/.ssh ] ; then
		mkdir $HOME/.ssh
	fi
	cat /tmp/ssh_known_hosts > $HOME/.ssh/known_hosts
	rm /tmp/ssh_known_hosts
  fi
}

get_config_files()
{

. $TS_GLOBAL

# Added group configs

if [ -e $TS_HOSTS ] ; then
 echo_log "Getting group configurations..." -n $debug
 thingrouplist=`cat $TS_HOSTS |grep -i $CLIENT_MAC |cut -f3- -d" " |cut -f1 -d#`

 if [ -z "$thingrouplist" ] ; then
	echo_log "Not found!" $debug
 else
  for thingroup in $thingrouplist
  do
   if `transport $BASEPATH/$CONFGROUP$thingroup /tmp/$CONFGROUP$thingroup $SERVER_IP` > /dev/null 2>&1; then
	  cat /tmp/$CONFGROUP$thingroup >> $TS_NETWORK 2> /dev/null
	  # Fixes any lines which haven't a CR/LF at end
	  echo >> $TS_NETWORK
	  echo_log "\nAdding group parameters for $thingroup" -n $debug
	  rm /tmp/$CONFGROUP$thingroup
   else
	  echo_log "\nERROR: $thingroup not found!" $debug
	  sleep 2

   fi
  done
  cleanup
 fi
else
	echo_log "\nHosts file not found!" $debug
fi

# Machine Config

echo_log  "\nGetting configuration for hostname $CLIENT_NAME..." -n $debug
if `transport $BASEPATH/$CONFFILE$CLIENT_NAME /tmp/$CONFFILE$CLIENT_NAME $SERVER_IP` ; then
 cat /tmp/$CONFFILE$CLIENT_NAME >> $TS_NETWORK 2> /dev/null
 # Fixes any lines which haven't a CR/LF at end
 echo >> $TS_NETWORK
 echo_log "Ok\n" $debug
 rm /tmp/$CONFFILE$CLIENT_NAME
else
 if [ -e /tmp/$CONFFILE$CLIENT_NAME ] ; then
 	rm /tmp/$CONFFILE$CLIENT_NAME
 fi
 echo_log "Not Found\n" $debug
 echo_log "Getting configuration for IP address $CLIENT_IP..." -n $debug
 if `transport $BASEPATH/$CONFFILE$CLIENT_IP /tmp/$CONFFILE$CLIENT_IP $SERVER_IP` ; then
   cat /tmp/$CONFFILE$CLIENT_IP >> $TS_NETWORK 2> /dev/null
   # Fixes any lines which haven't a CR/LF at end
   echo >> $TS_NETWORK
   echo_log "Ok\n" $debug
   rm /tmp/$CONFFILE$CLIENT_IP
 else
   if [ -e /tmp/$CONFFILE$CLIENT_IP ] ; then
	   rm /tmp/$CONFFILE$CLIENT_IP
   fi
   echo_log "Not Found\n" $debug
   echo_log "Getting configuration for MAC address $CLIENT_MAC..." -n $debug
   if `transport $BASEPATH/$CONFFILE$CLIENT_MAC /tmp/$CONFFILE$CLIENT_MAC $SERVER_IP` ; then
      cat /tmp/$CONFFILE$CLIENT_MAC >> $TS_NETWORK 2> /dev/null
      # Fixes any lines which haven't a CR/LF at end
      echo >> $TS_NETWORK
      rm /tmp/$CONFFILE$CLIENT_MAC
      echo_log "Ok\n" $debug
   else
      if [ -e /tmp/$CONFFILE$CLIENT_MAC ] ; then
      	rm /tmp/$CONFFILE$CLIENT_MAC
      fi	
      echo_log "Not Found\n" $debug
   fi
 fi
fi
cleanup
}

# Checks for booting without dhcp/tftp server

NET_FILE_ENABLED=`make_caps $NET_FILE_ENABLED`

#Bring up Network

# If Manually configured network interface and still
#  wanting to use a tftp server for files
if [ -n "$NET_FILE_ALTERNATE" ] ; then
	SERVER_IP=$NET_FILE_ALTERNATE
	echo "SERVER_IP=$SERVER_IP" >> $TS_RUNTIME
fi
if [ "$NET_FILE_ENABLED" != "OFF" ] ; then
	get_global_file
	get_hosts_file
	get_config_files
	get_ssh_known_hosts
fi

# Checks for booting without dhcp/tftp server

NET_USE_DHCP=`make_caps $NET_USE_DHCP`

if [ "$NET_USE_DHCP" != "OFF" ] ; then
	get_hostname
fi

. $TS_GLOBAL

echo "127.0.0.1 localhost $NET_HOSTNAME" > /etc/hosts
if [ -n "$CLIENT_IP" ] ; then
	echo "$CLIENT_IP $CLIENT_NAME" >> /etc/hosts
else
	echo "127.0.0.1 $CLIENT_NAME" >> /etc/hosts
fi
if [ -n "$SERVER_IP" ] ; then
	echo "$SERVER_IP $SERVER_NAME" >> /etc/hosts
fi
