#!/bin/sh

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

# This script will create the Release files
# First 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 "$1"

# 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 is now done in add_debs; we no longer use
# apt-ftparchive

# 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.$EXTRA_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.$EXTRA_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.$EXTRA_CODENAME.extra.$SECT
       touch $BDIR/indices-non-US/override.$CODENAME.$SECT.src
    done
fi

exit 0