#!/bin/bash

package() {
    set -x
    perl -0ne 's/^Section: debathena-config/Section: debathena-manual-config/m;
         s/^Package: debathena-(.*)-config$/Package: debathena-manual-$1-config/m;
         $package=$1;
         /^Architecture: (.*)$/m;
         $arch = $1;
         $versionadd = $arch eq "all" ? "" : "~$arch";
         s/^Version: (.*)~.*$/Version: \1$versionadd/m;
         $version="$1$versionadd";
         s/$/\nConflicts: debathena-$package-config\nProvides: debathena-$package-config\nMaintainer: Debathena Project <debathena\@mit.edu>\nStandards-Version: 3.6.2\nCopyright: ..\/common\/copyright\nReadme: ..\/common\/README-manual-config.in\nDescription: Debathena manual configuration for $package\n This is a Debathena manual configuration package.  It provides an\n alternate way to satisfy any dependencies on debathena-$package-config,\n for those who prefer to do this configuration manually.\n .\n If you want $package to be configured automatically, make sure you have\n enabled the debathena-config component of the repository, and install\n the debathena-$package-config package instead.\n/;
         print "manual-$package-config debathena-manual-$package-config_$version $arch\n$_";' | \
#    cat; cat /dev/null | \
    (
	read -r dir file arch && mkdir -p "$dir" && chmod 777 "$dir" && cd "$dir" && \
	    if [ "$arch" = all ]; then carch=amd64; archflag=; else carch="$arch"; archflag="--arch=$arch"; fi
	    if ! [ -e "${file}.equivs" ]; then
	        cat > "${file}.equivs"
	        equivs-build --full $archflag "${file}.equivs"
	    fi
	    if ! [ -e "/mit/debathena/apt/pool/debathena-manual-config/d/debathena-${dir}/${file}_${arch}.deb" ]; then
		echo "${dir}/${file}_${carch}.changes" >> ../upload-queue
	    fi
    )
}
