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

# 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/htdocs/scripts:$PATH
export PATH=$RELEASEDIR/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 2 -o \( "$1" != "full" -a "$1" != "all" -a "$1" != "quick" \) ]; then
	echo "Usage: $0 <mode> <discnum>"
	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"
	exit 1
fi

MODE=$1
DISCNUM=$2
#GETTING APTVERSION AND DEFAULT and RELEASEDIR from locations.include

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

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

fi

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

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

	# Create the release files
	cat >$RELEASEDIR/$DISCNUM/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/$DISCNUM/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 $RELEASEDIR/$DISCNUM/apt os
  fi
fi

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

if [ -d $RELEASEDIR/$DISCNUM/contrib/RPMS ] ; then
   if [ "$MODE" = "full" ]; then
	  # Create directories
	  mkdir -p $RELEASEDIR/$DISCNUM/apt/base || exit 1
	  cd $RELEASEDIR/$DISCNUM/apt
	  rm -f SRPMS.contrib
	  mkdir SRPMS.contrib
	  cd $RELEASEDIR/$DISCNUM/
          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/$DISCNUM/apt
	  rm -f RPMS.contrib
	  mkdir RPMS.contrib
	  cd $RELEASEDIR/$DISCNUM/
          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/$DISCNUM/apt/base/release.contrib <<EOF
Archive: stable
Component: contrib
Version: ${APTVERSION}
Origin: ftp.scientificlinux.org
Label: ${APTVERSION}
Architecture: $ARCH
EOF
   fi
# Regenerate the apt indexes
genbasedir --flat --bloat $RELEASEDIR/$DISCNUM/apt contrib 
fi

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

if [ -d $RELEASEDIR/$DISCNUM/notsupported/RPMS ] ; then
   if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $RELEASEDIR/$DISCNUM/apt/base || exit 1
	cd $RELEASEDIR/$DISCNUM/apt
	rm -f RPMS.notsupported
	ln -sf ../notsupported/RPMS RPMS.notsupported

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

   fi
# Regenerate the apt indexes
genbasedir --flat --bloat $RELEASEDIR/$DISCNUM/apt notsupported 
fi

###########################################################################
#### SRPMS packages
###########################################################################

if [ -d $RELEASEDIR/$DISCNUM/SRPMS ] ; then
   if [ "$MODE" = "full" ]; then
	# Create directories
	mkdir -p $RELEASEDIR/$DISCNUM/apt/base || exit 1
	cd $RELEASEDIR/$DISCNUM/apt
	ln -sf ../SRPMS SRPMS.os

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

   fi

# Regenerate the apt indexes
genbasedir --flat --bloat $RELEASEDIR/$DISCNUM/apt srpms 

fi
