#!/bin/sh
# Major update - Stage 1
##################################################################
PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin"
export PATH
PROGDIR="/usr/local/share/pcbsd/pc-updatemanager" ; export PROGDIR

DOWNLOADDIR="/usr/local/tmp"
STAGEDIR="${DOWNLOADDIR}/update-stagedir"
UPVER=`cat ${STAGEDIR}/sys-ver`

clear

echo "# PC-BSD System Upgrade"
echo "# Updating to $UPVER"
echo "# Please do NOT reboot the system until the update is finished"
echo "##############################################################"


# Start by cleaning old pkg's
echo "Cleaning old system pkgs..."

# Create the pkg list
pkg_info > /tmp/.pkg-remove.$$

# Delete the pkgs
while read line
do
  dPkg=`echo $line | cut -d ' ' -f 1`

  printf "\r%-${length}s\r"
  printf "%-${length}s\r" "${dPkg}"
  length=${#dPkg}

  pkg_delete -f "${dPkg}" >/dev/null 2>/dev/null

done < /tmp/.pkg-remove.$$
rm /tmp/.pkg-remove.$$
echo ""

# Start extracting the updated world now
echo -e "Extracting updated world environment...\c"
cd /

# Remove any old QSettings backends - This can be remove after 9.0-Final is released
rm /usr/local/etc/xdg/PCBSD.conf
rm /usr/local/etc/xdg/Trolltech.conf

# Remove the old meta-pkg data
rm -rf /usr/local/share/pcbsd/base-ports

# Exclude ./boot in extract will do after world is done
echo "./boot" >> ${STAGEDIR}/xtra-data/upgrade-excludes

tar xvpf ${STAGEDIR}/PCBSD.tbz -X ${STAGEDIR}/xtra-data/upgrade-excludes >/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
  echo "DONE"
else
  echo "DONE!"
fi

# Start extracting the boot
echo -e "Extracting kernel and boot environment...\c"
cd /

# Backup the old kernel
rm -rf /boot/kernel.old >/dev/null 2>/dev/null 
mv /boot/kernel /boot/kernel.old
echo -e ".\c"

mkdir /.boot-tmp
tar xvpf ${STAGEDIR}/PCBSD.tbz -C /.boot-tmp ./boot >/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
  echo -e ".\c"
else
  echo -e "x.\c"
  sleep 5 
fi

# Remove loader.conf
rm /.boot-tmp/boot/loader.conf
rm /.boot-tmp/boot/device.hints

tar cvf - -C /.boot-tmp/boot . 2>/dev/null | tar xvf - -C /boot >/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
  echo "DONE"
else
  echo "DONE!"
  sleep 5
fi

# Cleanup the boot tmp files
rm -rf /.boot-tmp

# Remove deleted files
cd /
oList="${STAGEDIR}/xtra-data/system-file.list.old"
nList="${STAGEDIR}/xtra-data/base-ports/system-file.list"
if [ -e "${oList}" -a -e "${nList}" ] ; then
  echo -e "Cleaning up old files...\c"

  # Look for files which have been removed
  diff ${oList} ${nList} | grep "<" | cut -d ' ' -f >/tmp/.rmFiles.$$ 2>/dev/null
  while read dFile
  do
    if [ -z "$dFile" -o ! -e "${dFile}" ] ; then continue ; fi
    cat ${nList} | grep -w "$dFile" >/dev/null 2>/dev/null
    if [ $? -eq 0 ] ; then
	echo "Removing $dFile"
	#rm ${dFile}
	# KPM Testing
    fi
  done < /tmp/.rmFiles.$$
  rm /tmp/.rmFiles.$$

  echo "DONE"
fi

# Merge changed files
cd /
if [ -e "${STAGEDIR}/upgrade-merges" ] ; then
  echo -e "Merging changes...\c"

  # Extract the new files into a tmpdir
  mkdir /tmp/merge-files.$$
  tar xvpf ${STAGEDIR}/PCBSD.tbz -T ${STAGEDIR}/xtra-data/upgrade-merges -C /tmp/merge-files.$$ >/dev/null 2>/dev/null

  while read mFile
  do
    if [ ! -e "${mFile}" ] ; then continue ; fi 
    if [ ! -e "/tmp/merge-files.$$/${mFile}" ] ; then continue ; fi

    # Create a backup copy of merged file
    cp "${mFile}" "${mFile}.preMerge"

    while read mLine
    do
      echo $mLine | grep "^#" >/dev/null 2>/dev/null  
      if [ $? -eq 0 ] ; then continue ; fi
      echo $mLine | grep "=" >/dev/null 2>/dev/null  
      if [ $? -ne 0 ] ; then continue ; fi

      key=`echo $mLine | cut -d '=' -f 1`
      val=`echo $mLine | cut -d '=' -f 2-30`
      if [ -z "$key" -o -z "$val" ] ; then continue ; fi

      # Got this var, found valid key to check for in merge file
      cat ${mFile} | grep "^$key" >/dev/null 2>/dev/null
      if [ $? -eq 0 ] ; then continue ; fi 
      
      # Add the new key to the merge file
      echo $mLine >> ${mFile}
      echo -e ".\c"
    done < /tmp/merge-files.$$/$mFile

  done < ${STAGEDIR}/upgrade-merges
  rm -rf /tmp/merge-files.$$
  echo -e "DONE"
fi


# Copy over the stage 2 setup script
mv /etc/rc /etc/rc.backup
cp ${STAGEDIR}/mu-rc /etc/rc
cp ${STAGEDIR}/mu-stage2 ${STAGEDIR}/doupdate.sh

echo -e "Rebooting for stage 2 of upgrade...\c"
sync
sleep 5
(shutdown -r now) &
sleep 500
