#!/bin/sh
#
# gigEnn.update.sh
#       - update gigEnn
#
# NN License
# ----------
#	http://NetworkNightmare.com/License
#
#
# 11-Jan-08 amo Date-of-Birth
# 08-Feb-08 amo Assume latest files is in /usr.rw/tmp
# 11-Feb-08 amo Force the install, install the kernel
# 14-Feb-08 amo Just check for latest, use -install to do the actual install
# 16-Feb-08 amo Do NOT use this script to update gigEnn-OC12 yet
# 08-Mar-08 amo Allow for "-img xxxx.img.gz"
# 10-Mar-08 amo Do normal processing for -img files too
# 22-May-09 amo Separate to add more checking of /mnt/CF/boot, add gigEnn -kernel
# 12-Jun-09 amo Prefix sudo for gtar, added VER
# 25-Dec-11 amo Install kernel
#
#
VER=2009.06112
VER=2011.1225
#
#
if [ ! -f /..CF ];
then
  echo "#"
  echo "# ERROR: Please do NOT use this script to update gigEnn-OC3, gigEnn-OC12 .. not ready yet "
  echo "#"
  #
  exit 1
fi
#
#
# 6.3-RC1
FreeVer=`uname -r | sed -e s/-RC1// `
#
#
# Load the default functions ( needed for myeval )
# --------------------------
. /usr/local/gigEnn/.Conf/Func
#
#
ERR=0
DEBUG=0
FORCE=0
INSTALL=0
#
RootFS=0
KERNEL=0
IMG=0
IMAGE=""
#
SUDO=""
if [ `whoami` != "root" ];
then
  SUDO="/usr/local/bin/sudo"
fi
#
#
# For debugging user's command inputs
# -----------------------------------
cmdopts=$*
#
# Check the command line args
# ---------------------------
for arg in $1 $2 $3 $4 $5 $6 $7
do
  # do the "shift" manually ( to process -img 2008.mmdd.img.gz )
  #
  if [ "$arg" = "-img" ];
  then
    IMG=1
  elif [ "$IMG" = 1 ];
  then
    IMG=0
    IMAGE=$arg
  else
  #
  case $arg in
    -d)
	DEBUG=1
	;;
    -debug)
	DEBUG=1
	;;
    -v)
	echo $VER
	exit 1
	;;
    -force)
	FORCE=1
	;;
    -install)
	INSTALL=1
	;;
    -rootfs)
	RootFS=1
	;;
    -kernel)
	KERNEL=1
	;;
   esac
   fi
done
#
#
usage()
{
  echo "#"
  echo "# usage: gigEnn.update.sh [ -v ] [ -force ] [ -install ] < -rootfs | -kernel | -img foo.img.gz > "
  echo "#	-ver		show version and exit "
  echo "#	-force		force an install even if its the same .. "
  echo "#	-install	install the new rootfs or new kernel .. just checking otherwise "
  echo "#"
  #
  exit 1
}
#
#
echo "#"
echo "# $0 $cmdopts "
echo "#"
#
if [ "$RootFS" = 0 -a "$KERNEL" = 0 -a "$IMAGE" = "" ];
then
  usage
fi
#
# To upgrade gigEnn-OC12
# ======================
# mnt=mounted_dir
# if [ "$mnt" != "mounted" ];
# then
#   echo "#"
#   echo "# To upgrade gigEnn-OC12 "
#   echo "# ---------------------- "
#   echo "#   cd / ; sudo gtar zxvfp /boot/$img.tar.gz.. "
#   echo "#"
# fi
#
#
# Check that we can get outside to download new images
# ----------------------------------------------------
ping -c 3 www.gigEnn.net > /dev/null
stat=$?
#
if [ $stat != 0 ];
then
  echo "#"
  echo "# ERROR: Could not find http://gigEnn.net to download new update info .. Aborting updates.. "
  echo "#"
  #
  exit 1
fi
#
# make sure e can write to user-defined area
# ------------------------------------------
df | grep /usr.rw > /dev/null
stat=$?
echo "# ..already mounted ??..stat=$stat.."
if [ $stat = 0 ];
then
  if [ "$DEBUG" -gt 0 ]; then echo "# Make sure /usr.rw is writable.. " ; fi
  $SUDO mount -uw /usr.rw
