#!/bin/bash
#
# --- ROCK-COPYRIGHT-NOTE-BEGIN ---
# 
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
# Please add additional copyright information _after_ the line containing
# the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
# the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
# 
# ROCK Linux: rock-src/scripts/Internal
# ROCK Linux is Copyright (C) 1998 - 2005 Clifford Wolf
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version. A copy of the GNU General Public
# License can be found at Documentation/COPYING.
# 
# Many people helped and are helping developing ROCK Linux. Please
# have a look at http://www.rocklinux.org/ and the Documentation/TEAM
# file for details.
# 
# --- ROCK-COPYRIGHT-NOTE-END ---
# 
# Internal contains functionality for maintaining ROCK Linux sources
# and development.
# 

#
# Helper scripts for core developers
#

if [ "$1" = "svn-add-remove" ] ; then

	rm -f files_svn.txt files_local.txt

	for dir in \
		architecture   misc     scripts \
		Documentation  package  target
	do

	    tmp1=`mktemp` tmp2=`mktemp`
	    echo $dir > $tmp1

	    while ! cmp -s $tmp1 $tmp2
	    do
		cat $tmp1 > $tmp2
		find $( cat $tmp2 ) -maxdepth 1 -type d | \
			egrep -v '/(\.svn|CVS)$' 2> /dev/null | sort -u > $tmp1
	    done

	    find $( cat $tmp2 ) -mindepth 1 -maxdepth 1 -type d -name .svn | \
	    while read dirname ; do
		grep ' name="' $dirname/entries | cut -f2 -d'"' | \
			grep -v ^svn: | sed "s,^,${dirname%/*}/,"
	    done | grep "/" | sort >> files_svn.txt

	    find $( cat $tmp2 ) -mindepth 1 -maxdepth 1 | \
			egrep -v '/(\.svn|CVS)' | grep -v '/\.$' | \
			sort >> files_local.txt

	    rm -f $tmp1 $tmp2
	done

	if [ "$DO_SVN_REVERT" = 1 ]
	then
		diff -u0 files_svn.txt files_local.txt | \
			grep '^[+][A-Za-z]' | \
			egrep -xv '\+Documentation/(FAQ|LSM)' | \
			sed 's,^+,rm -vf ,'
	elif egrep -q '#|/\.|~' files_local.txt
	then
		echo '#'
		echo '# Found dangerous files which must not go to SVN:'
		echo '#'
		egrep '#|/\.|~' files_local.txt | sed 's,^,rm -vf ,'
	else
		diff -u0 files_svn.txt files_local.txt | \
			grep '^[-+][A-Za-z]' | \
			egrep -xv '\+Documentation/(FAQ|LSM)' | \
			sed 's,^-,svn remove ,; s,^+,svn add    ,'
	fi

	rm -f files_svn.txt files_local.txt

elif [ "$1" = "svn-commit" ] ; then

	./scripts/Internal svn-add-remove | tee svn_add_remove.sh

	if [ -s svn_add_remove.sh ]
	then
		echo "Run 'sh svn_add_remove.sh' to execute these commands."
	else
		rm -f svn_add_remove.sh
		svn commit
	fi

elif [ "$1" = "svn-revert" ] ; then

	svn revert -R .
	DO_SVN_REVERT=1 ./scripts/Internal svn-add-remove | tee svn_revert.sh

	if [ -s svn_revert.sh ]
	then
		echo "Run 'sh svn_revert.sh' to execute these commands."
	else
		rm -f svn_revert.sh
	fi

elif [ "$1" = "cvs-add-remove" ] ; then

	rm -f files_cvs.txt files_local.txt

	for dir in \
		architecture   misc     scripts \
		Documentation  package  target
	do
	    find $dir/. -type d -name CVS | \
	    while read dirname ; do
		dirname=${dirname/\/./} ; echo ${dirname%/*}
		awk -F "/" '$1 == "" && $3 !~ /^-/ {
			print "'"${dirname%/*}"'/" $2; }' < $dirname/Entries
	    done | grep "/" | sort >> files_cvs.txt

	    find $dir/. | grep -v '/CVS' | grep -v '/\.$' | \
		sed 's,/./,/,' | sort >> files_local.txt
	done

	if egrep -q '#|/\.|~' files_local.txt
	then
		echo '#'
		echo '# Found dangerous files which must not go to CVS:'
		echo '#'
		egrep '#|/\.|~' files_local.txt | sed 's,^,rm -vf ,'
	else
		diff -u0 files_cvs.txt files_local.txt | \
			grep '^[-+][A-Za-z]' | \
			egrep -xv '\+Documentation/(FAQ|LSM)' | \
			sed 's,^-,cvs remove ,; s,^+,cvs add    ,'
	fi

	rm -f files_cvs.txt files_local.txt

elif [ "$1" = "cvs-commit" ] ; then

	./scripts/Internal cvs-add-remove | tee cvs_add_remove.sh

	if [ -s cvs_add_remove.sh ]
	then
		echo "Run 'sh cvs_add_remove.sh' to execute these commands."
	else
		rm -f cvs_add_remove.sh
		cvs commit
	fi

elif [ "$1" = "cvs-update" ] ; then

	cvs -q update -d -P

elif [ "$1" = "cvs-setroot" -a -n "$2" -a -n "$3" ] ; then

	find ./CVS ./architecture ./misc ./scripts ./target ./Documentation \
	     ./package -name Root | grep '/CVS/Root$' | while read fn
	do
		fn="${fn%/CVS/Root}"; fn="${fn#./}"
		echo "Writing $fn/CVS/{Root,Repository}.."
		echo "$2" > $fn/CVS/Root
		echo "$3/$fn" > $fn/CVS/Repository
	done

elif [ "$1" = "ftp-cleanup" ] ; then

	echo "Writing to cleanups.sh ..."
	lastsnap=`mktemp`; echo -n > cleanups.sh
	wget -nv -O $lastsnap http://www.rocklinux.org/sources/download/INDEX
	while read filename; do
		if ! egrep -q " ${filename//bz2/(gz|bz2)} " $lastsnap; then
			echo "rm -vf $filename" >> cleanups.sh
			echo "rm -vf $filename"
		fi
	done < <( ./scripts/Download -list-unknown | cut -f3 -d' ' )
	rm -f $lastsnap

else
	cat << "EOT"

  This is an internal Script. It is only used by the core developers.

	./scripts/Internal  {mode}

  Where the following modes are availalbe:


	svn-add-remove .... print suggested 'svn add' and 'svn remove' calls

	svn-commit ........ make a svn-add-remove and and automatically run a
	                    'svn commit' if svn-add-remove doesn't find
                            anything to add or remove.

	svn-revert ........ run a 'svn revert' and then remove all files which
                            are not known to svn. Useful before doing an 'svn
	                    up' to ver version containing a patch which is
	                    applied in the working directory.


	cvs-add-remove .... print suggested 'cvs add' and 'cvs remove' calls

	cvs-commit ........ make a cvs-add-remove and and automatically run a
	                    'cvs commit' if cvs-add-remove doesn't find
	                    anything to add or remove.

	cvs-update ........ run 'cvs update -d -P'

	cvs-setroot x y ... write 'x' to all CVS/Root files and 'y' to all
                            CVS/Repository files.


	ftp-cleanup ....... remove old files on ftp master

EOT
fi