#!/bin/sh
################################################
#
# This script generates an "apt-get"-able package.
# The package is empty and has a dependency for
# everything that's on the machine.
#
################################################

ARCH=${HOSTTYPE}
BOX=`hostname --fqdn`
VER=`date +%Y%m%d`
HUMANDATE=`date +%m/%d/%Y`
PKGNAME=`echo jablicate-${BOX} | sed 's/\\./-/g'`
PKGDIR=${PKGNAME}-${VER}
LETTER=`echo ${PKGNAME} | cut -b-1`
MAINTAINER="Collection Maintainer <${USER}@`dnsdomainname`>"
DISTRO=debian-custom
DEB=${PKGNAME}_${VER}_all.deb
DISTDIR=dists/${DISTRO}/contrib/binary-${ARCH}
POOLDIR=pool/contrib/${LETTER}/${PKGNAME}
SRCDIR=dists/${DISTRO}/contrib/source

parse_command_line() {
    if [ "$1" = "--versions" ]
    then
	VERSIONS="TRUE"
    elif [ "$1" ]
    then
	echo "Usage: jablicator [--versions]"
	exit 1
    fi
}

sources ()
{
    cat /etc/apt/sources.list | sed 's/\#.*$//g' | grep deb
}

# It has been suggested that dpkg --getselections
# may be better than grepping through files with
# grep-dctrl. I haven't yet investigated this.

install_list ()
{
    grep-dctrl -v --field Priority required  --exact-match \
	/var/lib/dpkg/status |\
	grep-dctrl    --field Status "install ok installed" --exact-match |\
	grep-dctrl -v --field Package xfonts \
	--no-field-names --show-field $1
}

available_list () 
{
    grep-dctrl --no-field-names --show-field Package -r . \
	/var/lib/apt/lists/*Packages | sort -u
}

monster_list () 
{
    if [ "$VERSIONS" ]
    then
	install_list "Package,Version" |\
	    awk '/^[ \t]*$/ { next } { printf "%s (= ",$0 ; getline ; printf "%s)\n",$0 }'
    else
	install_list "Package"
    fi
    available_list
}

list () 
{
    if [ "$VERSIONS" ]
    then
        # sed : you need a blank after the package name to get \"sort -r" working
        # sort -r : output the lines with versions before lines wit\hout version
        # uniq --check-fields=1 : check only the package name
	monster_list |\
	    sed -e 's/$/ /g' | sort -r | uniq --repeated --check-fields=1 |\
	    grep -v ${PKGNAME} | sort | sed 's/ $/,/g'
    else
	monster_list | sort | uniq --repeated | grep -v ${PKGNAME} | sed 's/$/,/g'
    fi
}

control () 
{
    PACKAGES=`list`
    echo Source: ${PKGNAME}
    echo Section: admin
    echo Priority: optional
    echo "Maintainer: ${MAINTAINER}"
    echo "Build-Depends-Indep: debhelper (>> 3.0.0)"
    echo Standards-Version: 3.5.2
    echo
    echo Package: ${PKGNAME}
    echo Architecture: all
    echo Depends: ${PACKAGES}
    echo Description: Replicate software collection on ${BOX}
    echo " Replicate the software collection from ${BOX}"
    echo " A software collection is simply the group of packages"
    echo " installed on a particular machine."
    echo " ."
    echo " This package contains dependencies that may be "
    echo " fulfilled from the following sources. Check to see if "
    echo " they are present in your /etc/apt/sources.list."
    echo " ."
    sources | sed 's/^/ /g'
}

changelog ()
{
    DATE=`date -R`
    echo "${PKGNAME} (${VER}) unstable; urgency=low"
    echo
    echo "  * This package was generated by a computer progam which"
    echo "    does not keep track of historical data."
    echo 
    echo " -- ${MAINTAINER}  ${DATE}"
}

copyright ()
{
    echo This package contains a list of the debian software that 
    echo was on ${BOX} on ${DATE}. 
    echo
    echo You are welcome to call an army of lawyers and philosphers 
    echo to decide on the exact copyright status and/or ownership
    echo of this package. If the software included is all from the
    echo Debian universe, then it should probably be as legally squeaky 
    echo clean to use as any other Debian subdistribution.
}

rules () 
{
    echo "#!/usr/bin/make -f"
    echo ""
    echo "export DH_COMPAT=3"
    echo ""
    echo "configure: configure-stamp"
    echo "configure-stamp:"
    echo "	dh_testdir"
    echo "	touch configure-stamp"
    echo ""
    echo "build: configure-stamp build-stamp"
    echo "build-stamp:"
    echo "	dh_testdir"
    echo "	touch build-stamp"
    echo ""
    echo "clean:"
    echo "	dh_testdir"
    echo "	dh_testroot"
    echo "	rm -f build-stamp configure-stamp"
    echo "	dh_clean"
    echo ""
    echo "install: build"
    echo "	dh_testdir"
    echo "	dh_testroot"
    echo "	dh_clean -k"
    echo "	dh_installdirs"
    echo ""
    echo "binary-indep: build install"
    echo "	dh_testdir -i"
    echo "	dh_testroot -i"
    echo "	dh_installdocs -i"
    echo "	dh_installchangelogs -i"
    echo "	dh_link -i"
    echo "	dh_compress -i"
    echo "	dh_fixperms -i"
    echo "	dh_installdeb -i"
    echo "	dh_shlibdeps -i"
    echo "	dh_gencontrol -i"
    echo "	dh_md5sums -i"
    echo "	dh_builddeb -i"
    echo ""
    echo "binary: binary-indep"
    echo ".PHONY: build clean binary-indep binary install configure"
    echo ""
}

serve ()
{
    mkdir -p ${DISTDIR}
    mkdir -p ${SRCDIR}
    mkdir -p ${POOLDIR}
    mv ${DEB} ${POOLDIR}
    mv ${PKGNAME}_${VER}.dsc ${POOLDIR}
    mv ${PKGNAME}_${VER}.tar.gz ${POOLDIR}
    mv ${PKGNAME}_${VER}_${ARCH}.changes ${POOLDIR}
    dpkg-scanpackages pool /dev/null | gzip -9c > ${DISTDIR}/Packages.gz
    dpkg-scansources  pool /dev/null | gzip -9c > ${SRCDIR}/Sources.gz 
}

instructions () 
{
    echo
    echo ========================================================
    echo
    echo "First, read the man page for security implications."
    echo "If you wish to share your softwre collection, you may"
    echo "copy the newly created dists and pool directories to a"
    echo "web accessable location. Others may then replicate your software"
    echo "collection by adding the following line to their config file"
    echo "/etc/apt/sources.list"
    echo
    echo "deb http://<WEB LOCATION> ${DISTRO} contrib"
    echo
    echo "Then it's just a matter of "
    echo "apt-get update && apt-get install ${PKGNAME}"
    echo
}

clean () 
{
    rm -rf ${PKGDIR}
}




# Prepare the subdistribution
parse_command_line $@
mkdir -p ${PKGDIR}/debian
control > ${PKGDIR}/debian/control
changelog > ${PKGDIR}/debian/changelog
copyright > ${PKGDIR}/debian/copyright
rules > ${PKGDIR}/debian/rules

# Build the package
cd ${PKGDIR}
debuild -us -uc
if [ "$?" != 0 ]
then
    echo "Aborting due to error."
    exit
fi
cd -

# Make package "apt-get"-able
serve
clean
instructions
