#!/bin/sh
# script to import from puppy1 to puppy2.

echo "This script will import installed packages and personal files from a"
echo "pupxxx file (the personal storage file used by Puppy 1.x.x) to Puppy2."
echo "But, you have to know the partition in which it resides."
echo
echo "here are the partitions in the PC:"
PCPARTS="`probepart | grep '^/dev/' | sed -e 's/none|.*NTFS.*/ntfs|/g' | cut -f 1-2 -d '|' | grep --extended-regexp 'ntfs|msdos|vfat|ext2|ext3|reiserfs|minix'`"
echo "0  abort-exit-quit"
CNTPARTS=1
CHOICES="`echo -n "$PCPARTS" | tr "\n" " "`"
for ONEPART in $CHOICES
do
 echo "$CNTPARTS  $ONEPART"
 CNTPARTS=`expr $CNTPARTS + 1`
done
echo -n "Please type a number then press ENTER key: "
read CHOSENNUM
[ $CHOSENNUM -eq 0 ] && exit
CHOSENPART="`echo -n "$CHOICES" | cut -f $CHOSENNUM -d " "`"

echo
echo "You have chosen $CHOSENPART"
INPART="`echo -n "$CHOSENPART" | cut -f 1 -d '|' | cut -f 3 -d '/'`"
INFS="`echo -n "$CHOSENPART" | cut -f 2 -d '|'`"
MNTPT="`mount | grep /dev/$INPART | tr -s " " | cut -f 3 -d " "`"
if [ "$MNTPT" = "" ];then
 mount -t $INFS /dev/$INPART /mnt/$INPART
 MNTPT="/mnt/$INPART"
fi

echo
echo "Here are the found pupxxx files:"
PUPFILES="`find /mnt/$INPART -maxdepth 1 -name pup???`"
echo "0  abort-exit-quit"
CNTPARTS=1
CHOICES="`echo -n "$PUPFILES" | tr "\n" " "`"
for ONEPUP in $CHOICES
do
 echo "$CNTPARTS  $ONEPUP"
 CNTPARTS=`expr $CNTPARTS + 1`
done
echo -n "Please type a number then press ENTER key: "
read CHOSENNUM
[ $CHOSENNUM -eq 0 ] && exit
CHOSENPUP="`echo -n "$CHOICES" | cut -f $CHOSENNUM -d " "`"

echo
echo "You have chosen $CHOSENPUP"

losetup /dev/loop2 $CHOSENPUP
mkdir /tmp/chosenpupmntpt 2>/dev/null
mount -t ext2 /dev/loop2 /tmp/chosenpupmntpt

echo
echo "Now probing for installed PupGet packages and DotPups that are"
echo "registered with the PupGet package manager."
echo "Note, it is recommended that you do not import these, and instead"
echo "reinstall them in Puppy2."
PUPGETFILES="`ls -1 /tmp/chosenpupmntpt/root/.packages/*.files 2> /dev/null`"
for APUPGETFILE in $PUPGETFILES
do
 APACKAGE="`basename $APUPGETFILE | sed -e 's/\.files//g'`"
 echo
 echo -n "Do you want to import package ${APACKAGE}? y|n: "
 read ASKYESNO
 if [ "$ASKYESNO" = "y" ];then
  cat $APUPGETFILE |
  while read ONEFILE
  do
   echo "Copying ${ONEFILE}..."
   ONEDIR="`dirname $ONEFILE`"
   mkdir -p $ONEDIR
   cp -a --remove-destination /tmp/chosenpupmntpt${ONEFILE} ${ONEDIR}/
  done
  PKGFILES="`ls -1 /tmp/chosenpupmntpt/root/.packages/${APACKAGE}.* 2> /dev/null`"
  for ONEPKGFILE in $PKGFILES
  do
   echo "Copying ${ONEPKGFILE}..."
   cp -af $ONEPKGFILE /root/.packages/
  done
  if [ -f /root/.packages/${APACKAGE}.keyword ];then
   echo "Updating $KEYWORD in w.m. menus..."
   KEYWORD="`cat /root/.packages/${APACKAGE}.keyword | cut -f 1 -d " "`"
   /usr/sbin/fixmenus /root +$KEYWORD
  fi
 fi
done

echo
echo "File $CHOSENPUP is mounted on /tmp/chosenpupmntpt."
echo "The trickiest part of importing is to get all the right stuff"
echo "out of your old home directory, /tmp/chosenpupmntpt/root."
echo "This section of the script is not complete, feedback welcome."

if [ -s /root/.events ];then
 echo
 echo -n "Import Agenda event manager data? y|n: "
 read nnnn
 if [ "$nnnn" = "y" ];then
  echo "Importing /root/.events, .events.log..."
  cp -af /tmp/chosenpupmntpt/root/.events /root/
  cp -af /tmp/chosenpupmntpt/root/.events.log /root/
 fi
fi

if [ -d /root/.gaim ];then
 echo
 echo -n "Import Gaim account settings? y|n: "
 read nnnn
 if [ "$nnnn" = "y" ];then
  echo "Copying /tmp/chosenpupmntpt/root/.gaim..."
  cp -af /tmp/chosenpupmntpt/root/.gaim /root/
 fi
fi

if [ -d /root/.LinNeighborhood ];then
 echo
 echo -n "Import LinNeighborhood settings? y|n: "
 read nnnn
 if [ "$nnnn" = "y" ];then
  echo "Copying /tmp/chosenpupmntpt/root/.LinNeighborhood..."
  cp -af /tmp/chosenpupmntpt/root/.LinNeighborhood /root/
 fi
fi

if [ -d /root/.mozilla ];then
 echo
 echo "Puppy2 has Seamonkey, not Mozilla."
 echo "Anything you want to import, please use Rox to drag from"
 echo "/tmp/chosenpupmntpt/root/.mozilla to /root/.mozilla"
 echo "anything that is relevant."
 echo "This script cannot do this automatically yet."
 echo -n "Press ENTER to continue: "
 read nnnn
fi

echo
echo "Do you have any DotPups installed in"
echo "/tmp/chosenpupmntpt/root/my-roxapps?"
echo "Then open Rox and drag them across to /root/my-roxapps."
echo -n "Press ENTER to continue: "
read nnnn

echo
echo "This release of Puppy2 does not have Sylpheed email program."
echo "It is a PupGet package and you can install it."
echo "If you wish, you can copy all the settings and mail from"
echo "Puppy1. use Rox to drag /tmp/chosenpupmntpt/root/.sylpheed-2.0"
echo "and /tmp/chosenpupmntpt/root/Mail to /root."
echo -n "Press ENTER to continue: "
read nnnn

echo
echo "That's it. Anything else you can think to copy over, do so now."
echo -n "Press ENTER to unmount ${CHOSENPUP}: "
read nnnn

sync
umount /tmp/chosenpupmntpt
rmdir /tmp/chosenpupmntpt
umount $MNTPT 2>/dev/null

echo
echo -n "Press ENTER key to end script: "
read nnnn

###END###
