#!/bin/sh
#(c) Copyright Barry Kauler, Dec. 28, 2013. bkhome.org
#from the quirky-*.usfs.xz files, create service packs, for upgrading existing quirky installation.
#140103 make sure 2nd-partition f.s. set correctly. fix for "p" in mmcklb0p1.
#140106 create-devx-service-packs, based in create-service-packs.
#140107 cmptree: detect changed symlinks.
#140120 work in new quirky build system.
#140203 fix a path.
#140216 please wait msg in pinstall.sh
#141026 change format devx_service_pack-${VER1}_TO_${VER2}-${DISTRO_FILE_PREFIX}.pet to devx_service_pack-${VER1}_TO_${VER2}-[${OLD_FILE_PREFIX}_TO_]${DISTRO_FILE_PREFIX}.pet
#       in other words, optional extra field when upgrade from, say, tahr to unicorn.

[ ! -f DISTRO_SPECS ] && exit
. ./DISTRO_SPECS

[ ! -d rootfs-skeleton ] && exit
#running in quirky build system.
if [ -d sandbox3 ];then
 cd sandbox3
 mkdir -p mntpt1
 mkdir -p mntpt2
else
 echo
 echo "You need to run 3builddistro first, to build sandbox3/devx*.sfs"
 exit
fi

#true version-number sort...
FLG=1
QLIST="$(ls -1 devx*.sfs | sort --version-sort --key=2 --field-separator='-' | tr '\n' ' ')" #ex: devx-1.2.3-tahr.sfs
echo -n "$QLIST" > /tmp/audit-qlist

