#!/bin/sh -e

# Copy changed files after building a package to a CVS tree:
# 1. changes cvs_base below
# 2. build the package
# 3. call this script with the .difff.gz as parameter
# 4. commit and tag the changes into CVS before uploading!
#
# Written by Stefan Gybas <sgybas@debian.org>. Released under the GPL!

# Base directory of your checked out CVS tree
cvs_base=$HOME/debian/cvs

###
### DON'T CHANGE ANYTHING BELOW THIS LINE! (unless you know what you're doing)
###

error() {
    echo $1 >&2
    exit 1
}

# Check arguments
diff_gz=$1
[ -z "${diff_gz}" ] && error "usage: `basename $0` <package.diff.gz>"

source_package=`echo ${diff_gz} | cut -d_ -f1`
source_version=`echo ${diff_gz} | cut -d_ -f2- | sed -e s/\.diff\.gz//`
cvstag_version=`echo ${source_version} | tr . _`
upstream_version=`echo ${source_version} | cut -f1 -d\-`
extracted_dir=${source_package}-${upstream_version}

#echo "DEBUG: source_package   = $source_package"
#echo "DEBUG: source_version   = $source_version"
#echo "DEBUG: upstream_version = $upstream_version"

# Check .diff.gz, extracted directory and destination directory
[ -r "${diff_gz}" ] || error "E: ${diff_gz} is not readable!"
[ -r "${extracted_dir}/debian/control" ] || error "E: ${extracted_dir}/debian/control is not readable!"
[ -d "${cvs_base}/${source_package}/CVS" ] || error "E: ${cvs_base}/${source_package}/CVS does not exist!"

echo "Copying files:"
zgrep "^+++ " $diff_gz | cut -d/ -f2- | xargs tar -C ${extracted_dir} -c | (cd ${cvs_base}/${source_package}; tar -x -v)
echo "Modified Debian files have been copied to ${cvs_base}/${source_package}."
echo "Please tag this release as RELEASE_${cvstag_version} before uploading!"

