#!/bin/sh
#
# $Id: genbasedir,v 1.18 2001/03/11 14:36:28 claudio Exp $
#
# This script generates the contents of the base/ directory, by creating
# the pkglists and the hash file. Update the components directory to contain
# the components of your repository.
#
# $Log: genbasedir,v $
# Revision 1.18  2001/03/11 14:36:28  claudio
# Added appropriate message at the end of script.
#
# Revision 1.17  2001/01/04 21:26:14  kojima
# fixed some mem leaks
#
# Revision 1.16  2000/12/10 13:42:58  claudio
# Added linefeeds to usage message, replaced spaces for tabs
#
# Revision 1.15  2000/11/08 21:30:00  kojima
# fixed bug in release retrieval code
#
# Revision 1.14  2000/11/08 20:40:23  kojima
# fixed compat prob with bash1
#
# Revision 1.13  2000/11/06 12:53:49  kojima
# fixed compile errors for RedHat 6.x (with gcc -Wall -Werror)
#
# Revision 1.12  2000/10/31 20:30:42  kojima
# some updates
#
# Revision 1.11  2000/10/30 16:41:18  kojima
# fixed genbasedir
#
# Revision 1.9  2000/10/29 21:49:54  kojima
# fixed gensrclist/pkglist bug
#
# Revision 1.8  2000/10/25 17:17:35  kojima
# *** empty log message ***
#
# Revision 1.7  2000/10/25 15:48:35  kojima
# *** empty log message ***
#
# Revision 1.6  2000/10/23 14:31:57  kojima
# *** empty log message ***
#
# Revision 1.5  2000/10/19 19:32:34  claudio
# Language setting to generate a consistent pkglist.
#
 
usage="\
Usage: genbasedir [<options>] <distribution> <comp1> [<comp2> ... <compN>]\n\
Options:\n\
   -s, --sign		Generate and sign hashfile\n\
   --hashonly		Do hashfile stuff only\n\
   --listonly		Generate pkglists/srclists and quit\n\
   --topdir=dir		Top directory of repository\n\
   --updateinfo=file	Update information file\n"

basedir=.
signature=0
listonly=0
hashonly=0
updateinfo=""
mapi=0

# bloat is necessary for non-Conectiva distros, at least RH,
# because they use file dependencies with a non-predictable
# heuristic. So we can't strip-off paths that will probably
# never appear in dependencies.
bloat=""

while test $# -gt 0 ; do
    case "${1}" in
    -h | --help)
        echo -e "${usage}"
        exit 0
        ;;
    --mapi)
    	# hee hee hee..
        mapi=1
	;;
    --listonly)
        listonly=1
	;;
    --hashonly)
        hashonly=1
	;;
    --updateinfo=*)
	updateinfo=${1}
	;;
    --bloat)
	bloat="--bloat"
	;;
    --topdir=*)
        topdir="`echo \"${1}\" | sed -e 's/^[^=]*=//'`"
	if [ ! -d $topdir ]; then
	     echo "Invalid top directory for distribution ${topdir}" 1>&2
	     exit 1
	fi
        ;;
    -s | --sign)
	signature=1;
	;;
    -*)
        echo -e "${usage}" 1>&2
        exit 1
        ;;
    *)
        break
        ;;
    esac
    shift
done

distro=${1}
shift

components=$*

if [ -z "$components" ]; then
    echo -e "${usage}"
    exit 0
fi


getsize() {
    tmp=`wc -c $1`
    echo $tmp|cut -f1 -d\  
}


phashstuff() {
    size=`getsize ${1}`
    md5=`md5sum ${1}|cut -f1 -d\  `

    echo " $md5 $size ${2}"
}



cd ${topdir}

basedir_=`echo ${distro}/base|tr -s /`
basedir=${topdir}/$basedir_


# release file
# ------------

#for comp in ${components}; do
#     true > ${basedir}/release.$comp
#           
#done


if [ $hashonly -ne 1 ]; then
# package lists
# -------------

true > /tmp/srcidx.$$

for comp in ${components}; do
    echo -n "${comp}: "

    echo -n "pkglist "
    if test x$updateinfo = x; then
    	(cd $basedir; genpkglist $bloat --index /tmp/srcidx.$comp.$$ $topdir/${distro} $comp)
    else
    	(cd $basedir; genpkglist $bloat --index /tmp/srcidx.$comp.$$ --info $updateinfo $topdir/${distro} $comp)
    fi
    
    bzip2 -c $basedir/pkglist.$comp > $basedir/pkglist.$comp.bz2

    cat /tmp/srcidx.$comp.$$ >> /tmp/srcidx.$$

    echo "done"
done


for comp in ${components}; do
    echo -n "${comp}: "

    echo -n "srclist "
    if [ $mapi -ne 0 ]; then
        (cd $basedir; gensrclist $topdir/${distro}/.. $comp /tmp/srcidx.$comp.$$ --mapi)
    else
        (cd $basedir; gensrclist $topdir/${distro}/.. $comp /tmp/srcidx.$$)
    fi

    bzip2 -c $basedir/srclist.$comp > $basedir/srclist.$comp.bz2

    echo "done"
done
fi

if [ $listonly -eq 0 ]; then
   hf=${basedir}/hashfile
   true > $hf
else
   hf=/dev/null
fi

echo "MD5SUM:" >> $hf

pkglist_=${basedir_}/pkglist
srclist_=${basedir_}/srclist
release_=${basedir_}/release
pkglist=${basedir}/pkglist
srclist=${basedir}/srclist
release=${basedir}/release

for comp in ${components}; do
    echo -n "${comp}: "

    echo -n "hashfile "
    phashstuff ${pkglist}.$comp ${pkglist_}.$comp >> $hf
    phashstuff ${srclist}.$comp ${srclist_}.$comp >> $hf

    phashstuff ${pkglist}.$comp.bz2 ${pkglist_}.$comp.bz2 >> $hf
    phashstuff ${srclist}.$comp.bz2 ${srclist_}.$comp.bz2 >> $hf

    if [ -f ${release}.$comp ]; then
	phashstuff ${release}.$comp ${release_}.$comp >> $hf
    fi

    echo "done"
done

echo >> $hf


if [ $signature -ne 0 -a $listonly -eq 0 ]; then
    gpg -armour -qs --yes $basedir/hashfile
    mv -f $basedir/hashfile.asc $basedir/hashfile.gpg
    rm -f $basedir/hashfile
fi


echo "All your base are belong to us !!"

