#!/bin/bash 
patch=
verbose=0
update=0
changes=0
srcdir=.

# --------------------------------------------------------------------
usage() 
{
    cat<<EOF
Usage: $0 [OPTIONS] PATCH_LEVEL

Options:

        -h,--help       Show this help 
        -v,--verbose    Increase verbosity (may be given multiple times)
        -u,--update     Update only (do not make list of changes) 
        -c,--changes    Make changes only
        -s,--srcdir DIR Where to new sources are (run pre-script from there)    

This script will make 4 files:  

   added-VERSION-PATCH_LEVEL.tar.gz    Archive of added files 
   removed-VERSION-PATCH_LEVEL.list    List of files removed 
   changes-VERSION-PATCH_LEVEL.patch   Patch of changed files 
   summary-VERSION-PATCH_LEVEL         List of all changes 

where VERSION is the upstream version number of ROOT, and PATCH_LEVEL is 
the patch number for this version.  Note, that the script uses CVS to get  
the information.  

EOF
}  

# ____________________________________________________________________
message()
{
    opt=
    post=
    while test $# -gt 0 ; do 
        case $1 in 
            -n) post=" ..." ; opt="$opt $1" ;;
            -*) opt="$opt $1" ;; 
            *)  break;;
        esac
        shift
    done
    echo $opt "$@${post}"
}

# ____________________________________________________________________
check_retval()
{
    retval=$?
    if test $# -gt 0 ; then 
        message -n $@ ":"
    fi
    if test $retval -ne 0 ; then 
        echo " Failure"
        cd $savdir
        exit $retval
    else
        echo " OK"
    fi
}

# -------------------------------------------------------------------- 
while test $# -gt 0 ; do  
    case $1 in 
        -h|--help)    usage ; exit 0 ;;  
        -v|--verbose) let verbose=$verbose+1 ;; 
        -u|--update)  update=1 ;; 
        -c|--changes) changes=1 ;;
        -s|--srcdir)  srcdir=$2 ; shift ;; 
        *)            patch=$1 ;;
    esac
    shift
done

# --------------------------------------------------------------------
if test $update -lt 1 && test "x$patch" = "x" ; then 
    echo "Please specify a patch level" 
    exit 1
fi

# --------------------------------------------------------------------
# Get version number 
if test ! -f build/version_number ; then 
    echo "Couldn't find build/version_number - are you sure you're in "
    echo "the source directory of ROOT?" 
    exit 1
fi

# Remove some stuff we don't want lying around 
rm -f cint/lib/posix/a.out
rm -f cint/include/ipc.so.5.15
rm -f cint/include/posix.so.5.15
rm -f cint/include/stdcxxfunc.so.5.15
rm -f cint/include/stdfunc.so.5.15
if test $update -gt 0 ; then 
    rm -rf math/unuran/src/unuran-*-root
fi

tmp="../list"
#msg 1 -n "Updating from CVS ... "
#cvs -q -z3 update -A -P -d > $tmp 2>&1 
message "Possibly update sources $update"
if test $update -gt 0 ; then
    message -n "Updating from SVN $update"
    svn status -u > $tmp 2>&1
    svn update -q 
    check_retval 
fi

# Purge directory, but do not setup. 
${srcdir}/build/package/lib/makedebdir.sh --purge --no-setup --leave-old

# --------------------------------------------------------------------
if test $changes -lt 1 ; then 
    echo "Update done"
    exit 0
fi

# --------------------------------------------------------------------
#
# Copy a roofit header file over
#
# cp ../extra/RooNumIntConfig.h roofit/inc/

# --------------------------------------------------------------------
#
# Move some files
#
#for i in inc/TFileMerger.h      src/TFileMerger.cxx \
#         inc/TProofOutputFile.h src/TProofOutputFile.cxx ; do 
#    test -f proof/proofplayer/$i && mv proof/proofplayer/$i tree/treeplayer/$i
#done
for i in inc/TXMLEngine.h src/TXMLEngine.cxx ; do 
    test -f io/xml/$i && mv io/xml/$i io/io/$i
