#!/bin/sh

# To be sure to get "ls" ordering the same interactively and from cron
export LC_ALL=C

# The ls ptions used to compare directory listing to find changes
LSOPT="-1 -R -s --block-size=1 -t"

# State directory which helps knowing if the contents changed
STATEDIR="${HOME}/.ayo-state/foo"
mkdir -p ${STATEDIR} || echo "WARNING: Unable to create ${STATEDIR}"

# Trivial locking mechanism
PID=`cat "${STATEDIR}/lock" 2>/dev/null || echo`
if [ -n "${PID}" -a -d "/proc/${PID}" ]; then
        # Exit silently
        exit 1
fi
if ! ( echo $$ > ${STATEDIR}/lock ); then
        echo "Impossible to create ${STATEDIR}/lock"
        exit 1
fi

# Sanity check as to where we're run from
if [ ! -x "./`basename $0 2>/dev/null`" ]; then
        echo "Run this script from within the directory where it is located."
        echo "It assumes waaaay too much about relative paths :-)"
        exit 1
fi

# Display usage if not enough command line arguments are given
if [ -z "$2" ]; then
        echo "Usage:   $0 <yd|rh|fd|fd64|fdppc> <version> [/path/to/find/ayo]"
        echo "Example: $0 rh 8.0"
        exit 1
fi

# Where to sync to
AYOFTPROOT="$3"

# Sanity check for needed executable files: yum-arch, genbasedir and createrepo
for exe in yum-arch genbasedir createrepo; do
        if [ ! -x "`which $exe 2>/dev/null`" ]; then
                echo "ERROR: Can't find $exe in your current search path"
                exit 1
        fi
done

# Set the directory, arch and extensions from the command line arguments
case "$1" in
    rh)
        ARCH="i386"
        EXTS="86 athlon noarch"
        DIR="../redhat/$2"
        SUBDIR="redhat/$2"
        ;;
    yd)
        ARCH="ppc"
        EXTS="ppc noarch"
        DIR="../yellowdog/$2"
        SUBDIR="yellowdog/$2"
        ;;
    fd)
        DIST="fd"
        ARCH="i386"
        EXTS="86 athlon noarch"
        DIR="../fedora/linux/$2"
        SUBDIR="fedora/linux/$2"
        ;;
    fd64)
        DIST="fd"
        ARCH="x86_64"
        EXTS="x86_64 noarch"
        DIR="../fedora/linux/$2"
        SUBDIR="fedora/linux/$2"
        ;;
    fdppc)
        DIST="fd"
        ARCH="ppc"
        EXTS="ppc noarch"
        DIR="../fedora/linux/$2"
        SUBDIR="fedora/linux/$2"
        ;;
esac

if [ ! -d "$DIR/$ARCH" ]; then
        echo "ERROR: Can't find directory $DIR/$ARCH"
        exit 1
else
        mkdir -p ${STATEDIR}/$DIR/$ARCH
fi

# Check which directory content has eventually changed
# and update all that is needed, nothing more

#for MODULE in os core updates freshrpms dma powertools; do
for MODULE in os core updates tupdates freshrpms extras; do
# Check if module is relevant
if [ -e $DIR/$ARCH/${MODULE}/RPMS -a ! -L $DIR/$ARCH/${MODULE} ]; then

        # Initial value
        ERROR=0

        if [ "${MODULE}" = "freshrpms" ]; then
                ls ${LSOPT} ../$DIR/ \
                        > ${STATEDIR}/$DIR/$ARCH/${MODULE}.list.new 2>/dev/null
        else
                ls ${LSOPT} $DIR/$ARCH/${MODULE}/RPMS/ \
                        > ${STATEDIR}/$DIR/$ARCH/${MODULE}.list.new 2>/dev/null
        fi

        # Check if the content is still identical (0: identical, >=1: different)
        #diff -Naupr ${STATEDIR}/$DIR/$ARCH/${MODULE}.list ${STATEDIR}/$DIR/$ARCH/${MODULE}.list.new
        diff -Naupr ${STATEDIR}/$DIR/$ARCH/${MODULE}.list ${STATEDIR}/$DIR/$ARCH/${MODULE}.list.new &>/dev/null
        IDENTICAL=$?

        # If the content has changed from the previous run, schedule update
        if [ $IDENTICAL -gt 0 ]; then
                touch ${STATEDIR}/$DIR/$ARCH/${MODULE}.changed
                mv ${STATEDIR}/$DIR/$ARCH/${MODULE}.list.new \
                       ${STATEDIR}/$DIR/$ARCH/${MODULE}.list
        fi

        # If the content is identical and the update was scheduled, go for it!
        if [ $IDENTICAL -eq 0 -a -e ${STATEDIR}/$DIR/$ARCH/${MODULE}.changed ]; then
                echo -n "Module $1 $2 ${MODULE} has changed... "

# Special case for the freshrpms module
if [ "${MODULE}" = "freshrpms" ]; then
    # Re-hardlink all packages!
    rm -f $DIR/$ARCH/freshrpms/RPMS/*
    for EXT in ${EXTS}; do
        find ../$DIR -type f -name *${EXT}.rpm -exec \
            ln {} $DIR/$ARCH/freshrpms/RPMS/ \;
    done
fi

                # Generate old yum headers
                # Don't generate obsolete .hdr for FC >= 3
                if [ "${DIST}" != "fd" -o "$2" == "1" -o "$2" == "2" ]; then
                    echo -n "oldyum... "
                    yum-arch $DIR/$ARCH/${MODULE} &>/dev/null || ERROR=1
                fi

		# Generate common metadata
		if [ -f $DIR/$ARCH/${MODULE}/comps.xml ]; then
			OPTS="-g comps.xml"
		else
			OPTS=""
		fi
		echo -n "common... "
		createrepo -d -x '*.src.rpm' ${OPTS} $DIR/$ARCH/${MODULE} &>/dev/null || ERROR=1

                # Generate apt metadata
                echo -n "apt... "
                genbasedir --flat --bloat --bz2only --partial \
                        `pwd`/$DIR/$ARCH ${MODULE} &>/dev/null || ERROR=1

                # Sync files
                #if [ "${MODULE}" != "freshrpms" -a ! -z "${AYOFTPROOT}" ]; then
                if [ ! -z "${AYOFTPROOT}" ]; then
                        echo -n "sync... "
                        rsync -aHP --delete $DIR/$ARCH ${AYOFTPROOT}/${SUBDIR}/ >/dev/null
                fi

                # If there was no error, update the list file, remove the schedule
                if [ ${ERROR} -ne 1 ]; then
                        mv ${STATEDIR}/$DIR/$ARCH/${MODULE}.list.new \
                                ${STATEDIR}/$DIR/$ARCH/${MODULE}.list
                        echo "done."
                        rm -f ${STATEDIR}/$DIR/$ARCH/${MODULE}.changed
                else
                        echo "error."
                fi
        fi
fi
done

# Last, remove trivial lock
rm -f ${STATEDIR}/lock

