#! /bin/bash
#                               -*- Mode: Sh -*- 
# cvs-upgrade --- 
# Author           : Manoj Srivastava ( srivasta@tiamat.datasync.com ) 
# Created On       : Fri Feb 20 15:17:09 1998
# Created On Node  : tiamat.datasync.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Fri May 14 23:04:55 1999
# Last Machine Used: glaurung.green-gryphon.com
# Update Count     : 90
# Status           : Unknown, Use with caution!
# HISTORY          : 
# Description      : 
# 
# 

# Make sure we abort on error
set -e

declare -a argv

progname="`basename \"$0\"`"
pversion='$Revision: 1.20 $'

setq() {
    # Variable Value Doc string
    if [ "x$2" = "x" ]; then
        echo >&2 "$progname: Unable to determine $3"
        exit 1;
    else
        if [ ! "x$Verbose" = "x" ]; then
            echo "$progname: $3 is $2";
        fi
        eval "$1=\"\$2\"";
    fi
}

withecho () {
        echo " $@" >&2
        "$@"
}

usageversion () {
        cat >&2 <<END
Usage: $progname  [options] <package name> <upstream version> <debian version>
Options: 
  -h           print this message
  -F           Force removal of CVS directories and symbolic links
               before import. This should be used with caution!!
  -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
  -ctp         Include <package>_ at start of CVS tag.
  -x<prefix>   CVS default module prefix.
 
 Files are unpacked in here while being upgraded. The new upstream
 sources are expected in the file
 <workdir>/<package name>/<package name>_<upstream version>.orig.tar.gz)
 and should unpack into a subdir.

Note that the current behaviour of cvs-upgrade is to ignore files that
match the default list of file name patterns to be ignored (this is
built into cvs); and that any .cvsignore files in the upstream sources
shall be honoured.

END
}

# The defaults
docmd='YES'
action='withecho'
tmpdir="temp-$progname"


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

