#!/bin/bash

if [ -f ~/.bashrc ]; then
    . ~/.bashrc # for DEBMAIL and such settings when calling dch later on
fi

ARG1="$1"

debclean >/dev/null 2>/dev/null || exit 1

mysponge() {
    cat - > .tmp
    mv .tmp "$1"
}

if [ "$ARG1" != "check" ]; then
    echo -n "" > .to_dch
fi
wget -q -O .scripts.irssi.org http://scripts.irssi.org/
for i in $(sed -r -n '/NAME=/ {s,.*NAME="([^"]+)".*,\1,; p;}' .scripts.irssi.org); do
    echo script $i
    echo normal
    echo
done > INDEX.scripts.irssi.org

get_new(){
	script="$1"
	url="$2"
        echo $script >> .scriptlist
        if [ "$PARSING" != "INDEX.scripts.irssi.org" ]; then
	    sed -i "/^script ${script}$/ {N; N; d;}" INDEX.scripts.irssi.org
        fi
	if [ ! -f scripts/$script ]; then
                if [ "$ARG1" != "check" ]; then
		    echo "$script is not currently in scripts/, skipping"
                fi
		return
	fi
        if [ "$ARG1" = "nodownload" ]; then
            cp scripts/$script scripts/$script.new
        else
	    wget -q -O scripts/$script.new "$url"
        fi
        if [ "$?" != "0" ]; then
		echo "$script failed to download"
                rm -f scripts/$script.new
		return
        fi
	if diff -up scripts/$script scripts/$script.new >/dev/null; then
                if [ "$ARG1" != "check" ]; then
		    echo "$script is up to date"
                fi
		rm scripts/$script.new
	else
	        echo "$script needs updating"
                if [ "$ARG1" = "check" ]; then
                    rm scripts/$script.new
                else
                    mv scripts/$script.new scripts/$script
                    echo "$script" >> .to_dch
                fi
	fi
}

get_description() {
    sed -n "/<A NAME=\"$1\"/ {n;n;n;p}" .scripts.irssi.org | sed -r 's/.*<td>(.*)<\/td>/\1/' | perl -M"CGI qw/unescapeHTML/" -n -e 'print unescapeHTML($_);'
}

strip() {
    echo "$@" | sed -e 's/^\s*//' -e 's/\s*$//'
}

set_description() {
    scriptname=$1
    shift
    echo "$scriptname" "$(strip "$@")" >> .descriptions
}

get_description_from_dot_descriptions() {
    grep "^$1 " .descriptions | tail -1 | cut -d ' ' -f 2-
}

echo -n > .scriptlist
echo -n > .descriptions

parse() {
PARSING=$1
while read tok1 tok2 ; do
	if [ "$tok1" = script ] ; then
		script=$tok2
                if [ "$ARG1" != "check" ]; then
		    echo "$script ------------------"
                fi
	fi

	if [ "$tok1" = "url" ] ; then
	    get_new "$script" "$tok2"
	fi

        if [ "$tok1" = "normal" ]; then
		upstream_name="$tok2"
		if [ -z "$upstream_name" ]; then
			upstream_name=$script
		fi
		get_new "$script" "http://scripts.irssi.org/scripts/$upstream_name"
                if [ -z "$(get_description_from_dot_descriptions $script)" ]; then
                    set_description "$script" "$(get_description $upstream_name)"
                fi
	fi

        if [ "$tok1" = "description" ]; then
                set_description "$script" "$tok2"
        fi
done < $1
}

echo -n > update-scripts.log
for i in $(cat INDEX | awk '/^script /{print $2}'); do touch scripts/$i; done
parse INDEX | tee -a update-scripts.log
parse INDEX.scripts.irssi.org | tee -a update-scripts.log
rm INDEX.scripts.irssi.org

cat .scriptlist | sort | sort -u | mysponge .scriptlist
ls scripts/ | grep -Ev "\.(asc|new)$" > .packagedlist
cat .packagedlist | sort | sort -u | mysponge .packagedlist

cp debian/description-list .orig-description-list
echo -n > debian/description-list
COL1_LENGTH=30
for i in $(cat .packagedlist | grep -v XMMSInfo.pm); do
    NAME=$i
    DESC=$(get_description_from_dot_descriptions $i)
    if [ -z "$DESC" ]; then
        echo "No description for: $NAME"
        DESC="No Description"
    fi
    WHITESPACE=""
    for i in `seq $(( $COL1_LENGTH - $(echo -n "$NAME" | wc -c) ))`; do
        WHITESPACE="$WHITESPACE "
    done
    echo "${NAME}${WHITESPACE}${DESC}" >> debian/description-list
done
rm .descriptions

./debian/rewrap.pl | mysponge debian/description-list
if ! diff .orig-description-list debian/description-list >/dev/null; then
    echo "Some descriptions have been changed"
fi
rm .orig-description-list

diff .scriptlist .packagedlist | grep "^>" | awk '{print $2}' | sort > .missinglist
rm .scriptlist .packagedlist
sed -e '/^Known-Missing:/ !d' -e 's/^Known-Missing: //' -e 's/,//g' -e 's/ /\n/g' debian/copyright | sort > .knownlist
if [ -n "$(diff .knownlist .missinglist)" ]; then
    echo "These are the differences between the known missing upstreams and the discovered missing upstreams:" | tee -a update-scripts.log
    diff .knownlist .missinglist | egrep "^[><]" | tee -a update-scripts.log
fi
rm .missinglist .knownlist

if [ "$ARG1" != "check" ]; then
    STR="$(perl -e 'open F, ".to_dch"; my @a = <F>; close F; chomp $_ foreach(@a); my $s = join(", ", @a); print "Updated: " . $s . "\n" if(length($s) > 0);')"
    if [ -n "$STR" ]; then
        dch "$STR"
        echo "$STR"
    fi
    rm .to_dch
fi
rm .scripts.irssi.org
