#!/bin/bash
# The script is borrowed from 
#============================================================================
# http://staff.washington.edu/corey/tools.html
# Author: Corey Satten, corey @ cac.washington.edu, 06/26/03, release 1.8
#============================================================================
# Modified by Steven Shiau <steven _at_ nchc org tw> 12/12/04
# We use this one to find the last version rpms in our apt repository.
# Because we might have a lot of drbl-setup rpms...
# drbl-script-1.0-45drbl.i386.rpm
# drbl-script-1.1-19drbl.i386.rpm
# drbl-script-1.1-1drbl.i386.rpm    
# drbl-setup-1.2-7drbl.i386.rpm
# we just want to list the last one drbl-setup-1.2-7drbl.i386.rpm
# 2005/8/22 Steven Shiau modified to add fbasename and  sed -n '/\.rpm$/s/^[-l].* //p' since there are some ftp mirror site can NOT handle symbolic link well (.i.e. dir -L will fail). One of the site is
# ftp://mirrors.mathematik.uni-bielefeld.de/pub/linux/suse/apt/SuSE/9.3-i386/RPMS.base/
#============================================================================
# load setting
. /opt/drbl/sbin/drbl-conf-functions
# we need to set this since fbasename is in /opt/drbl/bin
PATH=$PATH:/opt/drbl/bin

usage () {
  echo "Usage: `basename $0` [path]"
  echo "Example:"
  echo "`basename $0` /mirror/apt/RPMS.drbl-test"
  echo "`basename $0` http://free.nchc.org.tw/fedora/apt/fedora/linux/2/i386/RPMS.drbl"
}

# Try to help people figure out if/when this file has DOS line terminators
# Chances are the initial #!/bin/sh will fail with a cryptic "not found" msg
# If someone tries to debug it by feeding it to sh manually, this should help:
NL=
case "$NL" in '');; *)                                                    #
    echo "ERROR: $0 was improperly saved with CR-LF newlines." 1>&2       #
    echo "Please try again after restoring Unix-style (LF) newlines" 1>&2 #
    exit 1 ;;                                                             #
esac                                                                      #

SRC=$1
[ -z "$SRC" ] && echo "You must provide the URL or PATH!" && usage && exit 1

case "$SRC" in */);; *) SRC=$SRC/;; esac	# URL must end in slash

#-------------------------------------------------------------------------------
#
# Find all redhat patches available
#
# lynx -dump $SRC | awk '$4 == "rpm" {print $5}' | sed 's/^\[[0-9]*\]//'

TMP=/tmp/ftp$RANDOM$$
trap "rm -f $TMP" 0 1 2 13 15

if [ -d $SRC ] ;then	# local directory
    (cd $SRC && ls -l)
else			# ftp directory
    case "$SRC" in
	ftp://*)
	    PATH=/usr/bin:$PATH		# aim for /usr/bin/ftp
	    HST=`echo "$SRC" | sed "s,^ftp://,,; s,/.*,,"`
	    DIR=`echo "$SRC" | sed "s,^ftp://$HST/,,"`
	    rm -f $TMP
	    ( echo user ftp $USER@
	      echo cd $DIR
	      echo $PASSIVE
	      # Steven Shiau modified to use lftp.
	      #echo dir -L) | ftp -p -n $HST | grep '\.rpm$' > $TMP
	      echo dir -L) | lftp $HST 2>/dev/null | grep '\.rpm$' > $TMP
	    # wouldn't need TMP if ftp returned an error code on failure
	    # Steven Shiau commented this.
	    #if [ ! -s $TMP ] ;then
	    #  echo "`basename $0`: ftp failed on $SRC" 1>&2
	    #  echo "	Try the -p flag and/or use a URL to a mirror site" 1>&2
	    #  exit 1
	    #fi
	    cat $TMP 
            [ -f "$TMP" ] && rm -f $TMP ;;
	http://*)
	    lynx -dump $SRC | sed -n 's,^[ 0-9.]*http://.*/,-& ,p';;
    esac
    # In case dir -L fails, we need to add ^[-l] to strip the junk msg.
fi | sed -n '/\.rpm$/s/^[-l].* //p' | fbasename | sort
