#!/bin/sh

# Copyright 1999 Raphaƫl Hertzog <hertzog@debian.org>
# See the README file for the license

# This script will create the Packages.gz and the Packages.cd files
# First arg = "scan" or "install"
# Second arg = directory of the CD

#set -e

BDIR=$TDIR/$CODENAME-$ARCH

PREFIX=`echo $2 | sed "s?$BDIR/CD?$BDIR/?"`

SECTIONS="main"
SECTIONSNONUS="non-US/main"
if [ "${NONFREE:-0}" != "0" -o "${EXTRANONFREE:-0}" != "0" ]; then
  SECTIONS="${SECTIONS} non-free"
  SECTIONSNONUS="${SECTIONSNONUS} non-US/non-free"
fi
if [ "${CONTRIB:-0}" != "0" ]; then
  SECTIONS="${SECTIONS} contrib"
  SECTIONSNONUS="${SECTIONSNONUS} non-US/contrib"
fi

if [ -n "$LOCAL" ]; then
  SECTIONS="$SECTIONS local"
fi



cd "$2"

if [ "$1" = "install" ]; then

# these two lines can be inserted below the "Architectures" line below,
# to sort out broken Release files just after the testing --> stable
# transition
#     sed -e "s/^Suite: .*$/Suite: stable/" | \
#     sed -e "s/^Description: .*$/Description: Debian $DEBVERSION/" | \

if [ -e "$MIRROR/dists/$CODENAME/Release" ]; then
    # Strip the MD5Sum and SHA1 fields
    # Update some other information as well
    sed -e "s/^Architectures: .*$/Architectures: $ARCH/" \
    $MIRROR/dists/$CODENAME/Release | \
    sed -e "s|^Components: .*$|Components: $SECTIONS|" | \
    perl -ne 'if (/^(MD5Sum|SHA1):/i) { $f=1; next } 
    if ($f) { 
	unless (/^ /) { print; $f=0 }
    } else { print }' > dists/$CODENAME/Release
else
    echo "ERROR: Release file ($MIRROR/dists/$CODENAME/Release) is missing !"
    exit 1
fi
if [ -n "$NONUS" ]; then
if [ -e "$NONUS/dists/$CODENAME/non-US/Release" ]; then
    # Strip the MD5Sum and SHA1 fields
    # Update some other information as well
    sed -e "/^ /d" -e "s/^Architectures: .*$/Architectures: $ARCH/" \
    $NONUS/dists/$CODENAME/non-US/Release | \
    sed -e "s|^Components: .*$|Components: $SECTIONSNONUS|" | \
    perl -ne 'if (/^(MD5Sum|SHA1):/i) { $f=1; next } 
    if ($f) { 
	unless (/^ /) { print; $f=0 }
    } else { print }' > dists/$CODENAME/non-US/Release
else
    echo -n "ERROR: Release file ($NONUS/dists/$CODENAME/non-US/Release)"
    echo " is missing !"
    exit 1
fi
fi
# Copying release files
for SECT in $SECTIONS; do
    # Install the release files
    if [ -e "$MIRROR/dists/$CODENAME/$SECT/binary-$ARCH/Release" ]; then
	cp $MIRROR/dists/$CODENAME/$SECT/binary-$ARCH/Release \
	   dists/$CODENAME/$SECT/binary-$ARCH/
    fi
    if [ -n "$NONUS" -a -e "$NONUS/dists/$CODENAME/non-US/$SECT/binary-$ARCH/Release" ]; then
	cp $NONUS/dists/$CODENAME/non-US/$SECT/binary-$ARCH/Release \
	   dists/$CODENAME/non-US/$SECT/binary-$ARCH/
    fi
done

if [ -n "$LOCALDEBS" -a -n "$LOCAL" ]; then
    if [ -e $LOCALDEBS/dists/$CODENAME/local/binary-$ARCH/Release ]; then
       cp $LOCALDEBS/dists/$CODENAME/local/binary-$ARCH/Release \
	    dists/$CODENAME/local/binary-$ARCH/
    fi
