#! /bin/bash

# This is cvs2dist
# Webpage: http://www.katzbrown.com/shiritsu/programming/cvs2dist/
# Newest version is always available there!
# Please report bugs to <jason@katzbrown.com>.

# Original author (of cvs2pack.sh) was Sebastian Stein <seb.stein@hpfsc.de>
# Heavy, heavy modifications by Jason Katz-Brown <jason@katzbrown.com>
# Some modifications for i18n inclusion by Dominique Devriese <devriese@kde.org>
# License: GPL (http://www.gnu.org/)
# Last modification: 2003/1/20

cmdline="$@"

excludes=""
includes=""
returndir=`pwd`
override="README ChangeLog INSTALL AUTHORS AUTHOR COPYING"
remove="config.cache config.log config.status Makefile configure inst-apps CVS acinclude.m4 
  aclocal.m4 config.h 
  config.h.bot config.h.in configure.files libtool stamp-h stamp-h.in stamp-h1 
  subdirs *.moc *.la .libs .deps .cvsignore autom4te.cache {arch} .arch-ids
  "
  
toplevelremove="configure.in.bot *.kdevelop *.pcs *.kdevses *.tar.gz  Doxyfile"

test -e ~/.cvs2distrc && extraoptions=`cat ~/.cvs2distrc`

# getopt usage from the getopt bash example
# --log has optional argument
TEMP=`getopt \
-o i:x:v:n:r:e:a:B:dhmbgol \
--long includes:,excludes:,log::,version:,name:,required-header:,required-header-error-message:,make-unpackaged,help,no-bz2,no-bzip2,no-gzip,only-directory,remove-hidden,admin-dir:,branch:,no-i18n,cvs-root:,debug \
-n cvs2dist -- $extraoptions "$@"`

if [ $? != 0 ]; then
	echo "Aborted." >&2
	exit 1
fi

eval set -- "$TEMP"

log="/dev/null"
calclog=0
doi18n="yes"
cvsroot=""
debug=""

while true; do
	case "$1" in
    -x|--excludes) excludes="$excludes $2"; shift 2 ;;
    -i|--includes) includes="$includes $2"; shift 2 ;;
		-v|--version) version=$2; shift 2 ;;
		-n|--name) name=$2; shift 2 ;;
		-a|--admin-dir) admindir=$2;
			if [ `echo $admindir | sed -e 's#^/##'` = $admindir ]; then
				admindir="`pwd`/$admindir"
			fi
			shift 2 ;;
		--debug) debug="non-empty"; shift 1 ;;
		-m|--make-unpackaged) leavedir="non-empty"; shift 1 ;;
		-h|--help) showhelp="non-empty"; shift 1 ;;
		-b|--no-bz2|--no-bzip2) nobzip2="non-empty"; shift 1 ;;
		-o|--only-directory)
			nogzip="non-empty"
			nobzip2="non-empty"
			leavedir="non-empty"
			shift 1 ;;
		-g|--no-gzip) nogzip="non-empty"; shift 1 ;;
		-d|--remove-hidden) removehidden="non-empty"; shift 1 ;;
		-l) calclog=1; shift 1 ;;
		--log) 
			case "$2" in
				# no-argument case
				"") calclog=1; shift 2 ;;
				# something, should be a file
				*)  log=$2
				    origlog=$log
				    if [ `echo $log | sed -e 's#^/##'` = $log ]; then
				    	log="`pwd`/$log"
				    fi

				    shift 2 ;;
			esac ;;
		--) shift
			break ;;
		*) echo "Aborted."
			exit 1 ;;
	esac
done

count=0
for arg do
	test $count = 0 && module=$arg

	let count count++
done