fi
#
#
# Temporary space for new image
# -----------------------------
Dir=/usr.rw/tmp
LatestG="$Dir/latest.G.txt"
LatestK="$Dir/latest.K.txt"
#
if [ "$IMAGE" = "" ];
then
  #
  # ---------------------------
  # Get the latest update info
  # ---------------------------
  gigEnn -rootfs
  stat=$?
  #
  #
  if [ ! -f $LatestG -o $stat != 0 ];
  then
    echo "#"
    echo "# ERROR: upgrade could NOT download latest files .. " 
    echo "#"
    #
    exit 1
  fi
else
  #
  # ----------------------
  # Get a specific version
  # ----------------------
  echo "#"
  echo "# gigEnn -img $IMAGE "
  echo "#"
          gigEnn -img $IMAGE 
  #
  stat=$?
  if [ $stat != 0 ];
  then
    echo "#"
    echo "# ERROR: upgrade could NOT download image=$IMAGE.. " 
    echo "#"
    #
    exit 1
  fi
  #
  # syntax is different: md5  name  size
  LatestG="${Dir}/$IMAGE.md5"
  #
fi
#
#
# Check rootfs
# =============
CheckRootFS()
{
  if [ ! -z "$IMAGE" ];
  then
    #
    # syntax is different for test images: md5  name  size
    img=`cat $LatestG | awk '{ print $2 }' `
    siz=`cat $LatestG | awk '{ print $3 }' `
    md5=`cat $LatestG | awk '{ print $1 }' `
  else 
    #
    # released images
    #
    img=`cat $LatestG | awk '{ print $1 }' `
    siz=`cat $LatestG | awk '{ print $2 }' `
    md5=`cat $LatestG | awk '{ print $3 }' `
  fi
  #
  echo "#"
  echo "# checking new rootfs image=$img.. please wait a few seconds.. "
  #
  #
  #
  # =========================================
  # do some sanity checking before installing
  # =========================================
  #
  pimg=`ls -l /mnt/CF/boot/mfsroot.gz | awk '{ print $11 }' | cut -d '/' -f 2 `
  #
  if [ ! -f /mnt/CF/boot/$pimg ];
  then
    echo "#"
    echo "# rootfs ( $pimg ) does NOT exist .. mfsroot.gz is wrong ( corrupt or un-successful prev upgrade ) .. "
    echo "#"
    #
    exit 1
  fi
  #
  #
  psiz=`ls -l /mnt/CF/boot/$pimg | awk '{ print $5 }' `
  #
  # MD5 (/usr.rw/tmp/2008.mmdd.img.gz) = md5sum-data
  pmd5=`md5 /mnt/CF/boot/$pimg | awk '{ print $4 }' `
  #
  # echo "..pimg=$pimg..psiz=$psiz....pmd5=$pmd5.."
  # echo "...img=$img...siz=$siz.....md5=$md5.."
  #
  if [ "$pimg" != "$img" ];
  then
    ERR=1
    echo "# .. currently installed gigEnn version ( $pimg ) "
    echo "#      is NOT the same name as the latest downloaded version ( $img ) "
    echo "#"
    echo "#"
    #
  else
    echo "# .. currently installed gigEnn version ( $pimg ) is the same name as the latest downloaded version "
    #
    # check size
    # ----------
    if [ "$psiz" = "$siz" ];
    then
      echo "# .. currently installed gigEnn version is the SAME SIZE bytes as the latest downloaded version "
    else
      ERR=1
      echo "# .. SIZE MISMATCH between currently installed gigEnn version and the latest downloaded version "
    fi
    #
    # check md5sum
    # ------------
    if [ "$pmd5" = "$md5" ];
    then
      echo "# .. currently installed gigEnn version has the SAME MD5sum as the latest downloaded version "
    else
      ERR=1
      echo "# .. MD5 MISMATCH between currently installed gigEnn version and the latest downloaded version "
    fi
    #
    #
    if [ "$DEBUG" -gt 0 ];
    then
      echo "#"
      echo "#     current/prev = `ls -l /mnt/CF/boot/$pimg | awk '{ print $5 \"  \" $6 \"  \" $7 \"  \" $8 \"  \" $9 }` md5=$pmd5 "
      echo "#    download/new  = `ls -l         $Dir/$img  | awk '{ print $5 \"  \" $6 \"  \" $7 \"  \" $8 \"  \" $9 }` md5=$md5 "
      echo "#"
    fi
    #
    if [ "$FORCE" = 0 ];
    then
      if [ "$ERR" = 0 ];
      then
	INSTALL=0
	#
        echo "#"
        echo "# .. rootfs ( $img ) is current .. use 'gigEnn.update.sh -force' to force an overwrite if desired "
        echo "# =============================== "
        echo "#"
      fi
      #
    else
      INSTALL=1
      #
      echo "#"
      echo "# .. Found '-force' .. forcing an upgrade anyway .. "
      echo "#"
    fi
    #
  fi
  #
  # ---------------------------------
  # Check the MD5sum of the new image
  # ---------------------------------
  #	MD5 (/usr.rw/tmp/2008.mmdd.img.gz) = md5sum-data
  downloadMD5=` md5 $Dir/$img | awk '{ print $4 }' `
  #
  if [ "$md5" != "" ]
  then
    if [ "$downloadMD5" != "$md5" ];
    then
      echo "#"
      echo "# md5sum does NOT match .. aborting .."
      echo "# ===================== "
      echo "#   downloadMD5==$downloadMD5.. "
      echo "#   expectedMD5==$md5.."
      echo "#"
      #
    fi
  else
    echo "#"
    echo "# ERROR: downloaded image did not have its md5 "
    echo "#"
    #
  fi
  #
  #
  # Make sure we have enough disk space 
  # ------------------------------------
  #
  if [ "$INSTALL" = 0 ];
  then
    if [ "$ERR" != 0 ];
    then
      echo "#"
      echo "# To upgrade to latest rootfs: "
      echo "# ---------------------------- "
      echo "#    $0 -install $cmdopts "
      echo "#"
      echo "# or select 'Install new rootfs' vs Check from the wansim GUI"
      echo "#"
    fi
    #
  else 
    #
    # reset ( filename, size, md5 ) errors because we're gonna install a new version
    ERR=0
    #
    #
    echo "#"
    echo "# ------------------------------------------------------------------------------------------------- "
    echo "# Upgrading from previous rootfs=$pimg to latest rootfs=$img "
    echo "# ------------------------------------------------------------------------------------------------- "
    #
    myeval "$SUDO cp -p ${Dir}/$img /mnt/CF/boot "
    myeval "cd /mnt/CF/boot ; $SUDO mv mfsroot.gz mfsroot.$pimg ; $SUDO ln -s ./$img mfsroot.gz "
    #
    if [ -f "/mnt/CF/boot/$img" ];
    then 
      echo "#    `ls -la /mnt/CF/boot/$img ` "
    else
      ERR=1
      echo "# .. ERROR: something went wrong: missing rootfs=$img "
    fi
    #
    if [ -f "/mnt/CF/boot/mfsroot.gz" ]
    then
      echo "#    `ls -la /mnt/CF/boot/mfsroot.gz ` " 
    else
      ERR=1
      echo "# .. ERROR: something went wrong: missing mfsroot.gz link"
    fi
    #
    if [ $ERR = 0 ] ;
    then
      echo "#"
      echo "# Successfully Installed latest gigEnn software: "
    fi
    #
  fi
  #
  if [ "$DEBUG" -gt 0 ];
  then
    echo "#"
    if [ -f "/mnt/CF/boot/$pimg" ];
    then
      echo "#   previous = `ls -l /mnt/CF/boot/$pimg | awk '{ print $5 \"  \" $6 \"  \" $7 \"  \" $8 \"  \" $9 }` md5=$pmd5 "
    fi
    #
    if [ -f "/mnt/CF/boot/$img" ];
    then
      echo "#        new = `ls -l /mnt/CF/boot/$img  | awk '{ print $5 \"  \" $6 \"  \" $7 \"  \" $8 \"  \" $9 }` md5=$md5 "
    else
      ERR=1
      echo "# .. ERROR: something went wrong: missing rootfs=$img "
    fi
    echo "#"
  fi
  #
} # CheckRootFS
#
#
CheckBootKernel()
{
  #
  # 
# echo "# ------------------------------------------------------------------ "
# echo "# if you are super brave, you can try to boot the latest kernel, but "
# echo "# this step is NOT recommended for those that are NOT familiar with FreeBSD "
# echo "# ------------------------------------------------------------------ "
  #
  if [ ! -f /mnt/CF/boot/.arch.gigenn.txt ];
  then
    ERR=1
    echo "#"
    echo "#   -- ERROR: missing kernel info about i386, i586, i686 .. "
    echo "#   -- you cannot upgrade the kernel until this is resolved .. "
  else 
    #
    # 22-May-09 amo Separate to add more checking of /mnt/CF/boot, add gigEnn -kernel
    # ---------------------------
    # Get the latest update info
    # ---------------------------
    gigEnn -kernel
    stat=$?
    #
    if [ ! -f $LatestK -o $stat != 0 ];
    then
      echo "#"
      echo "# ERROR: upgrade could NOT download latest kernel files .. " 
      echo "#"
      #
      exit 1
    fi
    #
    tgz=`cat $LatestK | cut -d " " -f 1 `
    #
    echo "#"
    echo "# checking new boot kernel=$tgz.. "
    #
    cd /mnt/CF/boot 
    stat=$?
    if [ $stat != 0 ];
    then
      echo "#"
      echo "# ERROR: could not cd /mnt/CF/boot "
      echo "#"
      #
      exit 1
    fi
    #
    cd /mnt/CF/boot && sudo gtar zdf /usr.rw/tmp/$tgz > /dev/null 2>&1
    stat=$?
    #
    if [ $stat = 0 ];
    then
      if [ "$FORCE" = 0 ];
      then
        echo "# .. boot kernel is current .. use '$0 -force' to force an overwrite if desired "
        echo "# ========================= "
        echo "#"
        #
        INSTALL=0
      else
        echo "#"
        echo "# .. Found '-force' .. forcing an upgrade anyway .. "
        echo "#"
      fi
    else
      echo "#"
      echo "# .. boot kernel is different "
      echo "#"
    fi
    #
    if [ "$INSTALL" = 1 ];
    then
      echo "# .. installing latest boot kernel=$tgz .. "
      cd /mnt/CF/boot && sudo gtar zxf /usr.rw/tmp/$tgz > /dev/null 2>&1
      #
      kern=`cd /usr.rw/tmp && basename $tgz .tgz `
      cd /mnt/CF/boot && mv kernel kernel.xx && ln -s ./$kern kernel && cd
    fi
  fi
  #
} # CheckBootKernel
#
#
# Mount the /boot partitions
# ---------------------------
if [ -f /..CF ];
then
  #
  $SUDO mount /dev/ad0s1a /mnt/CF
  #
  # 22-May-09 amo Separate to add more checking of /mnt/CF/boot
  #
  if [ $stat != 0 ];
  then
    echo "#"
    echo "# ERROR: Could NOT mount /mnt/CF .. probably not gigEnn-T3 system "
    echo "#"
    #
    exit 1
  fi
  #
  #
  # df
  df
  df | grep /mnt/CF > /dev/null
  # stat=$?
  #
  # if [ $stat != 0 -o ! -d "/mnt/CF/boot" ];
  if [ ! -d "/mnt/CF/boot" ];
  then
    echo "#"
    echo "# ERROR: Could NOT mount /boot .. probably not gigEnn-T3 system "
    echo "#"
    #
    exit 1
  fi
  #