fi

if [ -e "$MIRROR/dists/$DI_CODENAME/main/debian-installer/binary-$ARCH/Release" ]; then
    cp $MIRROR/dists/$DI_CODENAME/main/debian-installer/binary-$ARCH/Release \
       dists/$CODENAME/main/debian-installer/binary-$ARCH/
fi

# Generating Packages files
apt-ftparchive generate $PREFIX.generate-binary
if [ -n "$NONUS" ]; then
    apt-ftparchive generate $PREFIX.generate-binary-non-US
fi
if [ -e "$MIRROR/dists/$DI_CODENAME/main/debian-installer" ]; then
    apt-ftparchive generate $PREFIX.generate-binary-debian-installer
fi

fi ## END "$1" = "install"

# Prepare the files needed to generate the Packages files
if [ "$1" != "scan" ]; then exit 0; fi

#
# Create the file lists and the config files for apt-ftparchive generate
#

# We have to scan all possible dists where packages can be
DISTS=""
DISTSNONUS=""
for i in `cd dists; echo *; cd ..`; do
    if [ ! -L "dists/$i" -a -d "dists/$i" ]; then
	if [ -d "dists/$i/main/binary-$ARCH" -o \
	     -d "dists/$i/non-free/binary-$ARCH" -o \
	     -d "dists/$i/contrib/binary-$ARCH" ]; then
	    DISTS="$DISTS $i"
	fi
        if [ -n "$SECURITY" ]; then
            DISTS="$DISTS $i/updates"
        fi
	if [ -d "dists/$i/non-US/main/binary-$ARCH" -o \
	     -d "dists/$i/non-US/non-free/binary-$ARCH" -o \
	     -d "dists/$i/non-US/contrib/binary-$ARCH" ]; then
	    DISTSNONUS="$DISTSNONUS $i"
	fi
    fi
done
# Creating the file lists
for SECT in $SECTIONS; do
    touch $PREFIX.filelist_$SECT
    if [ -d "pool/$SECT" ]; then
	find pool/$SECT >>$PREFIX.filelist_$SECT
    fi
    if [ -d "pool/unstable/$SECT" ]; then
	find pool/unstable/$SECT >>$PREFIX.filelist_$SECT
    fi
    if [ -n "$SECURITY" -a -d "pool/updates/$SECT" ]; then
	find pool/updates/$SECT >>$PREFIX.filelist_$SECT
    fi
    for DIST in $DISTS; do
	if [ -d "dists/$DIST/$SECT/binary-$ARCH" ]; then
	    find dists/$DIST/$SECT/binary-$ARCH >>$PREFIX.filelist_$SECT
	fi
    done
    if [ -n "$NONUS" ]; then
	touch $PREFIX.filelist_non-US_$SECT
	if [ -d "pool/non-US/$SECT" ]; then
	    find pool/non-US/$SECT >>$PREFIX.filelist_non-US_$SECT
	fi
	for DIST in $DISTSNONUS; do
	    if [ -d "dists/$DIST/non-US/$SECT/binary-$ARCH" ]; then
		find dists/$DIST/non-US/$SECT/binary-$ARCH \
		    >>$PREFIX.filelist_non-US_$SECT
	    fi
	done
    fi
done
# Creating the config files
cat >$PREFIX.generate-binary <<EOF
Dir::ArchiveDir "$2";
Dir::OverrideDir "$BDIR/indices";
Dir::CacheDir "$APTTMP/$CODENAME-$ARCH/apt-ftparchive-db";

TreeDefault::Contents " ";

Tree "dists/$CODENAME" {
    FileList "$PREFIX.filelist_\$(SECTION)";
    Sections "$SECTIONS";
    Architectures "$ARCH";
    
    BinOverride "override.$CODENAME.\$(SECTION)";
    ExtraOverride "override.$CODENAME.extra.\$(SECTION)";
    SrcOverride "override.$CODENAME.\$(SECTION).src";
}
EOF
cat >$PREFIX.generate-binary-non-US <<EOF
Dir::ArchiveDir "$2";
Dir::OverrideDir "$BDIR/indices-non-US";
Dir::CacheDir "$APTTMP/$CODENAME-$ARCH/apt-ftparchive-db";

