#!/bin/sh

firstBoot ()
{
  #create default port names (aliases)
  mac=$(set_nports 13)
  A=$(echo $mac | cut -b7-8)
  B=$(echo $mac | cut -b9-10)
  C=$(echo $mac | cut -b11-12)
  p=$(set_nports 2)
  n=1
  while [ $n -le $p ]
    do
    echo "s$n.serverfarm	${A}-${B}-${C}P$n" >> /etc/portslave/pslave.conf
    n=$(($n+1))
  done
  #start AIDP daemon
  /bin/aidpd &
}

mount -o remount,rw /dev/root / -t root
mount -a

cp /etc/portslave/pslave.conf /etc/portslave/pslave.save
cp /etc/network/firewall /etc/network/firewall.save
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.save
cp /etc/TIMEZONE /etc/TIMEZONE.save
cp /etc/pam.conf /etc/pam.conf.save
cp /etc/config_files /etc/config_files.save
cp /etc/group /etc/group.save
cp /etc/inetd.conf /etc/inetd.conf.save
cp /etc/inittab /etc/inittab.save
cp /etc/rc.sysinit /etc/rc.sysinit.save
cp /etc/websum.conf /etc/websum.conf.save
cp /etc/syslog-ng/syslog-ng.conf /etc/syslog-ng/syslog-ng.save
cp /etc/CA/global_appliance_key.txt /etc/CA/ssh_appliance_key

DATE=$(date)
YEAR=$(echo $DATE | cut -d" " -f6)
[ "$YEAR" -lt "1970" ] && date 010112002000

#Restore configuration from Flash

Mtype=$(set_nports 4)

umask 77
gunzip -c /proc/flash/script > /tmp/x 2>/dev/null

if [ $? -eq 0 ] ; then
  echo -n "Restoring configuration files from Flash ... "
  umask 22
  tar -xv -C / -f /tmp/x >/dev/null 2>/dev/null
  if [ $? -eq 0 ] ; then
    echo "done."
  else
    echo "error."
  fi
  rm -f /tmp/x
  #We do have a valid configuration in flash, but is it complete?
  tar -tvzf /proc/flash/script 2>/dev/null | grep pslave.conf > /dev/null 2>&1
  if [ $? -eq 1 ] ; then
    #No, it is not...
    firstBoot
  fi
  #Keep the same HW console speed throughout Linux execution
  /bin/setConSpeed
  /bin/upgrade.sh $1
  exit
fi

if [ "$Mtype" = "8" ] ; then
  # TS3000
  firstBoot
  #Keep the same HW console speed throughout Linux execution
  /bin/setConSpeed
  exit
fi

if [ "$Mtype" = "18" -o "$Mtype" = "12" -o "$Mtype" = "16" ] ; then
# TS100, SM100, and TS110
# 4096 - 7 * 64 (ori=2, alt=1, cnf+scr=2, old_scr=2)
skcnt=3648
else
# 4096 - 6 * 64 (ori=1, alt=1, cnf+scr=2, old_scr=2)
skcnt=3712
fi

echo -n "Restoring configuration files from old_script ... "
dd if=/proc/flash/.flash bs=1024 skip=$skcnt | zcat -c - > /tmp/x 2>/dev/null
if [ $? -ne 0 ]; then
  echo "not found."
else
  umask 22
  tar -xv -C / -f /tmp/x >/dev/null 2>/dev/null
  if [ $? = 0 ] ; then
    echo "done."
  else
    echo "error."
  fi
  gzip -c /tmp/x > /proc/flash/script 2>/dev/null
  if [ $? -eq 0 ] ; then
     # /dev/full was changed to return 0xFF's instead zeros
     dd of=/proc/flash/.flash bs=1024 count=128 seek=$skcnt if=/dev/full 2>/dev/null
     echo "Old configuration cleaned-up."
  fi
fi
rm -f /tmp/x
#Do we have a complete configuration saved to flash?
tar -tvzf /proc/flash/script 2>/dev/null | grep pslave.conf > /dev/null 2>&1
if [ $? -eq 1 ] ; then
  #No, we don't...
  firstBoot
else
  /bin/upgrade.sh $1
fi
#Keep the same HW console speed throughout Linux execution
/bin/setConSpeed
