#!/bin/bash
#
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: scripts/Check-PkgVersion
# Copyright (C) 2008 The OpenSDE Project
# Copyright (C) 2004 - 2006 The T2 SDE Project
# Copyright (C) 1998 - 2003 Clifford Wolf
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

if [ "$1" = "-repository" ] ; then
	shift ; for y ; do
		for x in package/$y/* ; do $0 ${x##*/} ; done
	done
	exit 0
fi

if [ "$1" != "${1#-}" -o $# -eq 0 ] ; then
	echo "Usage: $0 <package-names>"
	echo "or     $0 -repository <repository-names>"
	exit 1
fi

curl_options='--disable-epsv'

# Set proxy information
if [ -f download/Proxy ]; then
	proxy="`cat download/Proxy`"
	if [ "$proxy" ]; then
		echo "INFO: Setting proxy to $proxy."
		curl_options="$curl_options --proxy $proxy"
	else
		echo "INFO: No proxy information... removing download/Proxy."
		rm download/Proxy
	fi
fi
if [ -f download/Proxy-auth ]; then
	proxyauth="`cat download/Proxy-auth`"
	if [ "$proxyauth" ]; then
		echo "INFO: Setting proxy authentication information."
		curl_options="$curl_options --proxy-user $proxyauth"
	else
		echo "INFO: No proxy-auth information... removing download/Proxy-auth."
		rm download/Proxy-auth
 	fi
fi



mkdir -p checkver

for package ; do
	for pdir in package/*/$package ; do : ; done
	if [ -x $pdir/check-version ] ; then
		sh $pdir/check-version
	elif [ -f $pdir/$package.desc ] ; then
		x="`egrep '^\[D\] ' $pdir/$package.desc | head -n 1 | tr -s ' '`"
		if [ -z "$x" ] ; then
			echo "No [D] for package $package found!"
		else
		    echo "Testing package $package ..."
		    {
			pattern="`echo $x | cut -f3 -d' '`"
			url="`echo $x | cut -f4 -d' '`"; url="${url#-}"

			if [ "${url:0:1}" = "!" ]; then
				pattern="${url##*/}"
				url="${url%/*}"
			fi

			url="$( echo ${url#!} | sed -e 's,http://dl.sourceforge.net/sourceforge/,http://prdownloads.sourceforge.net/,' )"
			pattern="`echo $pattern | \
				sed -e 's,+,\\\\+,g' -e 's,[0-9].*$,,' \
				    -e 's,$,[0-9],' -e 's,^,^,'`"

			delpattern='\.(gz|bz2|Z)$ \.(tar|zip|tgz|tbz)$ ^[^0-9]+'
			delpattern="$delpattern"' .*\.(deb|sign?|diff|rpm)$'
			delpattern="$delpattern"' .*\.(dsc|lsm|asc|md5)$'

			if egrep -q '^\[CV-URL\] ' $pdir/$package.desc ; then
				url="`egrep '^\[CV-URL\] ' \
					$pdir/$package.desc | head -n 1 | \
					tr -s ' ' | cut -f2 -d' '`"
			fi

			if egrep -q '^\[CV-PAT\]' $pdir/$package.desc ; then
				pattern="`egrep '^\[CV-PAT\]' \
					$pdir/$package.desc | head -n 1 | \
					cut -s -f2- -d' '`"
			fi

			if egrep -q '^\[CV-DEL\]' $pdir/$package.desc ; then
				delpattern="`egrep '^\[CV-DEL\]' \
					$pdir/$package.desc | head -n 1 | \
					cut -s -f2- -d' ' | tr -s ' '`"
			fi

			echo -e "\n** `echo $x | cut -f3 -d' '`\n"
			echo "package='$package' pattern='$pattern'"
			echo -e "url='$url'\ndelpattern='$delpattern'"
			echo

			curl $curl_options -s "$url" | \
			tee checkver/debug.1 | \
			tr "\r\t\"'<>= /" '\n\n\n\n\n\n\n\n\n' | \
			tee checkver/debug.2 | \
			perl -e "while (<>) { chomp;
				\$fn=\$_; next unless /$pattern/;
				foreach \$x (qw/$delpattern/) { s/\$x//g; }
				print \"\$_\t\$fn\\n\" if \$_ ne ''; }" | \
			tee checkver/debug.3 | \
			perl -we '
	use strict;

	my ($a, $b);
	my %x;

	while (<>) {
		next unless /(\S+)\s+(\S+)/;
		$a=$1; $_=$1; $b=$2;
		s/(\d+)/sprintf("%020d", $1)/eg;
		# print "$_:$a:$b\n";
		if (not defined $x{$_}) {
			$x{$_}="$a\t$b";
		} else {
			$x{$_}.=" $b";
		}
	}

	foreach (reverse sort keys %x) {
		print "$x{$_}\n";
	}' | tee checkver/$package.new | \
			while read line ; do
				if [ ! -f checkver/$package.txt ] ||
				   ! grep -qx "$line" checkver/$package.txt
				then
					echo " * $line" | expand -t20
				else
					echo "   $line" | expand -t20
				fi
			done

		    } > checkver/$package.msg

		    if ! [ -s checkver/$package.new ] ; then
			echo "Got no list for package $package!" \
						>> checkver/$package.msg
			cat checkver/$package.msg ; echo
		    else
			if grep -q '^ \* ' checkver/$package.msg
			then cat checkver/$package.msg ; echo
			else rm checkver/$package.msg ; fi
		    fi

		    if [ -f checkver/$package.msg ] ; then
			echo >> checkver/$package.msg
		    fi
		fi
	else
		echo "ERROR: Package $package not found!"
	fi
done