TreeDefault::Contents " ";

Tree "dists/$CODENAME/non-US" {
    FileList "$PREFIX.filelist_non-US_\$(SECTION)";
    Sections "`echo $SECTIONSNONUS | sed -e 's#non-US/##g'`";
    Architectures "$ARCH";
    
    BinOverride "override.$CODENAME.\$(SECTION)";
    ExtraOverride "override.$CODENAME.extra.\$(SECTION)";
    SrcOverride "override.$CODENAME.\$(SECTION).src";
}
EOF
cat >$PREFIX.generate-binary-debian-installer <<EOF
Dir::ArchiveDir "$2";
Dir::OverrideDir "$BDIR/indices";
Dir::CacheDir "$APTTMP/$CODENAME-$ARCH/apt-ftparchive-db";

TreeDefault::Contents " ";

Tree "dists/$CODENAME/main" {
    FileList "$PREFIX.filelist_main";
    Sections "debian-installer";
    Architectures "$ARCH";
    
    BinOverride "override.$CODENAME.main.\$(SECTION)";
    ExtraOverride "override.$CODENAME.extra.main";
    SrcOverride "override.$CODENAME.main.src";
    Packages::Extensions ".udeb";
}
EOF
if [ -n "$LOCAL" -a -e "${LOCALDEBS:-$MIRROR}/dists/$DI_CODENAME/local/debian-installer" ]; then
cat >>$PREFIX.generate-binary-debian-installer <<EOF

Tree "dists/$CODENAME/local" {
    FileList "$PREFIX.filelist_local";
    Sections "debian-installer";
    Architectures "$ARCH";
    
    BinOverride "override.$CODENAME.local.\$(SECTION)";
    ExtraOverride "override.$CODENAME.extra.local";
    SrcOverride "override.$CODENAME.local.src";
    Packages::Extensions ".udeb";
}
EOF
fi


# Creating the indices directory
if [ ! -d "$BDIR/indices" ]; then
    mkdir $BDIR/indices
    cp $MIRROR/indices/* $BDIR/indices/
    if [ -n "$LOCALDEBS" -a -d $LOCALDEBS/indices ]; then
         cp $LOCALDEBS/indices/* $BDIR/indices/
    fi
    gunzip -f $BDIR/indices/*.gz
    for SECT in $SECTIONS; do
	touch $BDIR/indices/override.$CODENAME.$SECT
	touch $BDIR/indices/override.$CODENAME.extra.$SECT
	touch $BDIR/indices/override.$CODENAME.$SECT.src
    done
    if [ -e "$MIRROR/dists/$DI_CODENAME/main/debian-installer" ]; then
	touch $BDIR/indices/override.$CODENAME.main.debian-installer
    fi
    if [ -n "$LOCAL" -a -e "${LOCALDEBS:-$MIRROR}/dists/$DI_CODENAME/local/debian-installer" ]; then
	touch $BDIR/indices/override.$CODENAME.local.debian-installer
	touch $BDIR/indices/override.$CODENAME.extra.local
	touch $BDIR/indices/override.$CODENAME.local.src
    fi
fi
if [ -n "$NONUS" -a ! -d "$BDIR/indices-non-US" ]; then
    mkdir $BDIR/indices-non-US
    cp $NONUS/indices-non-US/* $BDIR/indices-non-US/
    gunzip -f $BDIR/indices-non-US/*.gz
    for SECT in `echo $SECTIONSNONUS | sed -e 's#non-US/##g'`; do
	touch $BDIR/indices-non-US/override.$CODENAME.$SECT
	touch $BDIR/indices-non-US/override.$CODENAME.extra.$SECT
	touch $BDIR/indices-non-US/override.$CODENAME.$SECT.src
    done
fi

exit 0