pool-addcd-files

# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this file, if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

usage_message () {
    echo "Usage: $0 cd-directory [mirror-directory]"
}

pushd () {
    OLDCURDIR="$(pwd)"
    cd "$1"
}

popd () {
    cd "$OLDCURDIR"
}

# Expand the given CD directory to the full canonical path
CDDIR=$(readlink --no-newline --canonicalize-existing "$1")

if [ -z "$CDDIR" ]; then
    usage_message
    echo "Unknown cd directory"
    exit 1
fi

if [ -z "$2" ]; then
    MIRRORDIR=/srv/debian
else
    MIRRORDIR=$(readlink --no-newline --canonicalize-existing "$2")
fi

if [ -z "$MIRRORDIR" ]; then
    usage_message
    echo "Unknown mirror directory"
    exit 1
fi

# echo $MIRRORDIR

VERBOSE=FALSE

if [ "$3" = "--verbose" ]; then
    VERBOSE=TRUE
fi

export VERBOSE

echo "        Adding $CDDIR"
pushd $CDDIR || exit 1

find pool -type f -print0 | xargs -0 --replace bash -c "export DEBFILE={}; if [ x$VERBOSE = xTRUE ]; then echo deb=\$DEBFILE; fi; export DEBDIR=\$(dirname \$DEBFILE); mkdir -p $MIRRORDIR/\$DEBDIR ; rm -f $MIRRORDIR/\$DEBFILE; ln -s $CDDIR/\$DEBFILE $MIRRORDIR/\$DEBFILE" || exit 1

popd || exit 1