# We need at lease two arguments
if [ $# -lt 2 ]; then
    echo >&2 " Require package name and upstream version"
    usageversion; 
    exit 1;
fi

package=$1
shift
upstream_version=$1
cvs_upstream_tag=`echo "upstream_version_$upstream_version" | tr . _ `
shift


if [ $# != 0 ]; then
    debian_version=$1
    cvstag=`echo "debian_version_${upstream_version}-$debian_version" | tr . _ `
else
    debian_version=""
    cvstag=`echo "debian_version_${upstream_version}" | tr . _ `
fi

CVS_QUIET="-Q"
TAR_QUIET=""

if [ $VERBOSE -gt 0 ]; then
    echo " *** Initializing ... ***"
fi

if [ "X$CVSROOT" = "X" ]; then
    echo "You should have set the CVSROOT env variable."  1>&2
    usageversion; 
    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_forceclean" = "x" ]; then
    forceclean="$opt_forceclean"
elif [ ! "x$CVSDEB_FORCECLEAN" = "x" ]; then
    forcecelan="$CVSDEB_FORCECLEAN"
elif [ ! "x$conf_forceclean" = "x" ]; then
    forceclean="$conf_forceclean"
else
    forceclean=""
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

# Command line, env variable, config file, or default
if [ ! "x$opt_use_changelog" = "x" ]; then
    use_changelog="$opt_use_changelog"
elif [ ! "x$CVSDEB_USE_CHANGELOG" = "x" ]; then
    use_changelog="$CVSDEB_USE_CHANGELOG"
elif [ ! "x$conf_use_changelog" = "x" ]; then
    use_changelog="$conf_use_changelog"
else
    use_changelog=""
fi
 

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


# No user defined changes here
pkgdir="$workdir/$package-$upstream_version"
orig_tarfile="$workdir/${package}_${upstream_version}.orig.tar.gz"

if [ ! -r $orig_tarfile ]; then
    echo >&2 "I could not find the expected file $orig_tarfile"
    echo >&2 "I parsed the package name=($package), version=${upstream_version}"
    if [ $debian_version ]; then
	echo >&2 "The debian revision seems to be  $debian_version"
    else
	echo >&2 "This seems to be a debian only package"
    fi
    echo >&2 "The working directory is $workdir"
    echo >&2 ""
    echo >&2 "Since something has gone wrong, I am aborting"
    exit 1
fi

echo "                                                                      "
echo "########################  Note   ########################             "
echo "                                                                      "
echo "# Please remember to incorporate the changes made upstream.           "
echo "# If you do not have a working directory, you may simply do the       "
echo "#     following manually:                                             " 
echo " cvs co -jsource-dist:yesterday -jsource-dist:today ${cvsmodule}      "
echo "# or                                                                  "
echo " cvs co -jupstream_version_<OLD-VERSION> -j$cvs_upstream_tag ${cvsmodule}"
echo "                                                                      "
echo "# If you already have a checked out working directory, please do:     "
echo " cd ${cvsmodule}                                                      "
echo " cvs update -d -jsource-dist:yesterday -jsource-dist:today            "
echo "# or                                                                  "
echo " cvs update -d -jupstream_version_<OLD-VERSION> -j$cvs_upstream_tag   "
echo "                                                                      "
echo "# If you have not already done so, you should also run cvs-co-upgrade "
echo "#  to see if you need to do anything about files that have been       "
echo "#  deleted in upstream sources                                        "
echo "cvs-co-upgrade ${package} <OLD-VERSION> $upstream_version             "
echo "                                                                      "
echo "# After that don't forget to commit and tag your newly created        "
echo "# revision as                                                         "
echo "                                                                      "
echo "       cd ${cvsmodule}                                                "
echo "       edit debian/changelog                                          "
echo "       cvs ci -m'New upstream source'                                 "
echo "       cvs $CVS_QUIET tag -F ${cvstag}                                "
echo "       cd ${workdir}                                                  "
echo "       rm -rf ${cvsmodule}                                            "
echo "                                                                      "
echo "##############  Starting automated tasks   ##############             "
echo "                                                                      "

if [ $VERBOSE -gt 0 ]; then
    echo ""
    echo " *** Preparing the working directory *** "
fi
if [ ! -d "$workdir" ]; then
    $action mkdir -p "$workdir"
fi
$action cd    "$workdir"
$action mkdir "$tmpdir"
if [ "X$docmd" = "XYES" ]; then
    if [ ! -d "$tmpdir" ]; then
	echo >&2 "The working directory ($workdir) is not writable"
	exit 1
    fi
fi

if [ $VERBOSE -gt 0 ]; then
    echo ""
    echo " *** Unpacking the new sources *** "
fi
eval "$action cd $tmpdir"
eval "$action tar ${TAR_QUIET}zfx ../${package}_${upstream_version}.orig.tar.gz"
if [ "X$docmd" = "XYES" ]; then
    numdirs=`ls -1 | wc -l`
    if [ $numdirs -gt 1 ]; then
	echo >&2 "Source unpacked into ore than one dir"
	cd ..
	rm -rf "$tmpdir"
	exit 1;
    fi
    $action cd `ls -1`
else
    echo "numdirs=\`ls -1 | wc -l\`"
    echo "if [ $numdirs gt 1 ]; then "
    echo "   echo >&2 Source unpacked into ore than one dir"
    echo "   cd ..; rm -rf $tmpdir"
    echo "   exit 1"
    echo "fi"
    echo "cd \`ls -1\`"
fi

clean_up() {
    if [ $VERBOSE -gt 0 ]; then
	echo ""
	echo "# Cleaning up"
    fi
    $action cd "${workdir}"
    if [ "X$docmd" = "XYES"  ]; then
	if [ -d "$tmpdir" ]; then
	    $action rm -rf "$tmpdir"
	fi
    else
	$action rm -rf "$tmpdir"
    fi
}

# check for symbolic links in the upstream sources
check_symlinks() {
    symbolic_links=$(find . -type l -print)
    if [ "X$symbolic_links" != "X" ]; then
	if [ "X$forceclean" != "X" ]; then
	    echo >&2 "You have asked me to remove symbolic links I find."
	    echo >&2 "since cvs import chokes on them. I have found"
	    echo >&2 "the following symbolic links:"
	    echo >&2 "$symbolic_links"
	    echo >&2 ""
	    while [ "X$ans" = "X" ]; do
		echo -n >&2 "Shall I remove the links?[y/n]"
		read ans;
		case $ans in
		    y*|Y*)
			$action rm -f $symbolic_links
			;;
		    *)
			echo >&2 "Please delete the following"
			echo >&2 "links and try again."
			echo >&2 "$symbolic_links"
			clean_up;
			exit 1
		esac
	    done
	else
	    echo >&2 "The upstream sources contain symbolic links."
	    echo >&2 "CVS ignores symblic links, you have to handle link "
	    echo >&2 "creation in the modules file (-e and -o args)"
	    echo >&2 "Since this causes an error on import, I can't."
	    echo >&2 "handle this. Please delete the following"
	    echo >&2 "links and try again."
	    echo >&2 "$symbolic_links"
	    clean_up;
	    exit 1
	fi
    fi
}

