#!/bin/bash

#-----------------------------------------------------------
# $Id$
#-----------------------------------------------------------

file_sed_src="${MORPHEO_TOPLEVEL}/IPs/systemC/processor/Morpheo/Behavioural/include/Version.h.sed";
file_sed_dest="${MORPHEO_TOPLEVEL}/IPs/systemC/processor/Morpheo/Behavioural/include/Version.h";
file_sed_script="/tmp/file_sed_script";

#-----[ usage ]---------------------------------------------
function usage ()
{
    echo "Usage           : ${0} action";
    echo "Arguments       : ";
    echo " * action";     
    echo "   * add        : add all file unpresent in project and set proprity";
    echo "   * commit     : update revision number and commit";
    echo "   * status     : print only locally modified items";
    echo "   * status_new : print only locally new      items";
    echo "   * help       : print this message";
    echo "Note            :";
    echo " * Morpheo's environnement must be positionned.";
    exit;
}

#-----[ main ]----------------------------------------------
function main ()
{
    # Test operand
    if test ${#} -ne 1; then
	usage ${*};
    fi;

    if test -z ${MORPHEO_TOPLEVEL}; then
	usage ${*};
    fi;

    export LC_ALL=C;
    export EDITOR="emacs";

    pwd=${PWD};
    
    case ${1} in
	"commit")
	    cd ${MORPHEO_TOPLEVEL};
    
	    # +1 because is the next revision
	    revision=$(($(export LC_ALL=C; svnversion  | tr -d [:alpha:] | cut -d : -f 2) + 1));
            date_day=$(date +%d);
            date_month=$(date +%m);
            date_year=$(date +%Y);
            
            echo "s/\"@REVISION\"/\"${revision}\"/"     >  ${file_sed_script};
            echo "s/\"@DATE_DAY\"/\"${date_day}\"/"     >> ${file_sed_script};
            echo "s/\"@DATE_MONTH\"/\"${date_month}\"/" >> ${file_sed_script};
            echo "s/\"@DATE_YEAR\"/\"${date_year}\"/"   >> ${file_sed_script};

	    sed -f ${file_sed_script} ${file_sed_src} &> ${file_sed_dest};

            rm ${file_sed_script};
            
	    svn commit;

	    cd ${pwd};
	    ;;

	"add")
	    cd ${MORPHEO_TOPLEVEL};

	    for i in $(svn status | grep '?'); do
		if test -e ${i}; then
		    svn add ${i};
		    svn propset svn:keywords "Id" ${i} -R
		fi;
	    done;

	    cd ${pwd};
	    ;;

	"status")
	    cd ${MORPHEO_TOPLEVEL};

	    svn status;

	    cd ${pwd};
	    ;;

	"status_new")
	    cd ${MORPHEO_TOPLEVEL};

	    svn status | grep '?';

	    cd ${pwd};
	    ;;
	    
	"help")
	    usage ${*};
	    ;;
	*)
	    usage ${*};
    esac
}

#-----[ Body ]----------------------------------------------
main ${*}
