#!/bin/bash
################################################################
# zsavedebz by Linux Eventually <oshazard@gmail.com> ver 0.9 ###
################################################################
# Check for X
if [ -z "$DISPLAY" ];
then echo "FATAL: X not detected"; exit 0
fi

# Check for zenity
ifzen=`which zenity`
if [ -z "$ifzen" ];
then 
echo "Zenity not installed or not in PATH"
echo "Please use savedebz instead"
exit 0
fi

# SD card install location
unset sdhc
unset partition
plist=`df -h | awk '{ print $1,$6 }' | grep / | awk '{ print $1 }'`; mountp=`echo $plist | sed 's/ / FALSE /g' | sed 's/^/FALSE /g'`; partition=$(zenity --list --radiolist --text="Which partition on the SD card?" --column="Choose" --column="Partitions" $mountp); 
sdhc=`df -h | grep $partition | awk '{ print $6}'`;

if [ -z "$partition" ];
then sdhc=`df -h | grep /dev/mmcblk1p1 | awk '{ print $6}'`;
fi

zenity --info --text="Mountpoint: $sdhc"

sleep 2

# Check for apt-get
ifapt=`which apt-get`
if [ -z "$ifapt" ];
then 
echo "FATAL: apt-get not installed"
echo "Utility is for Debian-based systems"
exit 0
fi

# Install packages back to Nokia Internet Tablet
if [ "$1" = "install" ];
then confirminstall=$(zenity --question --text="Install packages from SD card to NXx0?" --ok-label=yes --cancel-label=no; echo $?)
  if [ "$confirminstall" = "0" ]; then
  echo "Installing ... (this will take awhile)"
  debzinstall() { alldebz=`cat $sdhc/installdebz`; apt-get -o 
dir::cache=$sdhc/debz install $alldebz; }
  debzinstall
  zenity --info --text="debz installation complete"
  exit 0
    else
    echo "Installation not selected. Exiting."; exit 0
  fi
fi

# If invalid parameter then quit
if [ ! -z "$1" ];
then echo "ERROR Invalid parameter"
echo "Valid options are none or install"
exit 0
fi

# Ask user to execute script
if [ -z "$1" ]; then
confirmbackup=$(zenity --question --text="Download packages to SD card?" --ok-label=yes --cancel-label=no; echo $?)
  if [ "$confirmbackup" = "0" ]; then
  echo "Installing ... (this will take awhile)"
    else
    echo "Download not selected. Exiting."; exit 0
  fi
fi

# Create directory on SD card
if [ ! -d "$sdhc/debz/archives" ];
then
mkdir $sdhc/debz{,/archives}
cp -r /var/cache/apt/archives/partial/ $sdhc/debz/archives/
fi

# Save list of installed packages
dpkg -l | grep ii | awk '{ print $2 }' > $sdhc/alldebz; 

# Save list of packages on SD
ls $sdhc/debz/archives/ | awk 'BEGIN{FS="_"};{print$1}' > $sdhc/debzsd;

# Compare files (to save bandwidth)
cd $sdhc 
###########
## pdiff ##
###########
perl <<'EOF'
$output = newdebz;
chomp($output);
open a, "<alldebz";
open b, "<debzsd";
local $/;
my @a = split /\n/, <a>;
my @b = split /\n/, <b>;
my %b = map { $_ => 1 } @b; # Make hash of B
my @res = grep { !defined $b{$_} } @a; # Everything in A not in B
open myoutput, ">$output";
select myoutput;
print join "\n", @res;
print "\n";
EOF
###########

# Convert line breaks to spaces
foobar=`cat $sdhc/newdebz`; echo $foobar > $sdhc/installdebz
pkgz=`cat $sdhc/installdebz`

# Re-download packages and save them to SD card
apt-get -o dir::cache=$sdhc/debz --reinstall -d install $pkgz

zenity --info --text="Finished."
