#!/bin/sh

# Usage : updates.sh [/path/to/redhat [/path/to/updates]]

test -z "$1" && RHDIR="/var/ftp/pub/redhat"  || RHDIR="$1"
test -z "$2" && UPDIR="/var/ftp/pub/updates" || UPDIR="$2"

# 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, create if needed
STATEDIR="${HOME}/.ayo-state/updates"
[ -d ${STATEDIR} ] || mkdir -p ${STATEDIR}

X86RPM="86.rpm athlon.rpm noarch.rpm"
PPCRPM="ppc.rpm noarch.rpm"
SRCRPM="src.rpm"

fail() {
    echo $1
    exit 1
}

# Usage : flatten /path/to/subdirs /path/to/flat/dir "extension1 extension2 ..."
flatten() {
    SDIR="$1"
    FDIR="$2"
    EXTS="$3"
    test -d ${SDIR} || fail "The source directory ${SDIR} doesn't exist."
    test -d ${FDIR} || fail "The destination directory ${FDIR} doesn't exist."
    (cd ${FDIR} && rm -f *) || fail "Can't remove files in the destination directory."
    for EXT in ${EXTS}; do
        find ${SDIR} -name "*${EXT}" -exec cp -alf {} ${FDIR}/ \;
    done
}
        
# Red Hat Linux
# Updates
for version in 6.2 7.0 7.1 7.2 7.3 8.0 9; do
    SUBFDIR="redhat-${version}-i386"
    SDIR="${RHDIR}/linux/updates/${version}/en/os"
    FDIR="${UPDIR}/${SUBFDIR}"
    ls ${LSOPT} ${SDIR} > ${STATEDIR}/${SUBFDIR}.list.new 2>/dev/null
    #diff -Naupr ${STATEDIR}/${SUBFDIR}.list ${STATEDIR}/${SUBFDIR}.list.new >/dev/null 2>&1
    diff -Naupr ${STATEDIR}/${SUBFDIR}.list ${STATEDIR}/${SUBFDIR}.list.new
    IDENTICAL=$?
    if [ $IDENTICAL -gt 0 ]; then
        flatten ${SDIR} ${FDIR} "${X86RPM}" && \
        mv ${STATEDIR}/${SUBFDIR}.list.new ${STATEDIR}/${SUBFDIR}.list
    fi
done
# DMA
for version in 7.0 7.1; do
    # Binary packages
    SUBFDIR="redhat-${version}-DMA-i386"
    SDIR="${RHDIR}/linux/${version}/en/DMA"
    FDIR="${UPDIR}/${SUBFDIR}"
    ls ${LSOPT} ${SDIR} > ${STATEDIR}/${SUBFDIR}.list.new 2>/dev/null
    #diff -Naupr ${STATEDIR}/${SUBFDIR}.list ${STATEDIR}/${SUBFDIR}.list.new >/dev/null 2>&1
    diff -Naupr ${STATEDIR}/${SUBFDIR}.list ${STATEDIR}/${SUBFDIR}.list.new
    IDENTICAL=$?
    if [ $IDENTICAL -gt 0 ]; then
        flatten ${SDIR} ${FDIR} "${X86RPM}" && \
        mv ${STATEDIR}/${SUBFDIR}.list.new ${STATEDIR}/${SUBFDIR}.list
    fi
    # Source packages
    SUBFDIR="redhat-${version}-DMA-SRPMS"
    FDIR="${UPDIR}/${SUBFDIR}"
    ls ${LSOPT} ${SDIR} > ${STATEDIR}/${SUBFDIR}.list.new 2>/dev/null
    #diff -Naupr ${STATEDIR}/${SUBFDIR}.list ${STATEDIR}/${SUBFDIR}.list.new >/dev/null 2>&1
    diff -Naupr ${STATEDIR}/${SUBFDIR}.list ${STATEDIR}/${SUBFDIR}.list.new
    IDENTICAL=$?
    if [ $IDENTICAL -gt 0 ]; then
        flatten ${SDIR} ${FDIR} "${SRCRPM}" && \
        mv ${STATEDIR}/${SUBFDIR}.list.new ${STATEDIR}/${SUBFDIR}.list
    fi

#    flatten ${RHDIR}/linux/${version}/en/DMA \
#        ${UPDIR}/redhat-${version}-DMA-i386 "${X86RPM}"
#    flatten ${RHDIR}/linux/${version}/en/DMA \
#        ${UPDIR}/redhat-${version}-DMA-SRPMS "${SRCRPM}"
done
