#! /bin/ksh

usage="USAGE: $(basename $0) <project> <rel-dir> <passwd>"
if test $# -lt 3
then
  print -u2 $usage
  exit 1
fi

project="${1}"
shift

cd "$1" || {
    print -u2 $usage
    exit 1
}

# The release number should be part of the current directory name.
# Strip off the leading "rel-" for "rel"
#
reldir=$(basename $(pwd))
rel=$(echo ${reldir} | sed 's/^[a-zA-Z.-]*//')
shift

passwd="${1}"
test -z "${passwd}" && {
    print -u2 "$(basename $0) error: no password"
    exit 1
}

# Make sure we have the original tarball
#
test -f ${project}-${rel}.tar.gz || {
    print -u2 $(basename $0) error: \
        cannot find project tarball ${project}-${rel}.tar.gz
    print -u2 $usage
    exit 1
}

# Remove clutter from previous attempts
#
rm -f ${project}*.sig ${project}*.asc ${project}*.md5sum* ${project}*.directive
echo project contents:
ls -l

# If there are new distributable file types, add them to the suffix list.
#
list=$(for f in *.rpm *.gz *.bz2 ; do test -f ${f} && echo ${f} ; done )
md5sum ${list} > ${project}-${rel}.md5sum
list=$(echo ${list} ${project}-${rel}.md5sum)

# Construct the directive and signature files
#
for f in ${list}
do
    # The directive puts the file into a release-named directory
    # viz., the same as our current directory.  Reduce clutter in
    # the GNU/project distribution directory.
    #
    echo "directory: ${project}/${reldir}" > ${f}.directive
    ( echo "$passwd" | gpg --passphrase-fd 0 -b ${f}
      echo "$passwd" | gpg --passphrase-fd 0 --clearsign ${f}.directive
    ) 2> /dev/null 1>&2
    rm -f ${f}.directive
done

# Disable the upload.  Enable it by simply commenting out the next line
#
cat <<true

ftp -i -n -v <<EOF
open  ftp-upload.gnu.org
user anonymous
binary
cd incoming/ftp
`for f in ${list}
do print -u2 putting ${f}
   print put ${f}
   print put ${f}.directive.asc
   print put ${f}.sig
done`
bye
EOF
true
