#! /bin/sh
. locations.include
# 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.orapt-rpm/scripts/
# to be more generic.

# Relative directory from the top of the apt repository to the top of the mirror
#MIRRORDIR=../mirror
MIRRORDIR=.

# 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 "updates"
#	* if the main os packages change (should not), use "all"

if [ $# -ne 2 -o \( "$1" != "full" -a "$1" != "all" -a "$1" != "updates" -a "$1" != "contrib" \) ]; then
	echo "Usage: $0 <mode> <default> "
	echo "  <mode> is one of:"
	echo "        \"full\": full apt repository creation"
	echo "        \"all\": no apt repository creation, but full indexes regeneration"
	echo "        \"updates\": no apt repository creation, only updates indexes regeneration"
	echo "        \"contrib\": no apt repository creation, only contrib indexes regeneration"
	exit 1
fi

MODE=$1
#getting from locations.include
DEFAULT=$2
echo "Using $MODE"
if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $RELEASEDIR/$ARCH/apt/base || exit 1

	# Create the main release file
	cat >$RELEASEDIR/$ARCH/apt/base/release <<EOF
Origin: ftp.scientificlinux.org
Label: ${APTVERSION}
Suite: ${APTVERSION}
Architectures: ${ARCH}
Components: os updates contrib 
Description: apt mirror of ${APTVERSION} + updates
EOF

fi

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

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $RELEASEDIR/$ARCH/apt/base || exit 1

	# Create the release files
	cat >$RELEASEDIR/$ARCH/apt/base/release.os <<EOF
Archive: stable
Component: os
Version: ${APTVERSION}
Origin: ftp.scientificlinux.org
Label: ${APTVERSION}
Architecture: ${ARCH} 
EOF

	# Put the symlinks for the base OS packages
	cd $RELEASEDIR/$ARCH/apt
	rm -f SRPMS.os
	ln -sf ../../SRPMS/vendor/original SRPMS.os
	cd $RELEASEDIR/$ARCH/apt
	rm -f RPMS.os
	ln -sf ../$DEFAULT/RPMS RPMS.os
fi

if [ "$MODE" = "full" -o "$MODE" = "all" ]; then
	# Generate APT indexes
	genbasedir --flat --bloat --progress $RELEASEDIR/$ARCH/apt os
fi

###########################################################################
####  updates
###########################################################################

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $RELEASEDIR/$ARCH/apt/base || exit 1
#	mkdir -p $RELEASEDIR/$ARCH/apt/RPMS.updates || exit 1

	# Create the release files
	cat >$RELEASEDIR/$ARCH/apt/base/release.updates <<EOF
Archive: stable
Component: updates
Version: ${APTVERSION}
Origin: ftp.scientificlinux.org 

Label: ${APTVERSION}
Architecture: ${ARCH}
EOF

	cd $RELEASEDIR/$ARCH/apt
	rm -f SRPMS.updates
	ln -sf ../../SRPMS/vendor/errata SRPMS.updates
	rm -rf RPMS.updates
	mkdir RPMS.updates
	cd RPMS.updates
        find ../../errata/$DEFAULT/RPMS/*.rpm -lname "*/$DEFAULT/RPMS/*" -type l -printf "%f\n" | xargs -n1 --replace ln -s ../../$DEFAULT/RPMS/{} {}
        # Regenerate the apt indexes
        genbasedir --flat --bloat --progress $RELEASEDIR/$ARCH/apt/ updates
fi

if [ "$MODE" = "updates" ]; then
	cd $RELEASEDIR/$ARCH/apt
	rm -rf RPMS.updates
	mkdir RPMS.updates
	cd RPMS.updates
        find ../../errata/$DEFAULT/RPMS/*.rpm -lname "*/$DEFAULT/RPMS/*" -type l -printf "%f\n" | xargs -n1 --replace ln -s ../../$DEFAULT/RPMS/{} {}
        find ../../errata/$DEFAULT/RPMS/*.rpm -type f -printf "%f\n" | xargs -n1 --replace ln -s ../../errata/$DEFAULT/RPMS/{} {}
        # Regenerate the apt indexes
        genbasedir --flat --bloat --progress $RELEASEDIR/$ARCH/apt/ updates
fi


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

if [ "$MODE" = "contrib" ]; then
	cd $RELEASEDIR/$ARCH/apt
	rm -f SRPMS.contrib/*.rpm
	cd $RELEASEDIR/$ARCH/
        for a in `find contrib/SRPMS/ -name "*.src.rpm" -print`;
        do 
            bname=`basename $a`;           
            (cd apt/SRPMS.contrib ; ln -s ../../$a $bname);
        done
	cd $RELEASEDIR/$ARCH/apt
	rm -f RPMS.contrib/*.rpm
	cd $RELEASEDIR/$ARCH/
        for a in `find contrib/RPMS/ -name "*.rpm" -print`;
        do 
            bname=`basename $a`;           
            (cd apt/RPMS.contrib ; ln -s ../../$a $bname);
        done
        # Regenerate the apt indexes
        genbasedir --flat --bloat --progress $RELEASEDIR/$ARCH/apt contrib 
fi

if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $RELEASEDIR/$ARCH/apt/base || exit 1
	cd $RELEASEDIR/$ARCH/apt
	rm -rf SRPMS.contrib
        mkdir SRPMS.contrib
	cd $RELEASEDIR/$ARCH/
        for a in `find contrib/SRPMS/ -name "*.src.rpm" -print`;
        do 
            bname=`basename $a`;           
            (cd apt/SRPMS.contrib ; ln -s ../../$a $bname);
        done
	cd $RELEASEDIR/$ARCH/apt
	rm -rf RPMS.contrib
	mkdir RPMS.contrib
	cd $RELEASEDIR/$ARCH/
        for a in `find contrib/RPMS/ -name "*.rpm" -print`;
        do 
            bname=`basename $a`;           
            (cd apt/RPMS.contrib ; ln -s ../../$a $bname);
        done
	# Create the release files
	cat >$RELEASEDIR/$ARCH/apt/base/release.contrib <<EOF
Archive: stable
Component: contrib
Version: ${APTVERSION}
Origin: ftp.scientificlinux.org
Label: ${APTVERSION}
Architecture: ${ARCH}
EOF

         # Regenerate the apt indexes
         genbasedir --flat --bloat --progress $RELEASEDIR/$ARCH/apt contrib 
fi
####################################
########### Remake hash ############
####################################
genbasedir --hashonly $RELEASEDIR/$ARCH/apt os updates contrib