for AQ in `cat /tmp/audit-qlist`
do
 VER1="$(echo -n "$AQ" | cut -f 2 -d '-')"
 PREFIX1="$(echo -n "$AQ" | cut -f 3 -d '-' | cut -f 1 -d '.')" #141026
 for NEXTQ in `cat /tmp/audit-qlist`
 do
  [ "$NEXTQ" = "$AQ" ] && continue
  VER2="$(echo -n "$NEXTQ" | cut -f 2 -d '-')"
  PREFIX2="$(echo -n "$NEXTQ" | cut -f 3 -d '-' | cut -f 1 -d '.')" #141026
  if vercmp $VER2 le $VER1;then continue; fi

  if [ "$PREFIX1" != "$PREFIX2" ];then #141026
    [ -f devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}_TO_${PREFIX2}.pet ] && continue
    echo
    echo "Do you want to create devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}_TO_${PREFIX2}.pet?"
    echo -n "ENTER only for yes: "
    read yeswant
  else
    [ -f devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}.pet ] && continue #${DISTRO_FILE_PREFIX}
    echo
    echo "Do you want to create devx_service_pack-${VER1}_TO_${VER2}-${PREFIX2}.pet?"
    echo -n "ENTER only for yes: "
    read yeswant
  fi
  [ "$yeswant" != "" ] && continue

  #if [ ! -f devx_service_pack-${VER1}_TO_${VER2}-${DISTRO_FILE_PREFIX}.pet ];then
   busybox mount -t squashfs -o loop,ro ${AQ} mntpt1
   busybox mount -t squashfs -o loop,ro ${NEXTQ} mntpt2

   echo "Running 'cmptree' utility..."
   cmptree mntpt1 mntpt2 > /tmp/csp-cmptree
   sync
   
   if [ "$PREFIX1" != "$PREFIX2" ];then #141026
    rm -rf devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}_TO_${PREFIX2} 2>/dev/null
    mkdir devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}_TO_${PREFIX2}
    REALDIR="devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}_TO_${PREFIX2}"
   else
    rm -rf devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1} 2>/dev/null
    mkdir devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}
    REALDIR="devx_service_pack-${VER1}_TO_${VER2}-${PREFIX1}"
   fi
   
   #files to be deleted...
   grep '^mntpt2/.*does not exist.$' /tmp/csp-cmptree > /tmp/csp-removed-files
   #changed files...
   grep ' different from ' /tmp/csp-cmptree > /tmp/csp-changed-files
   #140107 symlinks have changed...
   grep 'different link targets ' /tmp/csp-cmptree > /tmp/csp-relinked-files
   #new files...
   grep '^mntpt1/.*does not exist.$' /tmp/csp-cmptree > /tmp/csp-new-files
   
   #changed and new files...
   echo "Installing changed and new dirs/files from mntpt2..."
   echo -n '' > /tmp/csp-install-err-log
   #i don't think will try to fix hyperlinks, as they get fixed when pet installed...
   sed -e 's%^mntpt2%%' -e 's%^mntpt1%%' /tmp/csp-changed-files /tmp/csp-new-files | cut -f 1 -d ' ' |
   while read ACHG
   do
    APATH="$(dirname "$ACHG")"
    mkdir -p ${REALDIR}"$APATH"
    cp -a -f --remove-destination mntpt2"${ACHG}" ${REALDIR}"${APATH}"/ 2>> /tmp/csp-install-err-log
   done
   sync
   #140107 symlinks changed...
   sed -e 's% mntpt2/% /%' -e 's% mntpt1/% /%' /tmp/csp-relinked-files | cut -f 4 -d ' ' |
   while read ACHG
   do
    APATH="$(dirname "$ACHG")"
    mkdir -p ${REALDIR}"$APATH"
    cp -a -f --remove-destination mntpt2"${ACHG}" ${REALDIR}"${APATH}"/ 2>> /tmp/csp-install-err-log
   done
   sync

   #delete files, put into pinstall.sh...
   echo "Creating the pinstall.sh script..."
   echo '#!/bin/ash' > ${REALDIR}/pinstall.sh
   echo "#post-install script, upgrading 'devx' from ${VER1} to ${VER2}." >> ${REALDIR}/pinstall.sh
   echo "yaf-splash -bg orange -fg black -close never -fontsize large -text \"Running post-install script, please wait...\" &" >> ${REALDIR}/pinstall.sh
   echo 'YPID=$!' >> ${REALDIR}/pinstall.sh
   echo '' >> ${REALDIR}/pinstall.sh
   echo "#These directories and files were in ${VER1}, not in ${VER2}..." >> ${REALDIR}/pinstall.sh
   sed -e 's%^mntpt2%%' /tmp/csp-removed-files | cut -f 1 -d ' ' |
   while read ADEL
   do
    [ ! "$ADEL" ] && continue
    if [ -d "mntpt1${ADEL}" ];then    #140203 fix path.
     echo "rm -rf \"$ADEL\"" >> ${REALDIR}/pinstall.sh
    else
     echo "rm -f \"$ADEL\"" >> ${REALDIR}/pinstall.sh
    fi
   done
   
   echo 'pupkill $YPID' >> ${REALDIR}/pinstall.sh
   echo "pupmessage -bg '#80FF80' -title \"devx Service Pack installed\" \"The 'devx' in Quirky has been updated from version ${VER1} to ${VER2}.
   
   The 'devx' is a complete C/C++/Vala/Genie/BaCon compiler environment, with many extras, such as nasm, yasm, python, git, svn, gdb.

It is essential that you reboot now, for changes to take effect. Please reboot now, do not keep using Quirky (after clicking the 'OK' button, wait a few minutes though, for the upgrade to fully complete).\"" >> ${REALDIR}/pinstall.sh #140216
   chmod 755 ${REALDIR}/pinstall.sh
   
   sync
   umount mntpt1
   umount mntpt2
   echo "Directory ${REALDIR} fully populated."
   
   SIZEK="$(du -s -k ${REALDIR} | cut -f 1)"
   REALEXT="$(echo -n "$REALDIR" | cut -f 2- -d '-')" #141026
   echo "${REALDIR}|devx_service_pack|${REALEXT}||Setup|${SIZEK}K||${REALDIR}.pet||upgrade Quirky from ${PREFIX1} ${VER1} to ${PREFIX2} ${VER2}|${DISTRO_BINARY_COMPAT}|${DISTRO_COMPAT_VERSION}||" > ${REALDIR}/pet.specs
   
   [ -f ${REALDIR}.tar.gz ] && rm -f ${REALDIR}.tar.gz
   [ -f ${REALDIR}.pet ] && rm -f ${REALDIR}.pet
   dir2tgz ${REALDIR}/
   tgz2pet ${REALDIR}.tar.gz
   sync
   echo "PET ${REALDIR}.pet created."
  #fi
 done

done

###END###
