#!/bin/bash
#                               -*- Mode: Sh -*- 
# cvs-pkg-upgrade --- 
# Author           : Marcelo E. Magallon" <mmagallo@efis.ucr.ac.cr>
# Created On       : Thu Apr 3 13:34:54 1999
# Created On Node  : efis.ucr.ac.cr
# Last Modified By : Manoj Srivastava
# Last Modified On : Thu May 13 16:39:07 1999
# Last Machine Used: glaurung.green-gryphon.com
# Update Count     : 13
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
# 



set -eC

files_to_remove=

trap '${verbose} Removing ${files_to_remove} >&2 ; rm -f ${files_to_remove}' \
    EXIT SIGHUP SIGINT SIGQUIT SIGTERM

progname="`basename \"$0\"`"
pversion='0.1'

usage () {
        cat >&2 <<EOU
Usage:
  $progname [options] <package name> <previous version> <current version>

Options: 
  -h           print this message
  -M<module>   CVS module name
  -R<root dir> Root directory.
  -W<work dir> Working directory.    
  -d<digit>    Set Debug Level
  -n           "Dry-run" mode - No action taken, only print commands.
  -v           make the command verbose
  -x<prefix>   CVS default module prefix.
 
 Sources for both the old and the new versions are expected in the
 files 
 <workdir>/<package name>/<package name>_<version>.orig.tar.gz
 and should unpack into a single subdir.
EOU
}

action=
verbose=:
DEBUG=0

while [ $# != 0 ]; do
    value="`echo x\"$1\" | sed -e 's/^x-.//'`"
    case "$1" in
        -h)  usage; exit 0                 ;;
        -M*) opt_cvsmodule="$value"        ;;
        -R*) opt_rootdir="$value"          ;;
        -W*) opt_workdir="$value"          ;;
        -d*) opt_DEBUG="$value"            ;;
        -n)  action=: ;  docmd='NO'        ;;
        -v)  verbose=echo;   VERBOSE=1     ;;
        -x*) opt_prefix="$value"           ;;
        -*) echo >&2 "Unknown option $1."
            usage; exit 1                  ;;
        *)  break                          ;;
    esac
    shift
done

if [ $# -ne 3 ]; then
    echo -e >&2 "${progname}: package name, previous version and upstream versio
n requiered.\n"
    usage;
    exit 1;
fi

package=$1

pre_version=$2
cur_version=$3

if dpkg --compare-versions $2 lt $3; then 
    ${verbose} Version test passed;
else
    echo Older version $2 greater than newer version $3
    echo This can break CVS badly. Aborting
    exit 1
fi

# get the package name
rootdir='/usr/local/src/Packages'

# Load site defaults and over rides.
if [ -f /etc/cvsdeb.conf ]; then
    . /etc/cvsdeb.conf
fi

# Load user defaults and over rides.
if [ -f ~/.cvsdeb.conf ]; then
    . ~/.cvsdeb.conf
fi

# If asked to debug on command line, that's what we get
if [ ! "x$opt_DEBUG" = "x" ]; then
    DEBUG="$opt_DEBUG"
fi

if [ "$DEBUG" -gt 0 ]; then
    CVS_QUIET=""
    TAR_QUIET="vv"
fi
# Command line, env variable, config file, or default
if [ ! "x$opt_rootdir" = "x" ]; then
    rootdir="$opt_rootdir"
elif [ ! "x$CVSDEB_ROOTDIR" = "x" ]; then
    rootdir="$CVSDEB_ROOTDIR"
elif [ ! "x$conf_rootdir" = "x" ]; then
    rootdir="$conf_rootdir"
fi

# Command line, env variable, config file, or default
if [ ! "x$opt_workdir" = "x" ]; then
    workdir="$opt_workdir"
elif [ ! "x$CVSDEB_WORKDIR" = "x" ]; then
    workdir="$CVSDEB_WORKDIR"
elif [ ! "x$conf_workdir" = "x" ]; then
    workdir="$conf_workdir"
else
    workdir="$rootdir/$package"
fi

# Command line, env variable, config file, or default
if [ ! "x$opt_prefix" = "x" ]; then
    prefix="$opt_prefix"
elif [ ! "x$CVSDEB_PREFIX" = "x" ]; then
    prefix="$CVSDEB_PREFIX"
elif [ ! "x$conf_prefix" = "x" ]; then
    prefix="$conf_prefix"
else
    prefix=""
fi

# put a slash at the end of the prefix
if [ "X$prefix" != "X" ]; then
    prefix="$prefix/";
    prefix=`echo $prefix | sed 's://:/:g'`;
fi

# Command line, env variable, or default
if [ ! "x$opt_cvsmodule" = "x" ]; then
    cvsmodule="$opt_cvsmodule"
elif [ ! "x$CVSDEB_MODULE" = "x" ]; then
    cvsmodule="$CVSDEB_MODULE"
else
    cvsmodule="${prefix}$package"
fi

# Command line, env variable, config file, or default
if [ ! "x$opt_packageintag" = "x" ]; then
    packageintag="$opt_packageintag"
elif [ ! "x$CVSDEB_PACKAGEINTAG" = "x" ]; then
    packageintag="$CVSDEB_PACKAGEINTAG"
elif [ ! "x$conf_packageintag" = "x" ]; then
    packageintag="$conf_packageintag"
else
    packageintag=""
fi

if [ "x$packageintag" != x ]; then
    cvstag="${package}_$cvstag"
    cvs_upstream_tag="${package}_$cvs_upstream_tag"
fi

# env variable, or default
if [ ! "x$CVSDEB_IMPORTSUBSTMODE" = "x" ]; then
    importsubstmode="$CVSDEB_IMPORTSUBSTMODE"
else
    importsubstmode="-ko"
fi


# No user defined changes here

pre_src_file="${workdir}/${package}_${pre_version}.orig.tar.gz"
cur_src_file="${workdir}/${package}_${cur_version}.orig.tar.gz"

for file in ${pre_src_file} ${cur_src_file} ; do
    if [ ! -f "${file}" ] ; then
        echo >&2 "${progname}: ${file} does not exist!"
        exit 1
    fi
done

build_list() {
    # extract the list of files contained in the tarball stripping
    # the first directory from the names and ommitting directories
    tar tvzf $1 | 
        sed -n -e 's:^.* [^/]*/::' -e '/\(^\|\/\)$/! p' | 
        sort
}

update_cvs() {
    diff -u $1 $2 |    # diffs both listings
    tail +3       |    # removes the diff header
    grep '^[-+]'  |    # trims unchanged stuff
    sed -e 's:^+:cvs add :' -e 's:^-:cvs delete -fR :' #| sh -s
}

pre_src_list=$(basename ${pre_src_file} .tar.gz).list.$$
files_to_remove="${files_to_remove} ${pre_src_list}"
${verbose} build_list ${pre_src_file} \> ${pre_src_list} >&2
${action}  build_list ${pre_src_file}  > ${pre_src_list}

cur_src_list=$(basename ${cur_src_file} .tar.gz).list.$$
files_to_remove="${files_to_remove} ${cur_src_list}"
${verbose} build_list ${cur_src_file} \> ${cur_src_list} >&2
${action}  build_list ${cur_src_file}  > ${cur_src_list}

update_cvs ${pre_src_list} ${cur_src_list}


exit 0
