#!/bin/sh

echo -n "Waiting for da0s2a, please wait...."
while [ ! -c /dev/da0s2a ]
do
 /rescue/ls -alihR /var/tmp/mnt >/dev/null 2>&1
done

echo -n "Extracting upgrade image, please wait..."

/rescue/mount -o ro /dev/da0s2a /mnt
/rescue/mount -t tmpfs tmpfs /installer
/rescue/mkdir -p /installer/.mount/FreeNAS

/rescue/tar xf /mnt/gui-install-environment.tar  -C /installer || (echo "FAILED BASE EXTRACTION" && /bin/sh && /rescue/sleep 15 && /rescue/reboot)
/rescue/tar xf /mnt/gui-packages.tar -C /installer/.mount || (echo "FAILED PACKAGE EXTRACTION" && /bin/sh && /rescue/sleep 15 && /rescue/reboot)
/rescue/mv /installer/.mount/Packages /installer/.mount/FreeNAS/Packages
/rescue/umount /mnt

echo " Done!"
echo -n "Applying upgrade..."

# Set up chroot jail for the first time installer
/rescue/mount -t devfs devfs /installer/dev
#echo "Starting a shell now; exit when done"
#/rescue/sh

/rescue/chroot /installer /bin/sh /etc/install.sh "da0" || (echo "FAILED" && /bin/sh && /bin/sh && /rescue/reboot)
echo " Done!"
echo "Rebooting..."
/rescue/reboot