done
#for i in inc/TGeoOverlap.h src/TGeoOverlap.cxx ; do
#    inc/TGeoTrack. src/TGeoTrack.cxx ; do 
#    test -f geom/geompainter/$i && mv geom/geompainter/$i geom/geom/$i
#done
# we need a new change list
svn status -u > $tmp 2>&1


# --------------------------------------------------------------------
# Get version number 
version=`cat build/version_number | tr '/' '.'`-$patch 

# --------------------------------------------------------------------
# Get changes 
added=`grep "^\? " $tmp | sed -e 's/^? *[0-9]* *//' -e 's/config.log//'` 
#removed=`grep "lost" $tmp | sed 's/cvs update: warning: \(.*\) was lost/\1/'`
removed=`grep "^\!" $tmp | sed 's/^! *[0-9]* *//'`
modified=`grep "^\M " $tmp | sed 's/^M *[0-9]* *//'` 
conflicts=`grep "^\C " $tmp | sed 's/^C *[0-9]* *//'` 


# --------------------------------------------------------------------
# Remove old package files 
for i in build/package/*/root-{bin,doc,common,xrootd,rootd,proofd}* ; do 
    if test ! -f $i ; then continue ; fi 
    echo -n "Removing old package file $i" 
    removed="$removed $i"
    rm $i 
    echo done
done


# --------------------------------------------------------------------
# Make summary 
message -n "Making a summary summary-${version} of all changes"
rm -f ../summary-${version}
echo "Added files: " > ../summary-${version}
newadded=
for i in $added ; do 
    case $i in
        config.log) continue ;;
        */G__*) continue ;;
        */*.d) continue ;;
        */*.dll) continue ;;
        */*.so*) continue ;;
        */*.o)   continue ;;
        cint/include/*.so.*) continue ;;
    esac
    echo -e "\t$i" >> ../summary-${version}
    newadded="$newadded $i"
done 
added=$newadded

# output Removed files 
echo "Removed files: " >> ../summary-${version}
for i in $removed ; do 
    case $i in 
        fonts/*) continue ;;
        net/xrootd/src/*.tgz) continue ;;
        graf2d/asimage/src/*.tar.gz) continue ;;
        math/unuran/src/*.tar.gz) continue ;;
    esac
    echo -e "\t$i" >> ../summary-${version}
done 

# output Modified files
echo "Modified files: " >> ../summary-${version}
for i in $modified ; do 
    echo -e "\t$i" >> ../summary-${version}
done 

# output files with conflicts
echo "Conflicts files: " >> ../summary-${version}
ret=0
for i in $conflicts ; do 
    echo -e "\t$i" >> ../summary-${version}
    echo "Warning: Conflicts found in $i" 
    false
done 
check_retval  

# --------------------------------------------------------------------
# Make a tar-ball of added files
if test "x$added" != "x" ; then 
    message -n "Creating tar-ball added-${version}.tar.gz of added files"
    rm -f ../added-${version}.tar.gz
    tar -czf ../added-${version}.tar.gz $added
    check_retval 
fi

# --------------------------------------------------------------------
# Make a patch of changes 
message -n "Creating patch changes-${version}.patch of changes" 
rm -f ../changes-${version}.patch
# cvs diff -u -N > ../changes-${version}.patch 2> /dev/null
svn diff -x -u > ../changes-${version}.patch 2> /dev/null 
check_retval
# result 1

# --------------------------------------------------------------------
# Making a list of removed files  
message -n "Creating list removed-${version}.list of removed files"
rm -f removed${version}.list
for i in $removed ; do 
    case $i in 
        fonts/*) continue ;;
        net/xrootd/src/*.tgz) continue ;;
        graf2d/asimage/src/*.tar.gz) continue ;;
        math/unuran/src/*.tar.gz) continue ;;
    esac
    echo -e "$i" >> ../removed-${version}.list
done 
check_retval 

# rm -f $tmp

# --------------------------------------------------------------------
# EOF
#