#! /bin/sh

# Hacked by bv@bnl.gov from the originals at
# Hacked by csieh@fnal.gov from the originals at
# ftp://apt-rpm.tuxfamily.org/pub/ftp.tuxfamily.org/apt-rpm/scripts/
# to be more generic.

# Absolute directory containing the top of the apt repository 
APTDIR=$4
# Relative directory from the top of the apt repository to the top of the mirror
#MIRRORDIR=../mirror
MIRRORDIR=.

# Extra PATH for genbasedir/genpkglist etc
#export PATH=/data/www/org/y/l/tuxfamily.org/m/p/apt-rpm/htdocs/scripts:$PATH
export PATH=$APTDIR/scripts:$PATH

# The idea of full/all/quick is:
#	* the first time you create the apt repository, choose "full".
#	* each night (when updates are downloaded), relaunch with "quick"
#	* if extra packages are put in 'extra' repository, relaunch with "quick"
#	* if the main os packages change (should not), use "all"

if [ $# -ne 4 -o \( "$1" != "full" -a "$1" != "all" -a "$1" != "quick" \) ]; then
	echo "Usage: $0 <mode> <version-string> <default> <topdir>"
	echo "  <mode> is one of:"
	echo "        \"full\": full apt repository creation"
	echo "        \"all\": no apt repository creation, but full indexes regeneration"
	echo "        \"quick\": no apt repository creation, only updates indexes regeneration"
	echo "  <version> gives the version in canonical form"
	echo "	      (eg: \"lts30rolling\", \"hepl30rolling\")"
	exit 1
fi

MODE=$1
VERSION=$2
DEFAULT=$3

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $APTDIR/${VERSION}/i386/apt-rpm/base || exit 1

	# Create the main release file
	cat >$APTDIR/${VERSION}/i386/apt-rpm/base/release <<EOF
Origin: linux.fnal.gov
Label: ${VERSION}
Suite: ${VERSION}
Architectures: i386
Components: os updates contrib 
Description: apt mirror of ${VERSION} + updates
EOF

fi

###########################################################################
#### REDHAT I386 MAIN OS
###########################################################################

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $APTDIR/${VERSION}/i386/apt-rpm/base || exit 1

	# Create the release files
	cat >$APTDIR/${VERSION}/i386/apt-rpm/base/release.os <<EOF
Archive: stable
Component: os
Version: ${VERSION}
Origin: linux.fnal.gov
Label: ${VERSION}
Architecture: i386
EOF

	# Put the symlinks for the base OS packages
	cd $APTDIR/${VERSION}/i386/apt-rpm
	rm -f SRPMS.os
	ln -sf ../../SRPMS/RedHat/original SRPMS.os
	cd $APTDIR/${VERSION}/i386/apt-rpm
	rm -f RPMS.os
	ln -sf ../$DEFAULT/RPMS RPMS.os
fi

if [ "$MODE" = "full" -o "$MODE" = "all" ]; then
	# Generate APT indexes
	genbasedir --flat --bloat $APTDIR/${VERSION}/i386/apt-rpm os
fi

###########################################################################
#### REDHAT  updates
###########################################################################

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $APTDIR/${VERSION}/i386/apt-rpm/base || exit 1
#	mkdir -p $APTDIR/${VERSION}/i386/apt-rpm/RPMS.updates || exit 1

	# Create the release files
	cat >$APTDIR/${VERSION}/i386/apt-rpm/base/release.updates <<EOF
Archive: stable
Component: updates
Version: ${VERSION}
Origin: linux.fnal.gov 

Label: ${VERSION}
Architecture: i386
EOF

	cd $APTDIR/${VERSION}/i386/apt-rpm
	rm -f SRPMS.updates
	ln -sf ../../SRPMS/RedHat/errata SRPMS.updates
	ln -sf ../errata/$DEFAULT/RPMS RPMS.updates
fi

# Remove old symlinks
#rm -f $APTDIR/${VERSION}/i386/apt-rpm/RPMS.updates/*
# Create new symlinks for all the updates
#cd $APTDIR/${VERSION}/i386/apt-rpm/RPMS.updates/
#find $APTDIR/$VERSION/ -type f ! -name "*.src.rpm" -name "*.rpm" -exec ln -sf {} \;

# Regenerate the apt indexes
genbasedir --flat --bloat $APTDIR/${VERSION}/i386/apt-rpm/ updates

###########################################################################
#### contrib packages
###########################################################################

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $APTDIR/${VERSION}/i386/apt-rpm/base || exit 1
	cd $APTDIR/${VERSION}/i386/apt-rpm
	rm -f SRPMS.contrib
	ln -sf ../contrib/SRPMS/ SRPMS.contrib
	cd $APTDIR/${VERSION}/i386/apt-rpm
	rm -f RPMS.contrib
	ln -sf ../contrib/RPMS RPMS.contrib

	# Create the release files
	cat >$APTDIR/${VERSION}/i386/apt-rpm/base/release.contrib <<EOF
Archive: stable
Component: contrib
Version: ${VERSION}
Origin: linux.fnal.gov
Label: ${VERSION}
Architecture: i386
EOF

fi

# Regenerate the apt indexes
genbasedir --flat --bloat $APTDIR/${VERSION}/i386/apt-rpm contrib 

###########################################################################
#### notsupported packages
###########################################################################

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $APTDIR/${VERSION}/i386/apt-rpm/base || exit 1
	cd $APTDIR/${VERSION}/i386/apt-rpm
	rm -f RPMS.notsupported
	ln -sf ../notsupported/RPMS RPMS.notsupported

	# Create the release files
	cat >$APTDIR/${VERSION}/i386/apt-rpm/base/release.notsupported <<EOF
Archive: stable
Component: notsupported
Version: ${VERSION}
Origin: linux.fnal.gov
Label: ${VERSION}
Architecture: i386
EOF

fi

# Regenerate the apt indexes
genbasedir --flat --bloat $APTDIR/${VERSION}/i386/apt-rpm notsupported 

####################################
########### Remake hash ############
####################################
genbasedir --hashonly $APTDIR/${VERSION}/i386/apt-rpm os updates contrib notsupported 

