#!/bin/sh

# define target arch and URL
ARCH=i386
#ARCH=x86_64
URL_BASE=ftp://updates.vinelinux.org/pub/Vine/
#URL_BASE=ftp://ftp.jaist.ac.jp/pub/Linux/Vine/
USE_MELD=Yes

# define URLs and files
URL_MAIN=$URL_BASE/Vine-5.0/$ARCH/Vine/RPMS.main/
URL_PLUS=$URL_BASE/VinePlus/5/$ARCH/

CAT_FILE=packages-vine5x.list
URL_CAT=$URL_BASE/VineSeed/category/$CAT_FILE

# define temporary directory/files
TMP_DIR=/var/tmp
TMP_CAT=$TMP_DIR/5category.list
TMP_FTP=$TMP_DIR/5ftpfiles.list
TMP_PKG=$TMP_DIR/5ftp_pkgs.list

# column width for diff output
COLS=80

#####

# clrear temp files
rm -rf $TMP_CAT $TMP_FTP

# get packages-vine5
wget $URL_CAT -O $TMP_DIR/$CAT_FILE

# get actual package list by lftp
lftp -c find $URL_MAIN > $TMP_PKG
lftp -c find $URL_PLUS | grep -v /pool/ >> $TMP_PKG

# sort packages-vine5.list by package name
sort -t "," -k2 $TMP_DIR/$CAT_FILE | sed 's/,,/,/' > $TMP_CAT

# create package list for actual files on ftp 
for f in `grep -e ".rpm$" $TMP_PKG`; do
    echo -n `echo $f | cut -d "/" -f9 | cut -d "." -f2` >> $TMP_FTP
    echo -n "," >> $TMP_FTP
    echo -n `basename $f | awk -F "-" '{S="-" $(NF-1) "-" $NF; sub(S,""); print $0}'` >> $TMP_FTP
    echo "," >> $TMP_FTP
done

sort -t "," -k2 $TMP_FTP > $TMP_DIR/tmp.list
mv $TMP_DIR/tmp.list $TMP_FTP

# compare
if [ $USE_MELD == Yes ]; then 
    meld $TMP_CAT $TMP_FTP &
else
    diff $TMP_CAT $TMP_FTP --side-by-side --suppress-common-lines --width=$COLS
fi
