#!/bin/bash
#
# jigit
#
# (c) 2004-2007 Steve McIntyre
#
# Wrapper for jigdo to make updating CDs easier
#
# GPL v2 - see COPYING

# Some definitions
APT_CACHE=/var/cache/apt/archives
WGET_OPTS="-q --passive-ftp --no-directories --no-verbose"
HOST="http://people.debian.org/~93sam/"

if [ -e /etc/jigit.conf ] ; then
    . /etc/jigit.conf
fi
if [ -e ~/.jigit.conf ] ; then
    . ~/.jigit.conf
fi

URL="$HOST/jigit"

if [ "$TMPDIR"x = ""x ] ; then
    TMPDIR=/tmp
fi

CDNAME=$1
case $CDNAME in
    --help|-h)
        echo "$0:"
        echo "Automatic downloader for Jigdo images"
        echo "Usage:"
        echo "   jigit [distribution]"
        exit 1
    ;;
esac

for DIR in files jigdo
do
    if [ ! -d $TMPDIR/jigit/$DIR ] ; then
        mkdir -p -m 700 $TMPDIR/jigit/$DIR
    fi
done

cd $TMPDIR/jigit/jigdo
# Grab the latest config file
echo "Downloading config:   $URL/$CDNAME.conf"
wget $WGET_OPTS --mirror $URL/$CDNAME.conf

. $CDNAME.conf
echo "Downloading jigdo:    $URL/$JIGDO"
wget $WGET_OPTS --mirror $URL/$JIGDO
echo "Downloading template: $URL/$TEMPLATE"
wget $WGET_OPTS --mirror $URL/$TEMPLATE

echo "If you have a mirror, or any previous CD or CD image(s) available,"
echo "where are they mounted?"
echo "Say \"none\" if you have none; separate multiple entries with spaces"
read -p "> [none] " CD
if [ "$CD"x = ""x ] ; then
    CD="none"
fi

cd $TMPDIR/jigit/files
# If we have a CD or iso, add it to the find list
if [ "$CD" != "none" ] ; then
    FIND="$CD"
fi

# Now the local apt cache and our own temp directory
FIND="$FIND $APT_CACHE $TMPDIR/jigit/files"

> $TMPDIR/jigit/files/md5-list

for LOC in $FIND
do
    echo
    echo "Checking MD5 sums of files in $LOC:"
    find $LOC -type f | \
        xargs jigsum >> $TMPDIR/jigit/files/md5-list
done
echo
echo

rm -f $TMPDIR/jigit/jigdo/missing-list

# Check if we have all the pieces we need
mkimage -f $TMPDIR/jigit/files/md5-list \
    -t $TMPDIR/jigit/jigdo/$TEMPLATE \
    -j $TMPDIR/jigit/jigdo/$JIGDO \
    -M $TMPDIR/jigit/jigdo/missing-list

# If we have a missing list, we're missing some files. Go and get them
if [ -e $TMPDIR/jigit/jigdo/missing-list ] ; then
    NUM=`wc -l $TMPDIR/jigit/jigdo/missing-list | awk '{print $1}'`
    echo "Need to download $NUM files to complete the image"
    while [ $NUM -gt 0 ]
    do
        cd $TMPDIR/jigit/files
        for file in `cat $TMPDIR/jigit/jigdo/missing-list`
        do
            printf "\r%5d files missing; retrieving %-45.45s" $NUM `basename $file`
            mkdir -p `dirname $file`
            wget $WGET_OPTS --mirror $URL/$SNAPSHOT/$file -O $file
            SIZE=`stat -c %s $file`
            if [ $SIZE -gt 0 ] ; then
                NUM=$(($NUM - 1))
            else
                echo
                echo "Unable to download file $URL/$SNAPSHOT/$file to $file. Abort"
                exit 1
            fi
            jigsum $file >> $TMPDIR/jigit/files/md5-list 2>/dev/null
        done
    done

    rm -f $TMPDIR/jigit/jigdo/missing-list
	# Check (again) if we have all the pieces we need
    mkimage -f $TMPDIR/jigit/files/md5-list \
        -t $TMPDIR/jigit/jigdo/$TEMPLATE \
        -j $TMPDIR/jigit/jigdo/$JIGDO \
        -M $TMPDIR/jigit/jigdo/missing-list

	# If we still have a missing list, something is wrong. Give up
    if [ -e $TMPDIR/jigit/jigdo/missing-list ] ; then
        echo "Failed to build image; could not find/download all the files. ABORT"
        exit 1
    fi
fi

printf "\r%5d files missing; all needed files available                       " 0
echo
echo
# We should have all the bits; build the image
mkimage -v -f $TMPDIR/jigit/files/md5-list \
    -t $TMPDIR/jigit/jigdo/$TEMPLATE \
    -j $TMPDIR/jigit/jigdo/$JIGDO \
    -o $TMPDIR/jigit/jigdo/$CDNAME.iso \

if [ $? -eq 0 ] ; then
    echo "Image created successfully in $TMPDIR/jigit/jigdo/$CDNAME.iso"
else
    echo "Error creating image: $?"
fi
