#!/bin/sh
# Teszt sajat gepen: wikicredat 'Kottát_és_midifájlt_tartalmazó_szócikkek' http://localhost/proba

if [ -z "$1" ]
then	echo Hivas: `basename $0` kategoria \[ wiki \] \[ logfile \]
	echo \"wiki\" default erteke https://hu.wikipedia.org/w
	echo \"logfile\" vegere irodik wget hibakimenete
	echo Peldak:
	echo `basename $0` Aminok
	echo `basename $0` Wiki http://localhost/unix
	echo `basename $0` Aminok \'\' /dev/null
	exit 1
fi

MUNKA=/tmp/`basename $0`.wget.$$
EREDM=/tmp/`basename $0`.out.$$
rm -f $EREDM
URL="${2:-https://hu.wikipedia.org/w}/api.php?action=query&prop=revisions&titles=`urlek "$1"`&rvprop=timestamp|user&rvlimit=max&format=xml"

FOLYT=""
while true
do
	wget -t3 -T20 "$URL$FOLYT" -a ${3:-/dev/stderr} -O- | sed -e 's@/>@&\n@g' -e 's/<rev /\n&/g' -e '/^\r*$/d' | tee $MUNKA | grep '^<rev ' >>$EREDM
	FOLYT=`sed -n -e '/rvcontinue=/!d' -e 's/^.*rvcontinue="//' -e 's/".*$//p' $MUNKA`	# nem ures: van folytatolap
	if [ -z "$FOLYT" ] ; then break ; fi
	#echo '***' >>$EREDM
	FOLYT="&rvcontinue="$FOLYT
done
#sed -n 's/^.*\(user="[^"]*"\).*\(timestamp="[^"]*"\).*$/\1 \2/p' $EREDM | sort -t = -k3 | head -1 >$MUNKA
sed -n 's/^.*\(user="[^"]*"\).*\(timestamp="[^"]*"\).*$/\1 \2/p' $EREDM | sort -t = -k3 >$MUNKA
CREUSR=`head -1 $MUNKA | logcut -d '"' -f 2 | tr ' ' _`
CREDAT=`head -1 $MUNKA | logcut -d '"' -f 4`
MODUSR=`tail -1 $MUNKA | logcut -d '"' -f 2 | tr ' ' _`
MODDAT=`tail -1 $MUNKA | logcut -d '"' -f 4`
echo `date -d "$CREDAT" +%Y-%m-%d.%H:%M:%S` $CREUSR `date -d "$MODDAT" +%Y-%m-%d.%H:%M:%S` $MODUSR 
rm -f $MUNKA $EREDM