fi
#
echo "#"
echo "# Please wait while we do some sanity checking .. "
#
# ----------------------------------------------
# Check for latest rootfs image or bootk kernel
# ----------------------------------------------
#
if [ ! -z "$IMAGE" ]; then  CheckRootFS ; fi
if [ "$RootFS" = 1 ]; then  CheckRootFS ; fi
if [ "$KERNEL" = 1 ]; then  CheckBootKernel ; fi
#
cd
#
# umount /boot
# --------------
if [ -f /..CF ];
then
  #
  df | grep /mnt/CF > /dev/null
  stat=$?
  if [ $stat = 0 ];
  then
    echo "#"
    echo "# done upgrading.. unmounting the compact flash"
    echo "#"
    $SUDO umount /mnt/CF
  fi
  #
  df | grep /usr.rw > /dev/null
  stat=$?
  if [ $stat = 0 ];
  then
    if [ "$DEBUG" -gt 0 ]; then echo "# Make sure /usr.rw is readonly.. " ; fi
    $SUDO mount -ur /usr.rw
  fi
  #
fi
#
#
echo "#"
#
if [ "$ERR" = 0 ];
then
  if [ "$INSTALL" = 1 ];
  then
    echo "# ----------------------------------------------------- "
    echo "# Please reboot when ready to run the lastest version "
    echo "#   user defined config options have NOT been modified "
    echo "# ----------------------------------------------------- "
# else
#   echo "#  use -install to perform the upgrade installation .. "
#   echo "#       gigEnn.ui.sh -install $cmdargs "
  fi
else
  echo "# ---------------------------------------------------------------------- "
  echo "# There is some errors .. Please fix them now .. Please do NOT reboot .. "
  echo "# ---------------------------------------------------------------------- "
fi
echo "#"
#
#
# End of file

