#!/bin/sh
#(c) Copyright bkauler 2008, puppylinux.com
#2008 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#v404 written from scratch for UniPup.
#v411 run rc.alsa as it also unloads modules (if not done, prevents shutdown some systems).
#v412 /etc/DISTRO_SPECS, renamed pup_xxx.sfs, pup_save.2fs etc.
#v555 pup files renamed to woofr555.sfs, woofsave.tar.gz, woofu555.sfs.
#w468 alsa shutdown now handled in /etc/init.d/alsa.

. /etc/rc.d/PUPSTATE #created by rc.sysinit.
. /etc/DISTRO_SPECS
#PUPPYVERSION=`cat /etc/puppyversion`

killzombies() {
 ZOMBIES="`ps -H -A | grep '<defunct>' | sed -e 's/  /|/g' | grep -v '|||' | cut -f 1 -d ' ' | tr '\n' ' '`"
 for ONEZOMBIE in $ZOMBIES
 do
  echo "Killing parentless zombie process $ONEZOMBIE"
  kill $ONEZOMBIE
 done
}

#some packages have a service script that requires stopping...
for service_script in /etc/init.d/*
do
  [ -x $service_script ] && $service_script stop
done

#unmount stray filesystems...
USRPART=""
[ "$USRSFS" ] && USRPART="/dev/`echo -n "$USRSFS" | cut -f 1 -d ','`"
for ONESTRAY in `mount | grep -o '^/dev/[a-zA-Z0-9]*' | grep -v '/dev/loop[01]'`
do
 [ "$USRPART" = "$ONESTRAY" ] && continue
 echo "Unmounting $ONESTRAY..."
 fuser -k -m $ONESTRAY
 killzombies
 sync
 umount -r $ONESTRAY
done

#save session...
#add more files to exclusion list...
echo '/etc/rc.d/rc.sysinit
/sbin/pup_event_backend_d
/sbin/pup_event_load_module' >> /tmp/init_startup_files
#echo '/root/.thumbnails' >> /tmp/init_startup_files
if [ "$PUPSAVE" ];then #see /etc/rc.d/PUPSTATE
 UNIPART="`echo -n "$PUPSAVE" | cut -f 1 -d ','`"
 UNIFS="`echo -n "$PUPSAVE" | cut -f 2 -d ','`"
 UNIPUP="`echo -n "$PUPSAVE" | cut -f 3 -d ','`"
 STATUS=0
 aPATTERN="/dev/${UNIPART} "
 if [ "`mount | grep "$aPATTERN"`" = "" ];then
  mount -t $UNIFS /dev/$UNIPART /mnt/$UNIPART
  STATUS=$?
 fi
 if [ $STATUS -eq 0 ];then
  echo "Saving session to '${DISTRO_FILE_PREFIX}save.tar.gz' in partition ${UNIPART}..."
  #save all except files that are in initrd.gz and ${DISTRO_FILE_PREFIX}uxxx.sfs...
  tar -c -z -l -f - -X /tmp/init_startup_files /dev /bin /sbin /etc /root /opt /lib /.mnt/tmpfs1 > /mnt/$UNIPART/${DISTRO_FILE_PREFIX}save.tar.gz
  sync
  umount /dev/$UNIPART
 else
  echo "Unable to save session, could not mount /dev/${UNIPART}"
 fi
else
 echo
 echo "Early version of this script! Here are the partitions:"
 PROBEPART="`probepart -m | sed -e 's%^/dev/%%' | grep -E 'ext2|ext3|reiserfs|vfat|ntfs'`"
 echo "$PROBEPART"
 echo
 echo "Choose a partition, or press ENTER only not to save session."
 echo -n "Type the name of the partition you want to save to (ex: sda1): "
 read UNIPART
 if [ "$UNIPART" != "" ];then
  aPATTERN='^'"$UNIPART"'|'
  UNIFOUND="`echo "$PROBEPART" | grep "$aPATTERN"`"
  UNIPART="`echo -n "$UNIFOUND" | cut -f 1 -d '|'`"
  UNIFS="`echo -n "$UNIFOUND" | cut -f 2 -d '|'`"
  UNISIZE="`echo -n "$UNIFOUND" | cut -f 3 -d '|'`"
  mkdir -p /mnt/$UNIPART
  STATUS=0
  aPATTERN="/dev/${UNIPART} "
  if [ "`mount | grep "$aPATTERN"`" = "" ];then
   mount -t $UNIFS /dev/$UNIPART /mnt/$UNIPART
   STATUS=$?
  fi
  if [ $STATUS -eq 0 ];then
   echo "Saving session to '${DISTRO_FILE_PREFIX}save.tar.gz' in partition ${UNIPART}..."
   #save all except files that are in initrd.gz (see rc.sysinit) and ${DISTRO_FILE_PREFIX}uxxx.sfs...
   #/.mnt/tmpfs1 is top layer in /usr layered f.s. ...
   tar -c -z -l -f - -X /tmp/init_startup_files /dev /bin /sbin /etc /root /opt /lib /.mnt/tmpfs1 > /mnt/$UNIPART/${DISTRO_FILE_PREFIX}save.tar.gz
   sync
   #if ${DISTRO_FILE_PREFIX}uxxx.sfs on cd, copy it to same place as ${DISTRO_FILE_PREFIX}save...
   if [ "`echo -n "$USRSFS" | grep 'iso9660'`" != "" ];then
    USRPART="`echo -n "$USRSFS" | cut -f 1 -d ','`"
    USRFS="`echo -n "$USRSFS" | cut -f 2 -d ','`"
    USRNAME="`echo -n "$USRSFS" | cut -f 3 -d ','`"
    if [ -f /mnt/${USRPART}${USRNAME} -a ! -f /mnt/${UNIPART}${USRNAME} ];then #precaution, not necessary though.
     ###TODO check space###
     echo "Copying $USRNAME to $UNIPART so optical drive freed for other uses..."
     cp -a /mnt/${USRPART}${USRNAME} /mnt/${UNIPART}/
     sync
    fi
   fi
   umount /dev/$UNIPART
  fi
 fi
fi

#unmount swap partitions...
for ONESTRAY in `cat /proc/swaps | grep '/dev/' | cut -f 1 -d ' '`
do
 sync
 swapoff $ONESTRAY
done


#busybox umount -ar > /dev/null 2>&1
###END###
