#!/usr/bin/env zsh

# Download all the packages needed for the XEmacs setup kits

emulate -LR zsh
progname=${0:t}

: ${ftpsite:=http://ftp.xemacs.org/pub/xemacs/packages}
: ${expsite:=http://ftp.xemacs.org/pub/xemacs/beta/experimental/packages}
default_pkgs="auctex
              bbdb
              c-support
              calc
              cc-mode
              debug
              dired
              easypg
              ecb
              ecrypto
              edebug
              ediff
              edit-utils
              efs
              eieio
              elib
              emerge
              eshell
              footnote
              fortran-modes
              fsf-compat
              gnus
              hm--html-menus
              ibuffer
              igrep
              ispell
              jde
              latin-euro-standards
              latin-unity
              locale
              lookup
              mail-lib
              mailcrypt
              mh-e
              mmm-mode
              mule-base
              net-utils
              os-utils
              pc
              pcl-cvs
              pcomplete
              perl-modes
              prog-modes
              ps-print
              psgml
              python-modes
              re-builder
              reftex
              ruby-modes
              sasl
              scheme
              semantic
              sgml
              sh-script
              sieve
              sounds-wav
              speedbar
              supercite
              texinfo
              text-modes
              textools
              time
              tramp
              vc
              vhdl
              viper
              vm
              w3
              x-symbol
              xemacs-base
              xemacs-devel
              xetla
              xslide
              xslt-process"
: ${pkglist:=$default_pkgs}

function getPackageFile {
  local file
  file=$1
  mv $file $file.old 2>/dev/null
  wget -q $expsite/$file && return 0
  [[ ! -f $file ]] && wget -q $ftpsite/$file && return 0
  return 1
}

# First get the latest package list
getPackageFile package-index.LATEST.gpg
if [[ ! -f package-index.LATEST.gpg ]]; then
  print -u2 "$progname: could not retrieve package index."
  exit 1
fi

# Find the right version of each package and fetch it
for f in ${=pkglist}; do
  grep "filename \"$f-[0-9]" package-index.LATEST.gpg | read fn pkg
  pkg=${pkg:s/\"//:s/\"//}
  getPackageFile $pkg
  [[ ! -f $pkg ]] && print -u2 "$progname: could not get package $pkg."
done

# Uncompress the compressed tar files (they will be compressed
# in the setup kit.
gunzip *.tar.gz

# Local Variables:
# mode: ksh
# sh-indentation: 2
# indent-tabs-mode: nil
# End:
