#! /bin/sh

# make-apt, make-yum2
# Create apt databases from a redhat mirror

# Based on the following scripts and information:
# http://freshrpms.net/apt/server/
# ftp://apt-rpm.tuxfamily.org/pub/ftp.tuxfamily.org/apt-rpm/scripts/
# http://mirror.phy.bnl.gov/apt-rpm/scripts/makeapt.sh

# Absolute directory for top of redhat mirror
MIRRORDIR=/home/yumadm/pub/package
# Absolute directory for top of apt repository
APTDIR=/home/yumadm/pub/up2date
# Name of distribution (case sensitive). Used below
DIST=RedHat
# Directory where `os' packages will reside, in MIRRORDIR,
# relative to MIRRORDIR/redhat/linux/VERSION/en/os/i386/
OSDIR=$DIST/RPMS
# Directory where `base' files such as comps.xml reside in MIRRORDIR.
BASE=$DIST/base
# URL of repository
ORIGINURL=localhost

ME=$(basename $0)

# 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" -a "$1" != "test" -a "$1" != "clean" \) ]; then
        echo "Usage: $ME  "
        echo "   is one of:"
        echo "        \"full\": full apt repository creation and index generation"
        echo "        \"all\": no apt repository creation, but full indexing"
        echo "        \"quick\": no apt repository creation, only updates & extra indexing"
        echo "        \"test\": output directory locations; does nothing"
        echo "   gives the Redhat version in canonical form"
        echo "        (eg: \"8.0\", \"9\")"
        exit 1
fi

MODE=$1
VERSION=$2

if [ "$MODE" = "test" ]; then
        echo "APTDIR:"
        echo "$APTDIR/redhat/${VERSION}/en/i386"
        echo "OS MIRRORDIR:"
        echo "$MIRRORDIR/redhat/linux/${VERSION}/en/os/i386/$OSDIR"
        echo "OS RPM:"
        find $MIRRORDIR/redhat/linux/${VERSION}/en/os/i386/$OSDIR -maxdepth 1 -type f ! -name "*.src.rpm" -name "*.rpm" | head -1
        echo "UPDATES MIRRORDIR:"
        echo "$MIRRORDIR/redhat/linux/updates/${VERSION}/en/os/"
        echo "UPDATE RPM:"
        find $MIRRORDIR/redhat/linux/updates/${VERSION}/en/os/ -type f ! -name "*.src.rpm" -name "*.rpm" | head -1
        exit 0
fi

if [ "$MODE" = "clean" ]; then
# Caution: Wipes out all extra rpms's; therefore, commented out and undocumented!!!
#rm -rf $APTDIR/redhat/${VERSION}
exit 0
fi 

if [ "$MODE" = "full" ]; then
  # Create directory
  mkdir -p $APTDIR/redhat/${VERSION}/en/i386/base

  cd $APTDIR/redhat/${VERSION}/en/i386/base
  if [ -d $MIRRORDIR/redhat/linux/${VERSION}/en/os/i386/$BASE ]; then
    find $MIRRORDIR/redhat/linux/${VERSION}/en/os/i386/$BASE/ -type f -exec ln -sf '{}' \;
  fi

  if [ $ME != make-yum2 ]; then
    # Create the main release file
    cat >$APTDIR/redhat/${VERSION}/en/i386/base/release <<EOF
Origin: $ORIGINURL
Label: Red Hat $VERSION
Suite: Red Hat $VERSION
Architectures: i386
Components: os updates extra
Description: apt repository of Red Hat $VERSION os + updates + extra RPM's
EOF
  fi
fi

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

if [ "$MODE" = "full" ]; then
  if [ $ME != make-yum2 ]; then
    # Create the release file
    cat >$APTDIR/redhat/${VERSION}/en/i386/base/release.os <<EOF
Archive: stable
Component: os
Version: $VERSION
Origin: $ORIGINURL
Label: Red Hat $VERSION
Architecture: i386
EOF
  fi
fi

