#!/bin/sh

#    plucker-prc-install: fetch and install plucker viewer .prc on the Palm
#    Copyright (c) 2006, Ludovic Rousseau <ludovic.rousseau@free.fr>
#    Copyright (c) 2005, Alex Malinovich <demonbane@the-love-shack.net>
#    Based on plucker-prc-install v1.1 by Ludovic Rousseau <ludovic.rousseau@free.fr>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

set -e

NONEN=_translations
VERS=1.8

#NOTE: FILE is made of $FILE_PREFIX + $LORES + $NONEN + $FILE_SUFFIX
FILE_PREFIX=plucker_viewer
FILE_SUFFIX=-$VERS.tar.bz2

#directory within tar file containing necessary files
TAR_DIR=plucker-$VERS

URL=http://downloads.plkr.org/$VERS
TMP=/tmp/plucker-prc-install.$$
PRCS="PluckerUserGuide.pdb SysZLib.prc"

if [ "x$PILOTPORT" = "x" ]
then
	if [ -e /dev/pilot ]
	then
		PILOTPORT=/dev/pilot
	else
		PILOTPORT=usb:
	fi
fi

bad=0
if [ ! -x /usr/bin/wget ]
then
    echo "Error: You must install wget (wget package) to use this script!"
    bad=1
fi

if [ -x /usr/bin/gpilot-install-file ]
then
    SYNC_CMD=gpilot-install-file
elif [ -x /usr/bin/pilot-xfer ]
then
    SYNC_CMD="pilot-xfer -i -p $PILOTPORT"
else
    echo "Error: You must install pilot-xfer (pilot-link package) to use this script!"
    bad=1
fi

if [ $bad -ne 0 ]
then
    exit
fi

# remove temporary files in case of premature exit (Ctrl-C)
clean_exit()
{
    echo
    rm -rf $TMP
}

trap clean_exit INT

case "$1" in
    "-v"|"--version")
	echo "plucker-prc-install: fetch and install plucker viewer .prc on the Palm"
	echo "Version 1.3, September 2006"
	echo "Copyright (c) 2006, Ludovic Rousseau <ludovic.rousseau@free.fr>"
	echo "Copyright (c) 2005, Alex Malinovich <demonbane@the-love-shack.net>"
	echo "Based on plucker-prc-install v1.1 by Ludovic Rousseau <ludovic.rousseau@free.fr>"
	echo "This program is protected by the GNU GPL version 2 of later"
	exit
	;;

    "--nohires")
	LORES=_nonhires
	;;

    ?*)
	echo "Usage: $0 [--nohires] [-h|--help] [-v] [--version]"
	echo " --nohires: install non-hires version"
	echo "           instead of default hires version"
	exit
	;;
esac

echo "Will synchronize using port: $PILOTPORT"

PRC_LANG=$(echo $LANG | cut -c 1-2)

mkdir $TMP
cd $TMP

FILE=$FILE_PREFIX$LORES$FILE_SUFFIX
echo -n "Downloading $URL/$FILE ... "
wget -q $URL/$FILE
echo "done!"
tar xjf $FILE

if [ $PRC_LANG != "en" ]
then
    FILE=$FILE_PREFIX$LORES$NONEN$FILE_SUFFIX
    echo -n "Downloading $URL/$FILE ... "
    wget -q $URL/$FILE
    echo "done!"
    tar xjf $FILE
fi

cd $TAR_DIR

if [ ! -f viewer_$PRC_LANG.prc ]
then
    echo "Viewer for language $PRC_LANG does not exist, using English (en) instead"
    PRC_LANG=en
else
    echo "Using viewer language: $PRC_LANG"
fi

PRCS="$PRCS viewer_$PRC_LANG.prc"

echo "Will install: " $PRCS

$SYNC_CMD $PRCS

rm -rf $TMP