check_CVS_dirs() {
    CVS_dirs=$(find . -type d -name CVS -print)
    if [ "X$CVS_dirs" != "X" ]; then
	if [ "X$forceclean" != "X" ]; then
	    echo >&2 "You have asked me to remove CVS dirs I find."
	    echo >&2 "since cvs import chokes on them. I have found"
	    echo >&2 "the following dirs:"
	    echo >&2 "$CVS_dirs"
	    echo >&2 ""
	    while [ "X$ans" = "X" ]; do
		echo -n >&2 "Shall I remove the Directories?[y/n]"
		read ans;
		case $ans in
		    y*|Y*)
			$action rm -rf $CVS_dirs
			;;
		    *)
			echo >&2 "Please delete the following"
			echo >&2 "CVS directories and try again."
			echo >&2 "$CVS_dirs"
			clean_up;
			exit 1
		esac
	    done
	
	else
	    echo >&2 "The upstream sources contain CVS directories."
	    echo >&2 "This I can't handle. Please delete the following"
	    echo >&2 "directories and try again."
	    echo >&2 "$CVS_dirs"
	    clean_up;
	    exit 1
	fi
    fi
}
check_CVS_ignore() {
    CVS_ignores=$(find . -type f -name .cvsignore -print)
    if [ "X$CVS_ignores" != "X" ]; then
	if [ "X$forceclean" != "X" ]; then
	    echo >&2 "You have asked me to remove .cvsignore I find."
	    echo >&2 "since cvs import may be adversly affected.. "
	    echo >&2 "I have found the following dirfiles:"
	    echo >&2 "$CVS_ignores"
	    echo >&2 ""
	    while [ "X$ans" = "X" ]; do
		echo -n >&2 "Shall I remove the files?[y/n]"
		read ans;
		case $ans in
		    y*|Y*)
			$action rm -f $CVS_ignores
			;;
		    *)
			echo >&2 "Please delete the following"
			echo >&2 "CVS .ignore files and try again."
			echo >&2 "$CVS_ignores"
			clean_up;
			exit 1
		esac
	    done
	fi
    fi
}
 
if [ "X$docmd" = "XYES" ]; then
    check_symlinks;
    check_CVS_dirs;
    check_CVS_ignore;
fi

if [ $VERBOSE -gt 0 ]; then
    echo ""
    echo " *** Importing the new sources *** "
fi

if [ ! "X$use_changelog" = "X" -a -f debian/changelog ]; then
    changes=`dpkg-parsechangelog | grep '^  '`
fi


argv=(cvs $CVS_QUIET import ${importsubstmode} -m"Initial Import. $changes" "${cvsmodule}" source-dist "${cvs_upstream_tag}")
$action "${argv[@]}"
$action cd ../..
$action rm -rf "$tmpdir"

echo "# Don't forget cvs-co-upgrade $cvsmodule <OLD-VERSION> $upstream_version"

exit 0