if [ "$MODE" = "full" -o "$MODE" = "all" ]; then
# Remove old symlink to rpm directory
rm -f $APTDIR/redhat/${VERSION}/en/i386/os
# Create new symlink to rpm directory
ln -s $MIRRORDIR/redhat/linux/${VERSION}/en/os/i386/$OSDIR $APTDIR/redhat/${VERSION}/en/i386/os

# Generate apt index
#genbasedir --flat --bloat $APTDIR/redhat/${VERSION}/en/i386 os

  # Generate yum index
  echo "yumifying $APTDIR/redhat/${VERSION}/en/i386/os"
  if [ $ME = make-yum2 ]; then
    createrepo $APTDIR/redhat/${VERSION}/en/i386/os
  else
    rm -rf $APTDIR/redhat/${VERSION}/en/i386/os/headers
    yum-arch -lq $APTDIR/redhat/${VERSION}/en/i386/os
  fi
fi

###########################################################################
#### REDHAT UPDATES
###########################################################################

if [ "$MODE" = "full" ]; then
  # Create directory
  mkdir -p $APTDIR/redhat/${VERSION}/en/i386/updates
  if [ $ME != make-yum2 ]; then
    # Create release file
    cat >$APTDIR/redhat/${VERSION}/en/i386/base/release.updates <<EOF
Archive: stable
Component: updates
Version: $VERSION
Origin: $ORIGINURL
Label: Red Hat $VERSION
Architecture: i386
EOF
  fi
fi

# Remove old symlinks to rpms
rm -f $APTDIR/redhat/${VERSION}/en/i386/updates/*.rpm
# Create new symlinks for all updates (in multiple directories)
cd $APTDIR/redhat/${VERSION}/en/i386/updates/
find $MIRRORDIR/redhat/linux/updates/${VERSION}/en/os/ -type f ! -name "*.src.rpm" -name "*.rpm" -exec ln -sf '{}' \;

# Regenerate apt index
#genbasedir --flat --bloat $APTDIR/redhat/${VERSION}/en/i386 updates

# Regenerate yum headers
echo "yumifying $APTDIR/redhat/${VERSION}/en/i386/updates"
if [ $ME = make-yum2 ]; then
  createrepo $APTDIR/redhat/${VERSION}/en/i386/updates
else
  rm -rf $APTDIR/redhat/${VERSION}/en/i386/updates/headers
  yum-arch -lq $APTDIR/redhat/${VERSION}/en/i386/updates
fi

###########################################################################
#### REDHAT EXTRA PACKAGES
###########################################################################

if [ "$MODE" = "full" ]; then
  # Create directory
  mkdir -p $APTDIR/redhat/${VERSION}/en/i386/extra
  if [ $ME != make-yum2 ]; then
    # Create release file
    cat >$APTDIR/redhat/${VERSION}/en/i386/base/release.extra <<EOF
Archive: stable
Component: extra
Version: $VERSION
Origin: $ORIGINURL
Label: Red Hat $VERSION
Architecture: i386
EOF
  fi
fi

# Regenerate apt index
#genbasedir --flat --bloat $APTDIR/redhat/${VERSION}/en/i386 extra

# Regenerate yum headers
echo "yumifying $APTDIR/redhat/${VERSION}/en/i386/extra"
if [ $ME = make-yum2 ]; then
  createrepo $APTDIR/redhat/${VERSION}/en/i386/extra
else
  rm -rf $APTDIR/redhat/${VERSION}/en/i386/extra/headers
  yum-arch -lq $APTDIR/redhat/${VERSION}/en/i386/extra
fi

###########################################################################
########### REMAKE HASH
###########################################################################

#genbasedir --hashonly $APTDIR/redhat/${VERSION}/en/i386 os updates extra

# For yum>2.4 with xml repository metadata: needs createrepo package.
if [ $ME = make-yum2 ]; then
  echo "Generating repository metadata of $APTDIR"
  createrepo $APTDIR
fi