# test if a module and directory name was given
if [ ! -z $showhelp ] || [ -z $module ] ; then
	echo "Usage: cvs2dist module directory [options] [addfile1] [addfile2] ..."
	echo ""
	echo "  -n --name <name>"
	echo "  -v --version <version>"
	echo "  --admin-dir <dir> admin/ location (default is module/admin)"
	echo "                    (symbolic links are OK.)"
	echo "  --log=<logfile> If logfile unspecified, default file is used."
	echo "                  (The '=' is essential and may not be ommited"
	echo "                  when specifying the logfile.)"
	echo "  -l Log to default logfile."
	echo "  -m --make-unpackaged Also makes an unpacked distribution"
	echo "                 in the current directory."
	echo "  -g --no-gzip Do not create gzip package."
	echo "  -b --no-bz2 Do not create bzip2 package."
	echo "  --no-bzip2 Alias for the above."
	echo "  -o --only-directory Alias for -mbg (no packages, only a directory.)"
	echo "  -d --remove-hidden Remove hidden files/directories from packages"
	echo "  -h --help Show this help"
	echo ""
	echo "	Name defaults to the last part of the directory."
	echo "By default, creates name[-version].tar.gz if gzip is installed"
	echo "and/or name[-version].tar.bz2 if bzip2 is installed in the current"
	echo "directory. Removes all temporary files it creates. Produces"
	echo "tarballs that are standard distribution tarballs with a"
	echo "configure script."
	echo "  Unless --no-i18n is given, it automatically tries to check out "
	echo "strings and documentation translation from cvs."
	echo "	Arguments after the second are added to the toplevel directory"
	echo "in the package. Short options can be combined, eg -dm to create"
	echo "a directory and remove hidden files. ~/.cvs2distrc is added to"
	echo "the beginning of command line arguments if it exists."
	echo "	'--' signifies the end of options."
	echo ""
	echo "Example: cvs2dist kcmpureftpd v 0.9 \\"
	echo "         --log ~/tmp/extra-file"
	echo ""
	echo "  	Creates packages of the kolf picture plugin, naming the"
	echo "  packages kolf-picture-0.9 and logging the process. For configure"
	echo "  to succeed, kolf/game.h must be installed or an error will occur."
	echo "  ~/tmp/extra-file is added to the packages."
	echo ""
	echo "Webpage: http://www.katzbrown.com/shiritsu/programming/cvs2dist/"
	echo "Authors: Jason Katz-Brown <jason@katzbrown.com>,"
	echo "         Sebastian Stein <seb.stein@hpfsc.de>,"
	echo "         Dominique Devriese <devriese@kde.org>"
	exit 1
fi

# test if the given module is a directory
test -d $module
if [ $? -ne 0 ]; then
	echo "$module is not a directory."
	echo "Aborted."
	exit 1
fi

test $calclog = 1 && log="$returndir/cvs2dist-$module.log" && origlog="cvs2dist-$module.log"

test $log != "/dev/null" && echo "Logging to $log."

echo "cvs2dist log on "`date` > $log
echo "http://katzbrown.com/shiritsu/programming/cvs2dist/" >> $log
echo -n "Module: $module;" >> $log
if [ -z $version ]; then
	echo "Version unspecified." >> $log
else
	echo "Version $version." >> $log
fi
echo $cmdline >> $log
echo "--------" >> $log

if [ -z $version ]; then
	filename=$module
else
	filename="$module-$version"
fi

# the temporary directory
temp_dir="cvs2dist-tmp"
temp_dist="$temp_dir/$filename"

echo "Temporary directory is $temp_dir." >> $log

# make a temporary directory
test -d $temp_dir && rm -Rf $temp_dir

mkdir -p $temp_dist

# check if we were able to create temp_dir
test -d $temp_dist
if [ $? -ne 0 ]; then
	echo "Could not create temporary directory $temp_dist."
	echo "$temp_dist could not be created." >> $log
	echo "Aborted."
	exit 1
fi


