#!/bin/sh

# Copyright 1999 Raphal 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/?"`

if [ -n "$NONFREE" -o -n "$EXTRANONFREE" ]; then
  SECTIONS="main contrib non-free"
  SECTIONSNONUS="non-US/main non-US/contrib non-US/non-free"
else
  SECTIONS="main contrib"
  SECTIONSNONUS="non-US/main non-US/contrib"
fi

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

cd "$2"

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

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" ]; 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

# Generating Packages files
apt-ftparchive generate $PREFIX.generate-binary
if [ -n "$NONUS" ]; then
    apt-ftparchive generate $PREFIX.generate-binary-non-US
fi


fi

# 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 [ -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
    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
# 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
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
