#!/bin/sh

while [ $# -ne 0 ]
do
case "$1" in
-oz)
    RHOST=openzaurus.sf.net
    RDIR=/home/groups/o/op/openzaurus/htdocs/feeds/3.0
    INDEXER="~/ipkg-make-index ."
    ;;
-oz-test)
    RHOST=openzaurus.sf.net
    RDIR=/home/groups/o/op/openzaurus/htdocs/feeds/3.0/test
    INDEXER="~/ipkg-make-index ."
    ;;
-hh.org)
    RHOST=opie.handhelds.org
    RDIR=/home/opie/website/html/feed/ipaq
    INDEXER="/home/opie/ipkg-make-index ."
    ;;
-qpe)
    RHOST=qpe.sf.net
    RDIR=/home/groups/q/qp/qpe/htdocs/packages/ipaq/unstable
    ;;
-force)
    FORCE=1
    ;;
*)
    MKIPKSOPTS="$MKIPKSOPTS $1"
    ;;
esac
shift
done

if [ -z "$RHOST" -o -z "$RDIR" ]
then
    echo Error: RHOST and RDIR not set! Aborting.
    exit
fi 

[ -z "$INDEXER" ] && INDEXER="./mkPackages"

NEW=""
rm -f *.ipk

ssh $RHOST -C "cd $RDIR; ls *.ipk" >currentipks &
wait # for ssh
./mkipks $MKIPKSOPTS

for i in *.ipk
do
    if [ -z "$FORCE" ]
    then
        if grep -q $i currentipks
        then
	    rm $i
        else
	    NEW="$NEW $i";
        fi
    else
        NEW="$NEW $i";
    fi
done

if [ -n "$NEW" ]
then
    scp $NEW $RHOST:$RDIR
    rm $NEW
    ssh $RHOST -C "cd $RDIR; chmod g+w *.ipk; $INDEXER"
fi

rm currentipks