# copy all files of the module to temp_dir/name
cp -r $module/* $temp_dist/

echo "cp -R $module/ $temp_dist/$name" >> $log

modulename="$module"

remove="$remove $modulename.lsm"

# we now enter the temp_dist and delete all unwanted files
# and add wanted files
cd $temp_dist

echo "--------" >> $log

# version file, if it doesn't exist
test ! -z $version && test ! -e VERSION && echo "$module version $version" > VERSION

echo -n "make clean in $temp_dist/... "
make clean >> $log
echo "--------" >> $log
echo "done."

# remove files
echo "Remove files: " >> $log
for file in $remove $includes; do
	find . -name $file | while read f ; do
    found=0
    for p in $excludes ; do
      if echo $f | grep -q $p ; then
        found=1
      fi
    done
    if test $found -eq 0 ; then
      rm -Rf $f
    fi
  done
	echo "find . -name $file | xargs rm -Rf" # >> $log
	find . -name $file | xargs rm -Rf >> $log
done

# remove more files
echo "Remove toplevel files: " >> $log
for file in $toplevelremove; do
	test -e $file && rm -Rf $file && echo "rm -Rf $file" >> $log
done
unset file

# remove hidden files
test ! -z $removehidden && echo "Remove hidden files: " >> $log && find $temp_dist -name ".*" -and ! -name "." | xargs --verbose rm -Rf 2>> $log

echo "--------" >> $log

# create the configure script
# working directory is $temp_dist
echo "make -f Makefile.cvs in $temp_dist/:" >> $log
echo "" >> $log
echo -n "make -f Makefile.cvs in $temp_dist/... "
make -f Makefile.cvs >> $log 2>> $log
echo "--------" >> $log
echo "done."

echo "Directory will be $filename."

# play around with our tar file in temp_dir
cd ..

# make a tar of temp_dist
if [ -z $nogzip ] || [ -z $nobzip2 ]; then
	echo -n "Tarring... "
	echo "tar cf $filename.tar $filename" >> $log
	tar cf $filename.tar $filename >> $log
	echo "done."
fi

if [ -z $nogzip ] && [ ! -z `which gzip 2> /dev/null` ]; then
	cp $filename.tar $filename.tar.tmp
	echo -n "running gzip... "
	echo "gzip $filename.tar" >> $log
	gzip $filename.tar
	echo "done."
	mv $filename.tar.tmp $filename.tar
	mv $filename.tar.gz $returndir
	echo "mv $filename.tar.gz $returndir" >> $log
fi
if [ -z $nobzip2 ] && [ ! -z `which bzip2 2> /dev/null` ]; then
	echo -n "running bzip2... "
	echo "bzip2 $filename.tar" >> $log
	bzip2 $filename.tar
	echo "done."
	mv $filename.tar.bz2 $returndir
	echo "mv $filename.tar.bz2 $returndir" >> $log
fi

test -e $filename.tar && rm -f $filename.tar

cd $returndir

echo -n "Cleaning up... "
test -z "$debug" && rm -Rf $temp_dir
echo "done."


test -z $leavedir && test ! -z $nobzip2 && test ! -z $nogzip && echo "Finished - no created files." && exit 1

# cool, everything went ok!
if [ -z $leavedir ]; then
	echo "Finished. Created packages:"
else
	if [ -z $nogzip ] || [ -z $nobzip2 ]; then
		echo "Finished. Created packages/directories:"
	else
		echo "Finished. Created directory:"
	fi
fi

echo "--------" >> $log
echo "Done: " >> $log
echo "Files are in `pwd`." >> $log

if [ ! -z $leavedir ] && [ -d $filename ]; then
	ls -ld $filename
	ls -ld $filename >> $log
fi
if [ -z $nogzip ] && [ -e $filename.tar.gz ]; then
	ls -l $filename.tar.gz
	ls -l $filename.tar.gz >> $log
fi
if [ -z $nobzip2 ] && [ -e $filename.tar.bz2 ]; then
	ls -l $filename.tar.bz2
	ls -l $filename.tar.bz2 >> $log
fi
if [ $log != "/dev/null" ]; then
	echo "Created log:"
	ls -l $origlog
fi
