#!/bin/csh -f

# This is a little smarter than the straight "purge"

set PILOTDIR = ~/.pilot
set DEVICE = /dev/pilot

echo "Backing up databases..."

if !( -e $PILOTDIR ) then
  echo "Creating backup directory: $PILOTDIR"
  mkdir $PILOTDIR
endif

if !( -e $PILOTDIR/archive ) then
  echo "Creating archive directory..."
  mkdir $PILOTDIR/archive
  chdir $PILOTDIR/archive
  echo "Creating archive databases..."
  pui-makenew
endif

set d = `date +"%Y-%m-%d_%H:%M"`
echo "Creating backup: $PILOTDIR/$d"
mkdir $PILOTDIR/$d
chdir $PILOTDIR/$d

echo
echo "Initiate pilot transfer to back up pilot:"
pui-xfer $DEVICE -b .
echo "Tagging completed todos for archival..."
pui-archivetodos ToDoDB.pui
echo "Cleaning and archiving databases..."
foreach db (`/bin/ls *.pui`)
  pui-clean $db $PILOTDIR/archive/$db
end
echo
echo "Initiate pilot transfer to restore cleaned databases to pilot:"
pui-xfer $DEVICE -r .

exit 0
