#!/bin/sh -e
#
# $Id: sisyphus_link_valid,v 1.1.1.1 2005/05/18 15:23:21 legion Exp $
#

. /etc/sisyphus/functions
. /etc/sisyphus/config

cd "$PREFIX"

PROG="${0##*/}"
LANG=C
WORKDIR=
rarch="$DISTRIBUTION"
quiet=
test_only=

check_format() {
    local f="$1"
    shift || return 1
    local rc=0
    local tempfile="$(mktemp -p $WORKDIR -t "check-format.XXXXXXXXXX")"
    if echo "${f##*/}" |egrep -qs 'list\.src\.'; then
        egrep -v '^[^[:space:]]+[[:space:]][[:alnum:] ]+' "$f" > "$tempfile"
    elif echo "${f##*/}" |egrep -qs "list\.$rarch\."; then
        egrep -v '^[^[:space:]]+[[:space:]][^[:space:]]+' "$f" > "$tempfile"
    fi
    [ $(cat "$tempfile"|wc -l) -eq 0 ] || { rc=1; cat "$tempfile"; }
    rm -f "$tempfile"
    return $rc
}

try_to_fix() {
    local f="$1"
    shift || return 1
    local rc=0
    local tempfile="$(mktemp -p $WORKDIR -t "${f##*/}-try-to-fix.XXXXXXXXXX")"

    cat "$f" |awk '
        /^[^[:space:]]+[[:space:]]+[^[:space:]]/            {print gensub("^([^[:space:]]+)[[:space:]]+([^[:space:]]+.*)$","\\1\t\\2","G"); }
        /^[^[:space:]]+[[:space:]][^[:space:]][[:space:]]+/ {print gensub("^([^[:space:]]+)[[:space:]]([^[:space:]])[[:space:]]+$","\\1\t\\2","G"); }
        /^[[:space:]]+[^[:space:]]+[[:space:]][^[:space:]]/ {print gensub("^[[:space:]]+([^[:space:]]+)[[:space:]]([^[:space:]]+.*)$","\\1\t\\2","G"); }
    ' > "$tempfile"
    
    if check_format "$tempfile"; then
        Message "${f##*/} FIXED"
        [ -n "$test_only" ] || mv "$tempfile" "$f"
    else
        Message "ERROR: ${f##*/} FAILD"
#        [ -n "$test_only" ] || mv "$tempfile" "$f-faild"
    fi
    rm -f "$tempfile"
}

WORKDIR=
exit_handler()
{
	local rc=$?
	trap - EXIT
	[ -z "$WORKDIR" ] || rm -rf -- "$WORKDIR"
	exit $rc
}

Usage() {
    [ "$1" = 0 ] || exec >&2
    cat << EOF
Usage: $PROG [options]

Options:
    -h| --help       show this message
    -q| --quiet      quiet
    -t| --test-only  Do nothing. Report only. 
EOF
    [ -n "$1" ] && exit "$1" || exit
}

TEMP=`getopt -n $PROG -o h,q,t,a: -l help,quiet,test-only,arch: -- "$@"` || Usage
eval set -- "$TEMP"

while :; do
    case $1 in
        -q| --quiet) quiet=1
            ;;
        -t| --test-only) test_only=1
            ;;
        -a| --arch) shift
            [ -z "$1" ] || rarch="$1"
            ;;
	-h| --help) Usage 0
	    ;;
	--) shift; break
	    ;;
	*) Fatal "unrecognized option: $1"
	    ;;
    esac
    shift
done

trap exit_handler SIGHUP SIGINT SIGQUIT SIGTERM EXIT
WORKDIR="$(mktemp -d -t "$PROG.XXXXXXXXXX")"

Message "Check format lists"
for f in files/list.*; do
    [ "$f" != "files/list.*" ] || { echo "ERROR: Can't find lists">&2; exit 1; }

    if ! check_format "$f"; then
        Message "WARNING: Format errors detected at '${f##*/}'!"
        try_to_fix "$f"
    fi
done

Message "Check order in lists"
for f in files/list.*; do
    [ "$f" != "files/list.*" ] || { echo "ERROR: Can't find lists">&2; exit 1; }
    sort -u -o "$WORKDIR/${f##*/}" "$f"
    if ! cmp -s "$WORKDIR/${f##*/}" "$f"; then
        [ -n "$test_only" ] || mv "$WORKDIR/${f##*/}" "$f"
        Message "'${f##*/}' sorted"
    else
        rm -f "$WORKDIR/${f##*/}"
    fi
done
