#!/bin/bash

# Quick and dirty script to check all the files that should be changed
# before tagging a new version in CVS


function validtag {
    tag=$1
    [ "$tag" = "OK" ] && return 1
    test=`echo "$tag" | sed -r -e 's/[0-9]+\.[0-9]+\.[0-9]+/OK/'`
    if [ "$test" = "OK" ]; then
	return 0
    fi
    return 1
}

function tagnamecvs {
    tag="$1"
    echo $tag | sed -e 's/^/asap-/;s/\./-/g'
}

function cvsexistingtags {
    cvs status -v Asap/config.mk \
	| grep -A1000 'Existing Tags' \
	| awk '{print $1}' | grep -v 'Existing' \
	| xargs
}

function validtagcvs {
    tagcvs=$1
    listtags="$2"
    for i in $listtags; do
	if [ "$tagcvs" = "$i" ]; then
	    return 1
	fi
    done
    return 0
}

function checkconfigmk {
    tag="$1"
    versionexpect=`grep VERSION Asap/config.mk | sed -e 's/^.*:= *//'`
    if [ "$tag" = "$versionexpect" ]; then
	return 0
    fi
    return 1
}

function checkasapspec {
    tag="$1"
    versionexpect=`grep '^%define *ver' Asap/asap.spec | awk '{print $3}'`
    if [ "$tag" = "$versionexpect" ]; then
	return 0
    fi
    return 1
}

function checkchangelog {
    # TODO ChangeLog browsing should be made slightly more robust...
    tag="$1"
    versionexpect=`head -3 ChangeLog | tail -1 \
	| grep 'Version.*released' | awk '{print $3}'`
    if [ "$tag" = "$versionexpect" ]; then
	return 0
    fi
    return 1
}

function checkcvsfile {
    file=$1
    test=`cvs status $file | grep 'Up-to-date'`
    if [ "$test" = "" ]; then
	return 1
    else
	return 0
    fi
}

function checkcvsallfiles {
    ret=0
   if ! checkcvsfile ChangeLog; then
	echo "File ChangeLog is not sync with CVS"
	ret=1
    fi
    if ! checkcvsfile Asap/asap.spec; then
	echo "File Asap/asap.spec is not sync with CVS"
	ret=1
    fi
    if ! checkcvsfile Asap/config.mk; then
	echo "File Asap/config.mk is not sync with CVS"
	ret=1
    fi
    return $ret
}

function usage {
    echo "Usage: $0 [-h] [-d] [-t <asap_version_number>]"
    echo "    -t  tag asap release according to argument e.g. -t 1.2.3"
    echo "    -n  dummy mode: show what would be done, but don't do anything"
    echo "    -d  print debug information (implies -n)"
    echo "    -h  this help message"
    exit 1
}

# Parse script's command line arguments
tag=""
dummy=0
debug=0
while getopts "ndht:" flag
do
    case "$flag" in
	n) dummy=1;;
	d) dummy=1; debug=1;;
	t) tag="$OPTARG";;
        h|?) usage;;
    esac
done

if [ "$tag" = "" ] || ! validtag $tag; then
    echo "Invalid tag format. Tag should be of the form <number>.<number>.<number>"
    usage
fi

tagcvs=`tagnamecvs $tag`
listtags=`cvsexistingtags`

if validtagcvs $tagcvs "$listtags"; then
    [ "$debug" = "1" ] && echo "$tagcvs is a valid CVS tag"
else
    echo "CVS tag $tagcvs is already used"
    exit 1
fi

if checkconfigmk $tag; then
    [ "$debug" = "1" ] && echo "$tag is correctly entered in config.mk"
else
    echo "$tag is not correctly entered in config.mk"
    exit 1
fi

if checkasapspec $tag; then
    [ "$debug" = "1" ] && echo "$tag is correctly entered in asap.spec"
else
    echo "$tag is not correctly entered in asap.spec"
    exit 1
fi

if checkchangelog $tag; then
    [ "$debug" = "1" ] && echo "$tag is correctly entered in ChangeLog"
else
    echo "$tag is not correctly entered in ChangeLog"
    exit 1
fi

if checkcvsallfiles; then
    [ "$debug" = "1" ] && echo "All files are correctly committed to CVS"
else
    echo "Sync the above files with CVS first"
    exit 1
fi

echo "Will tag the ASAP source according to version $tag, CVS tag will be $tagcvs"
if [ "$dummy" = 1 ]; then
    echo cvs tag $tagcvs "(dummy mode)"
else
    echo cvs tag $tagcvs
    cvs tag $tagcvs
fi
