#! /bin/bash
# Adds a binary-distribution to a binary-repository.
#
# Usage: $0 repoRoot repoDir distro
#
# where:
#     repoRoot  Path to the root of the package-manager-specific repository. May
#               be absolute or relative to the current working directory. Will
#               be created if it doesn't exist. The repository-indexing utility
#               will be executed here.
#     repoDir   Path to the platform-specific binary repository relative to
#               the root of the package-manager-specific repository. Will be
#               created if it doesn't exist.
#     distro    Path to the binary-distribution. May be absolute or relative to
#               the current working directory. The filename portion must have
#               the form
#
#		    <name>-<version>[-<release>].<cpu>.[deb,rpm]
#
#		where:
#		    <name>      Package name (e.g., "udunits")
#		    <version>   Package version (e.g., "2.2.3")
#		    <release>   Optional package release number (e.g., "4").
#		                Ignored.
#		    <cpu>       Package CPU (e.g., "x86_64")

set -e  # Exit on error

repoRoot=${1:?Root of package-manager-specific binary-repository not specified}
repoDir=${2:?Platform-specific binary-repository not specified}
distro=${3:?Binary-distribution not specified}

# Verify that the path to the platform-specific binary-repository is relative.
#
if echo $repoDir | grep -q '^/'; then
    echo 1>&2 "Path of platform-specific binary repository not relative"
    exit 1;
fi

# Set the filename extension used by the binary-distribution, which also 
# identifies the type of binary-distribution.
#
if echo $distro | grep -q '\.deb$'; then
    ext=deb
elif echo $distro | grep -q '\.rpm$'; then
    ext=rpm
else
    echo 1>&2 "Unrecognized filename extension: \"$distro\""
    exit 1
fi

# Ensure that all input paths are absolute,
#
echo $repoRoot | grep -qv '^/' && repoRoot=`pwd`/$repoRoot
echo $repoDir | grep -qv '^/' && repoDir=$repoRoot/$repoDir
echo $distro | grep -qv '^/' && distro=`pwd`/$distro

# Vet and parse the filename of the input binary-distribution.
#
distroName=`basename "$distro"`
if ! echo $distroName | egrep -q '[^-]+-[0-9.]+(-[0-9]+)?\.[^.]+\.'$ext; then
    echo 1>&2 "Invalid binary-distribution name: \"$distroName\""
    exit 1
fi
set `echo "$distroName" | 
        sed -r 's/([^-]+-[0-9.]+)(-[0-9]+)?\.([^.]+)\.'$ext'/\1 \3/'`
pkgId=$1
pkgCpu=$2

# Ensure that the platform-specific binary-repository directory exists and go
# to it.
#
mkdir -p $repoDir
cd $repoDir

# Set the glob pattern for all releases of the same version of the same package
# in the platform-specific binary-repository directory:
#
releaseGlob=$pkgId-*.$pkgCpu.$ext 

# Determine what the next release number should be.
#
if prevRelease=`ls $releaseGlob 2>/dev/null | 
        sed -r "s/$pkgId-([0-9]*)\.$pkgCpu\.$ext/\1/"`; then
    pkgRelease=$(($prevRelease+1))
else
    pkgRelease=1
fi

# Copy the input binary-distribution to the output binary-distribution.
#
outDistro=$pkgId-$pkgRelease.$pkgCpu.$ext
cp $distro $outDistro

# Delete all previous releases of the same version of the same package.
#
rm -f `ls $releaseGlob | fgrep -v $outDistro`

# Go to the root-directory of the package-manager-specific binary-repository and
# update the platform-specific binary-repository.
#
cd $repoRoot
case "$ext" in
    (deb) flock $repoDir dpkg-scanpackages $repoDir /dev/null | \
            gzip -9c >$repoDir/Packages.gz;;
    (rpm) flock $repoDir createrepo $repoDir;;
esac