#! /bin/sh

# Copyright 2004 Steve McIntyre <steve@einval.com>
# See the README file for the license

# This script is called by the Makefile to cleanup the .jigdo
# files:
#
# Make sure the iso filename is sane
# Add in the TEMPLATEURL information
# Update the Servers lines

jigdo="$1"    # .jigdo file to work on
iso="$2"      # The name to give to the .iso
dirpath="$3"  # The disc build tree
template="$4" # URL for .template file, can be relative URL
discinfo="$5" # e.g. "Debian GNU/Linux 3.0 r1 "Woody" - Unofficial i386 Binary-1"
snapurl="$6"  # URL for snapshot.d.o. e.g. http://snapshot.debian.org/archive/debian/20110630T000000Z/
# any further parameters are fallback URLs
info="Generated on "`date -R`

# Don't panic - the \047 in the awk below is the ASCII for ' - if I
# use ' instead, the enclosing shell script gets annoyed...!
cat $jigdo | awk -v ISO="$iso" -v TEMPLATE="$template" -v DISCINFO="$discinfo" -v INFO="$info" '
    /^Filename=/        {
                            printf("Filename=%s\n", ISO);
                            next
                        }
    /^Template=/        {
                            printf("Template=%s\n", TEMPLATE);
                            next
                        }
    /^Template-MD5Sum=/ {
                            printf("%s\n", $0);
                            printf("ShortInfo=\047%s\047\n", DISCINFO);
                            printf("Info=\047%s\047\n", INFO);
                            next
                        }
    /.*/                {   print $0 }
' > $jigdo.1
mv -f $jigdo.1 $jigdo

# Snapshot URL
if [ "$snapurl"x != ""x ] ; then
    echo "$snapurl" >> $jigdo
fi

# If some fallbacks were specified, output a servers section with the
# URLs. The entries of the variable are expected to be already of the
# form "Label=http://some.url/"
if test -n "$JIGDOFALLBACKURLS"; then
    for url in "$JIGDOFALLBACKURLS"
    do
        # The --try-last switch assigns a lower priority to the URL,
        # so it will only be used if other server entries (without the
        # --try-last) have already been tried without success.
        echo "$url --try-last" >> $jigdo
    done
fi

# Create a snapshot tree if we've been told where to create one
if test -n "$JIGDOFALLBACKPATH"; then
    echo "Creating snapshot tree:"
    for jentry in `cat $jigdo | grep =Debian:`
    do
        file=`echo $jentry | sed 's/^.*Debian://g'`
        dir=$JIGDOFALLBACKPATH/Debian/`dirname $file`
        if [ ! -d $dir ] ; then
            mkdir -p $dir
        fi
        ln -f $MIRROR/$file $JIGDOFALLBACKPATH/Debian/$file
    done
    for jentry in `cat $jigdo | grep =Non-US:`
    do
        file=`echo $jentry | sed 's/^.*Non-US://g'`
        dir=$JIGDOFALLBACKPATH/Non-US/`dirname $file`
        if [ ! -d $dir ] ; then
            mkdir -p $dir
        fi
        ln -f $NONUS/$file $JIGDOFALLBACKPATH/Non-US/$file
    done
fi



