#!/bin/bash

# Skript that updates src packages of the Bygfoot Football Manager
# using wget to retrieve patches from the homepage

# variables we sometimes read something into
tempvar=

# the directory containing the Bygfoot src package
srcdir=$PWD

# a directory we can write to and read from
tmpdir=/tmp

# the Bygfoot package type, stable or unstable
type=

# the current revision number of the user's package
revnr=

# number of new revisions found
found=0

function get_revision()
{
    local query=
    local updatedir=$PWD

    echo
    echo "** b-u get_revision: Retrieving revision_$type$tempvar.tar.bz2"
    wget http://bygfoot.sourceforge.net/revisions/$type/revision_$type$tempvar.tar.bz2

    mkdir revision_$type$tempvar
    cd revision_$type$tempvar
    tar xfj ../revision_$type$tempvar.tar.bz2
    echo "** b-u get_revision: Here's the revision description:"
    echo "##############"
    cat README
    echo "##############"
    echo
    echo "** b-u get_revision: Would you like to apply the patch?"
    echo "** b-u get_revision: (You can't apply newer ones if you don't apply"
    echo "** b-u get_revision: this one because they depend on each other): ([y]/n)"

    read query

    if [ ! -z $query ]; then
	if [ $query != "y" ]; then
	    exit
	fi
    fi
    
    echo
    echo "** b-u get_revision: Applying patch revision_$type$tempvar"
    echo

    cd $srcdir
    
    if [ -e $updatedir/install.sh ]; then
	$updatedir/install.sh
    fi

    patch -p1 < $updatedir/revision_$type$tempvar/patch*
    cd $updatedir
    
    echo
    echo "** b-u get_revision: done."
}

echo "** bygfoot-update: bygfoot-update v0.1"
echo "** by Gyz Both (gyboth@bygfoot.com)"
echo
echo "** b-u: I'm going to need the program wget,"
echo "** b-u: so if you don't have it, press Ctrl-C"
echo "** b-u: to quit and install it."
echo
echo "** b-u: This script doesn't contain tests whether things"
echo "** b-u: like tar, bzip2, sed etc. work and whether you"
echo "** b-u: have appropriate permissions, so if you notice"
echo "** b-u: that something doesn't work out try to find out"
echo "** b-u: what it is and correct the problem. Or contact me."
echo
echo "** b-u: Let's go:"
echo
echo "** b-u: Enter the directory containing your current Bygfoot src"
echo "** b-u: package (e.g. (/home/john/compiled/bygfoot-1.6.1):"
echo "** b-u: [default: $PWD]"

read tempvar

if [ ! -z $tempvar ]; then
    srcdir=$tempvar
fi

echo "** b-u: Enter temp directory I can use"
echo "** b-u: (you need read/write permissions there): [/tmp]"

read tempvar

if [ ! -z $tempvar ]; then
    tmpdir=$tempvar
fi

if [ ! -e $srcdir/revision_number ]; then
    echo "** b-u: I couldn't find out your revision. Perhaps your Bygfoot"
    echo "** b-u: version's too old. If you have 1.6.1 or 1.7.1 or higher"
    echo "** b-u: (not 1.7.0 though) please write me."
    exit
fi

# find out whether the package type is stable or unstable
type=$(cat $srcdir/revision_number | sed s/[0-9]*//g)

if [ $type != "stable" -a $type != "unstable" ]; then
    echo "** b-u: Couldn't find out your package type." 
    echo "** b-u: Enter type manually (depends on the middle number"
    echo "** b-u: of your package, 1.X.0 is stable if X is even,"
    echo "** b-u: unstable if X is odd): ([stable]/unstable)"
    read type
    
    if [ -z $type ]; then
	type=stable
    fi
fi

revnr=$(cat $srcdir/revision_number | sed s/[a-z]*//g)


echo "** b-u: Changing to the temp directory and retrieving"
echo "** b-u: the revisions list."
echo

cd $tmpdir

rm -rfv bygfoot-update
mkdir -v bygfoot-update
cd bygfoot-update
echo
wget -v http://bygfoot.sourceforge.net/revisions/$type/revisions_$type

for i in $(cat revisions_$type); do
    echo
    tempvar=$(echo $i | sed s/'[a-z_]*\([0-9]*\).*'/'\1'/g)
    if [ $tempvar -gt $revnr ]; then
	found=$[found + 1]
	get_revision
    fi
done

echo
echo "** b-u: Cleaning up in the temp directory."
rm -rf $tmpdir/bygfoot-update

if [ $found -ge 1 ]; then
    echo "** b-u: $found new revisions found."
    echo "** b-u: Your new revision number should be $[revnr + found]."
    echo "** b-u: Have a look at the files README, ChangeLog and"
    echo "** b-u: ReleaseNotes to see the changes that were made."
else
    echo "** b-u: No new revisions found. Your package seems"
    echo "** b-u: to be up-to-date."
fi

echo
echo "** b-u: done."
