#!/bin/sh

# This script sets up a hasher aptbox by reusing the packages cache that
# is located at /var/cache/apt/archives/. The script is in alpha state,
# use with caution. No any warranties, as usual.
#
# Author: Alexey "Ktirf" Rusakov <ktirf@users.sf.net>
#

# FIXME: Remove the necessity to run the script from a certain directory,
# make it a parameter.

cat <<EOF
The script will create everything needed to use your apt cache
as hasher aptbox. The files of the fake repository to build will be
located in the current directory. It is safe to locate the fake repository
in hasher working directory (such as $HOME/hasher or alike).

Press Enter to continue or Ctrl-C to abort the script.
EOF
read

HSH_APT_CONF='Sisyphus/apt.conf'
HSH_SRC_LIST='Sisyphus/sources.list'

# FIXME: Sisyphus/ directory name should be tuneable.
# /var/lib/apt/lists/ and /var/cache/apt/archives/ should be tuneable too.

echo 'Creating the fake repository...'
mkdir -p Sisyphus/RPMS.all

pushd /var/lib/apt/lists/ >/dev/null
APT_SRC_PREFIX=`ls -- *Sisyphus* | \
    awk -F'_' -v OFS='_' '/release$/ { NF -= 3; print $0 }' | uniq`
case `echo $APT_SRC_PREFIX | wc -w` in
    '0')
        echo Failed to find any Sisyphus repository in /var/lib/apt/lists/, aborting.
        exit 1
        ;;
    '1')
        ;; # ok
    *)
        cat <<EOF
More than one Sisyphus repository is being used by apt.
This script does not support multiple repositories. Bye-bye.
EOF
        exit 1
        ;;
esac
APT_SRC_ARCHS=`ls -- ${APT_SRC_PREFIX}* |
    awk -F'_' '/release/ { print $(NF-2) }' | uniq`
if [ `echo $APT_SRC_ARCHS | wc -w` = "0" ]; then
    echo 'There are no available architectures for Sisyphus, aborting.'
    exit 1
fi
echo "  Architectures:" $APT_SRC_ARCHS
popd >/dev/null
for arch in $APT_SRC_ARCHS; do
    echo "  Processing architecture $arch"
    mkdir -p -- "Sisyphus/$arch/base"
    ln -sf /var/lib/apt/lists/${APT_SRC_PREFIX}_${arch}_base_release \
        Sisyphus/$arch/base/release
    APT_SRC_COMPONENTS=`cd /var/lib/apt/lists/ >/dev/null 2>&1 &&
        ls -- *Sisyphus_${arch}_* | \
            awk -F'.' '/release\./ { print $NF }' | uniq`
    echo "    Components:" $APT_SRC_COMPONENTS
    for component in $APT_SRC_COMPONENTS; do
        echo "    Processing component '$component'"
        for f in release pkglist; do
#            echo "      Setting symlink for $f.$component"
            ln -sf -- /var/lib/apt/lists/${APT_SRC_PREFIX}_${arch}_base_$f.${component} \
                Sisyphus/$arch/base/$f.${component}
        done
        unset f
        if egrep "Sisyphus\W+$arch\W+$component" "$HSH_SRC_LIST" >/dev/null 2>&1; then
            :
#            echo "      The line for $arch/$component is already in $HSH_SRC_LIST"
        else
            echo "      Adding the line to $HSH_SRC_LIST"
            echo "rpm file:`pwd`/Sisyphus $arch $component" >>"$HSH_SRC_LIST"
        fi
        echo "      Linking a package directory (Sisyphus/$arch/RPMS.$component)"
        ln -sf -- `pwd`/Sisyphus/RPMS.all Sisyphus/$arch/RPMS.$component
#        echo "    '$component' is done"
    done
    unset component
    echo "  $arch is done"
done
unset arch

cat <<EOF
  Creating symlinks from the fake repository
  to the files in /var/cache/apt/archives
EOF

RPMS_ALL="`pwd`/Sisyphus/RPMS.all"
# FIXME: Instead of creating everything from scratch each time, it is
# better to check for dead symlinks and to link the files that are not yet
# linked to.
rm -f "RPMS_ALL/*"

pushd /var/cache/apt/archives/ >/dev/null
# Thanks to mike@ for the piece of code below that converts apt naming to
# rpm naming.
ls | grep "\.rpm$" | while read aptish; do
    rpmish=`rpm -qp --queryformat='%{NAME}-%{VERSION}-%{RELEASE}.%{ARCH}.rpm' "$aptish"`
    if [ "$rpmish" != "$aptish" ]; then
        echo "    Linking $rpmish"
        ln -sf "/var/cache/apt/archives/$aptish" "$RPMS_ALL/$rpmish"
    fi
done
popd >/dev/null
echo "  Done"

# TODO: use sisyphus_cleanup_dups to remove duplicates (is it really needed?)

echo "  Generating $HSH_APT_CONF"
cat >"$HSH_APT_CONF" <<EOF

Dir::Etc::SourceList "`pwd`/$HSH_SRC_LIST";

EOF
echo All done

cat <<EOF

The fake repository is ready. Use the following line to run your hasher:

hsh -v --no-contents-indices --no-cache --repackage-source \\
--apt-config=`pwd`/$HSH_APT_CONF <hasher-workdir> <src_package.rpm>

Good luck!
EOF

unset HSH_APT_CONF HSH_SRC_LIST
unset APT_SRC_PREFIX APT_SRC_ARCHS APT_SRC_COMPONENTS APT_LIST_PREFIX

