#!/bin/sh
#
# (C) 2001 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>

umask 022

TMPROOT="/home/users/pldra/tmp"
FTPROOT="/home/users/pldra/ftp/"
ra_ARCHS="i386 i586 i686 sparc alpha ppc"
ac_ARCHS="athlon i386 i586 i686 sparc alpha ppc"
nest_ARCHS="athlon i386 i586 i686 ppc"
current_DISTRO=ac

usage="\
Usage: gen.databases [<options>] <distribution>\n\
Options:\n\
   --ftproot=dir         Top directory of ftp site\n\
   --tmproot=dir         Directory for temporary files (normally /tmp)\n\
   --arch="names"        Update information for archs\n"

while test $# -gt 0 ; do
    case "${1}" in
    -h | --help)
        echo -e "${usage}"
        exit 0
        ;;
    --ftproot=*)
        FTPROOT="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
        if [ ! -d $FTPROOT ]; then
             echo "Invalid top directory for ftp site ${FTPROOT}" 1>&2
             exit 1
        fi
        ;;
    --tmproot=*)
        TMPROOT="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
        if [ ! -d $TMPROOT ]; then
             echo "Invalid tmp directory ${tmpdir}" 1>&2
             exit 1
        fi
        ;;
    --arch=*)
        ra_ARCHS="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
        ac_ARCHS="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
        nest_ARCHS="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
        ;;
    -*)
        echo -e "${usage}" 1>&2
        exit 1
        ;;
    *)
        break
        ;;
    esac
    shift
done



if [ -n "$1" ]; then
	DISTROS="$1"
else
	DISTROS="ra"
fi
FTPBINDIR="${FTPROOT}/.stat/bin"
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${FTPROOT}/.stat/lib; export LD_LIBRARY_PATH

for distros in $DISTROS; do
  eval ARCHS="\$${distros}_ARCHS"
  echo "Doing distribution: ${distros}"

  case "${distros}" in
	  nest)
	  	POLDEK=${FTPBINDIR}/poldek-nest
		GENBASEDIR=${FTPBINDIR}/genbasedir
		GENTOCF=${FTPBINDIR}/gentocf
		;;
	  *)
	  	POLDEK=${FTPBINDIR}/poldek-ra
		GENBASEDIR=${FTPBINDIR}/genbasedir
		GENTOCF=${FTPBINDIR}/gentocf
		;;
  esac

  for arch in ${ARCHS}; do
    echo "Doing ${distros} - architecture: ${arch}"

    TOCFTEMPDIR="$(mktemp -d ${TMPROOT}/gentocf.XXXXXX)" || exit 1
    echo "Indexes for poldek"
    if [ "$distros" = "$current_DISTRO" ]; then
      ${POLDEK} --cachedir=${TMPROOT}/poldek -s ${FTPROOT}/dists/${distros}/PLD/${arch}/PLD/RPMS --mkidxz
      ${POLDEK} --cachedir=${TMPROOT}/poldek -s ${FTPROOT}/dists/${distros}/supported/${arch} --mkidxz
    fi
    if [ "$distros" = ra ]; then
      ${POLDEK} --cachedir=${TMPROOT}/poldek -s ${FTPROOT}/dists/1.1/PLD/${arch}/PLD/RPMS --mkidxz
    fi
    ${POLDEK} --cachedir=${TMPROOT}/poldek -s ${FTPROOT}/dists/${distros}/updates/general/${arch} --mkidxz
    ${POLDEK} --cachedir=${TMPROOT}/poldek -s ${FTPROOT}/dists/${distros}/updates/security/${arch} --mkidxz
    ${POLDEK} --cachedir=${TMPROOT}/poldek -s ${FTPROOT}/dists/${distros}/ready/${arch} --mkidxz
    ${POLDEK} --cachedir=${TMPROOT}/poldek -s ${FTPROOT}/dists/${distros}/test/${arch} --mkidxz

    echo "Indexes for apt"
    ${GENBASEDIR} --bloat --tmpdir=${TMPROOT} --topdir=${FTPROOT}/dists/ ${distros}/apt/${arch} test
    ${GENBASEDIR} --bloat --tmpdir=${TMPROOT} --topdir=${FTPROOT}/dists/ ${distros}/apt/${arch} base updates-security updates-general supported test
    if [ "$distros" = "$current_DISTRO" ]; then
      ${GENBASEDIR} --bloat --tmpdir=${TMPROOT} --topdir=${FTPROOT}/dists/ ${distros}/apt/${arch} base updates-security updates-general supported
    else
      ${GENBASEDIR} --bloat --tmpdir=${TMPROOT} --topdir=${FTPROOT}/dists/ ${distros}/apt/${arch} updates-security updates-general
    fi
    echo "Part Two"

  done
done

LANG=C date -u > "$FTPROOT/.stat/trace/ftp.pld-linux.org"

