#!/bin/sh

if [ $# -lt 1  -o $# -gt 3 ]; then
    echo "Usage: $0 sourcedir [workdir] [vendor]" >&2
    echo "    if workdir is unspecified then it is derived from the" >&2
    echo "    sourcedir name. This is also the repository name if no" >&2
    echo "    existing workdir is found" >&2
    echo "    if vendor is unspecified then it tries to figure this out" >&2
    echo "    by looking at the repository/configure file." >&2
    exit 1
fi
dir=$1
name=$2
vendor=$3
workbasedir=`pwd`
fullname=`basename $dir`
ver=`echo $fullname | sed -e 's/\./_/g'`
if [ -z "$name" ]; then
  name=`echo $fullname | sed -e 's/[-_].*//'`
fi
if [ -z "$name" ]; then
  echo "ERROR! Can't figure out which CVS name to use from $fullname" >&2
  exit 1
fi
rmdir $name 2>/dev/null
if [ -d $name ]; then
    oldtag=`cat $name/CVS/Tag | cut -c2-`
    if [ -z "$oldtag" ]; then
	echo "ERROR! There is a foreign directory named $name" >&2
	echo "Please clean this up before running this command" >&2
	exit 1
    fi
else
    oldtag=""
fi
if [ -z "$vendor" -a -f $name/configure ]; then
    vendor=`cvs log -h $name/configure | sed -n -e '/^[	 ][	 ]*\(.*\): 1.1.1$/ s//\1/p'`
fi
if [ -z "$vendor" ]; then
    echo "Can't determine source vendor tag"
    echo -n "Vendor tag: "
    read vendor
fi
if [ -z "$vendor" ]; then
    vendor=OTHER
fi
if [ -f $name/CVS/Repository ]; then
    repository=`cat $name/CVS/Repository`
    if [ -n "$CVSROOT" ]; then
	repository=`echo $repository | sed -e "s%^$CVSROOT/*%%"`
    fi
else
    repository=$name
fi
if [ ! -d $dir ]; then
    echo "ERROR! Can't find $dir"
    exit 1
fi

# Ask if we should join local changes on first checkout
join="";
while [ -n "$oldtag" ]; do
    echo -n "Join local changes from $oldtag?"
    read ans
    case "$ans" in
    y*|Y*)
	join="-j $oldtag"
	break # done
	;;
    n*|N*)
    	join=""
	break # done
	;;
    *|"")
    	cvs rdiff -r $oldtag $repository | more
	;;
    esac
done

cat <<EOM

About to import $dir into $repository

Repository  : $repository
Directory   : $name
Source      : $dir
Vendor      : $vendor
Version tag : $ver
Local tag   : $ver-local
Patch tag   : $ver-local-lastpatch
Join changes: ${join:-no}

EOM
echo -n "Press enter to continue"
read junk

# Import new version
( cd $dir
  cvs import -m "Imported $fullname" -b1.1.1 $repository $vendor $ver
)

# Create local branch and supporting tags
cvs rtag -F -b -r "${ver}" "${ver}-local" $repository
cvs rtag -F -r "${ver}-local" "${ver}-local-lastpatch" $repository

# Move the old working directory to cvsold
if [ -d $name ]; then
    mkdir -p cvsold
    cp -p $name/config.cache $oldtag.config.cache 2>/dev/null
    cp -p $name/config.status $oldtag.config.status 2>/dev/null
    mv $name cvsold/$oldtag
    if [ -d $name ]; then
      mv $name cvsold/$oldtag.`date +%Y%m%d%H%M` 2>/dev/null
    fi
fi

# Checkout a new working directory
mkdir -p $name
cvs checkout -r "${ver}-local" -d $name $join $repository 

# Make the local branch the default
# can't do. cvs import gets upset by this. CVS 1.9 bug?
#( cd $name
#  cvs admin -b"${ver}-local" .
#)

# Run configure
mv $oldtag.config.cache $name/config.cache 2>/dev/null
mv $oldtag.config.status $name/config.status 2>/dev/null
cd $name
if [ -f config.status ]; then
  sh -c "./config.status --recheck ; ./config.status"
fi