#!/bin/sh
# IceOS installer script
#  iceinstaller cmd devos devin dirin
#
# cmd = preinstall|reinstall|send
# devos = device to install new OS on
# devin = device hosting the iceiinstaller.tar file
# dirin = directory hosting the iceinstaller.tar file
#

saveoff() {
  if [ ! -d $1 ]; then 
    echo "Not saving $1 not found" 
    return 1
  fi
  cd $1
  size=`du -sm . | awk '{print $1}'`
  cd $pathin
  echo -e "$3" > icetarx
  echo "Save $1 files to `pwd`/$2"
  if [ $size -gt 60 ]; then
    echo "***** Size=$size MBy too large to save. Prune to under 60MBy. ***** "
  else
    tar -cf $2 -X icetarx -C $1 .
    ls -alh $2
  fi
  echo ""
}

cmd=$1
if [ "$cmd" = "" -a "$PWD" = "/var/local" ]; then cmd=preinstall ; fi

devos=$2
if [ "$devos" = "" ]; then devos=/dev/sda ; fi
host=$2
if [ "$host" = "" ]; then host=icetray ; fi

devin=$3
if [ "$devin" = "" ]; then devin=${devos}3 ; fi

dirin=$4
if [ "$dirin" = "" ]; then dirin=/var/local ; fi

pathin=$dirin

users="root iceman icemkr iceuser"

echo "Run iceinstaller opt=$cmd"

if [ "$cmd" = "sendrm" ]; then
  scp iceinstaller.readme $host:$pathin
  exit
fi
if [ "$cmd" = "sendl" ]; then
  scp iceinstaller-lite.tar $host:$pathin/iceinstaller.tar
  exit
fi
if [ "$cmd" = "send" ]; then
  scp iceinstaller-full.tar $host:$pathin/iceinstaller.tar
  exit
fi
if [ "$cmd" = "sendu" ]; then
  if [ "$4" = "" ]; then pathin=/mnt/data/updates ; fi
  scp iceinstaller-full.tar $host:$pathin/iceinstaller.tar
  exit
fi
if [ "$cmd" = "sendx" ]; then
  if [ "$4" = "" ]; then pathin=/mnt/data/updates ; fi
  scp iceinstaller-fullx.tar $host:$pathin/iceinstaller.tar
  exit
fi
if [ "$cmd" = "sendy" ]; then
  if [ "$4" = "" ]; then pathin=/mnt/data/updates ; fi
  scp iceinstaller-full.tar $host:$pathin/iceinstaller.tar
  exit
fi
if [ "$cmd" = "sendz" ]; then
  if [ "$4" = "" ]; then pathin=/mnt/data/updates ; fi
  scp iceinstaller-fullz.tar $host:$pathin/iceinstaller.tar
  exit
fi
if [ "$cmd" = "makeusb" ]; then
  tar -xvf iceinstaller.tar 
  ./makeusb $2
  exit
fi
if [ "_$devos" = "_" ]; then
  echo "Problem getting ICEOS device. Aborting ..."
  echo "To create a live boot USB stick, user ./iceinstaller makeusb <usbdevname>"
  exit
fi

if [ "$cmd" = "reinstall" ]; then

  pathin=/mnt/tmp/$dirin
  echo "Run iceinstaller opt=$cmd on devos=$devos from devin=$devin dirin=$dirin"

  echo "Copy installation archives into RAM"
  mount $devin /mnt/tmp
  tar -xf ${pathin}/iceinstaller.tar -C /var/ 
  for id in $users ; do
    if [ -e ${pathin}/${id}_home.tar ]; then cp ${pathin}/${id}_home.tar /var/ ; fi
  done
  if [ -e ${pathin}/icevar_etc.tar ]; then cp ${pathin}/icevar_etc.tar /var/ ; fi
  umount /mnt/tmp

  echo "Restore / from installation archive"
  mount ${devos}2 /mnt/tmp
  rm -rf /mnt/tmp/*
  tar -xzf /var/iceroot.tz -C /mnt/tmp/
  /mnt/tmp/etc/iceos/checkRoot
  umount /mnt/tmp

  echo "Restore /user from installation archives"
  mount ${devos}3 /mnt/tmp
  rm -rf /mnt/tmp/*
  if [ -e /var/iceuser.tz ];  then tar -xzf /var/iceuser.tz  -C /mnt/tmp/ ; fi
  if [ -e /var/iceuserd.tz ]; then tar -xzf /var/iceuserd.tz -C /mnt/tmp/ ; fi
  if [ -e /var/icepods.tar ]; then tar -xf  /var/icepods.tar -C /mnt/tmp/opt/ ; fi

  echo "Restore /var/etc scripts from installation archive"
  if [ -e /var/icevar_etc.tar ]; then tar -xpf /var/icevar_etc.tar -C /mnt/tmp/var/etc/ ; fi
  mv /mnt/tmp/home/iceman/icebox.tbl /mnt/tmp/home/iceman/icebox.tbl.base

  for id in $users ; do
    echo "Restore $id from installation archive"
    if [ -e /var/${id}_home.tar ]; then tar -xpf /var/${id}_home.tar -C /mnt/tmp/home/${id}/ ; fi
  done
  umount /mnt/tmp

elif [ "$cmd" = "preinstall" ]; then

  if [ $dirin != "/var/local" ]; then
    pathin=`findmnt -n -o TARGET $devin`/$dirin
  fi

  cd $pathin
  rm -rf ice*.tz
  if [ "$1" != "preinstall" ]; then
    echo -n "Save off user data [y/n]? "; read ans ;
  else
    ans="y"
  fi
  if [ "$ans" = "y" ]; then
    echo " "
    saveoff /user/var/etc/     icevar_etc.tar "./.*\ndocker\nopenvpn"
    saveoff /user/home/root/   root_home.tar "./.*\niceconsole\nmake*"
    saveoff /user/home/iceman/ iceman_home.tar "./.*\niceshell\niceserver\nvncserver"
    saveoff /user/home/icemkr/ icemkr_home.tar "./.*"
    saveoff /user/home/iceuser/ iceuser_home.tar "./.*"
    echo "Your old data and configuration files will be restored after the upgrade,"
    echo "   but you may wish to save them to another computer as well."
  fi
  if [ -d /mnt/data11/users ]; then
    echo "Move data file to new format"
    mkdir /mnt/data11/data11
    mv /mnt/data11/*.* /mnt/data11/data11/
    mv /mnt/data11/users /mnt/data11/data1
  elif [ -d /mnt/data11 ]; then
    echo "Create /mnt/data11/data1/<user> for new /user/data1/<user> links"
    mkdir -p -m 777 /mnt/data11/data1
    for id in $users ; do
      mkdir -p -m 777 /mnt/data11/data1/$id  
      mv /user/data1/$id /mnt/data11/data1/
    done
  fi
  if [ "$1" != "preinstall" ]; then
    echo -n "If all completed without errors, answer Y to replace the boot partition files [y/n]? "; read ans ;
    if [ "$ans" != "y" ]; then exit; fi
  fi 
  mount ${devos}1 /mnt/fat
  echo " "
  echo "Replace boot partition files."
  tar -xf iceinstaller.tar iceboot.tz
  rm -rf /mnt/fat/*
  tar -xzf iceboot.tz -C /mnt/fat/
  rm iceboot.tz
  cp iceinstaller /mnt/fat/etc/
  echo "reinstall $devos $devin $dirin" >> /mnt/fat/etc/reinstall
  umount /mnt/fat
  echo "Verify reinstallation disk space."
  df / /user
  echo "If all completed without errors, reboot the system to finish the install."; 

else
  cat $0.readme
fi
