#!/bin/ksh
##
##### 
#####  Written by Paul A. Luzzi on 07-16-2005
#####
#####  Purpose : to get rpm updates to /tmp/updates.$$ folder
#####

#####  
#####  Check that input is provided
#####    and strip out any ; chars
#####
INPUT_LIST="$*"
INPUT_RPMS=`echo "$INPUT_LIST" | sed -e 's/;/\ /g'`
if [ "$#" -lt "1" ]
  then
    echo "You must ask for at least one RPM"
    echo "Exiting on error"
    exit 1
  fi

#####  
#####  Provide list back to user as confirmation
#####
echo "You requested RPMS with this substring/pattern :"
for RPM in "$INPUT_RPMS"
  do
    echo "  $RPM"
  done
echo " "

#####
#####  Setup any variables
#####
MAIN_URL="http://ftp.suse.com/pub/suse/i386/9.2/suse/i586"
OUTPUT_DIR="/tmp/downloaded_rpms_$$"

#####
#####  Search for rpms in $1 - space seperated list
#####
WHICH_RPMS=`echo "$INPUT_RPMS" | sed -e 's/\ /|/g'`

#####  
#####  Get list of targets
#####  
FULL_TARGET_OPUT=`lynx -dump "$MAIN_URL" 2&gt;&amp;1 | egrep -i "$WHICH_RPMS"`

#####  
#####  Make output directory
#####  
mkdir "$OUTPUT_DIR"
cd "$OUTPUT_DIR"
if [ ! `pwd` = "$OUTPUT_DIR" ]
  then
    echo "Problem changing dir to $OUTPUT_DIR"
    exit 2
  fi

#####
#####  Go get 'em
#####
LIST_OF_TARGETS=`echo "$FULL_TARGET_OPUT" | egrep -i "$WHICH_RPMS" | grep http | awk -F" " '{print $2}'`
## 
if [ ! -z "$LIST_OF_TARGETS" ]
  then
    for TARGET in $LIST_OF_TARGETS
      do
        echo "wget -nd $TARGET"
        wget -nd "$TARGET"
      done
    echo ""
    echo "Your downloaded rpms are in $OUTPUT_DIR"
    echo ""
   else
    echo ""
    echo "No such targets exist"
    echo ""
  fi

#####
#####  The end
#####
