#!/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 - 2003 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.
# 

#
# Clifford's Personal Helper Script
#

export CVSROOT=":pserver:anonymous@clifford.homedns.org:/home/cvs"
export CVSREPOSITORY=rock-src-1.7

if [ "$1" = "snapshot" ] ; then

	rm -rf src/snapshot
	mkdir -p src/snapshot
	cd src/snapshot

	cvs checkout -P $CVSREPOSITORY

	tm=$( date +%Y%m%d%H%M )
	cd $CVSREPOSITORY; ./scripts/Puzzle; cd ..
	mv $CVSREPOSITORY $CVSREPOSITORY-$tm

	tar cIf $CVSREPOSITORY-$tm.tar.bz2 $CVSREPOSITORY-$tm

	echo -n "Press enter when you are ready to type password #1."
	read; scp $CVSREPOSITORY-$tm.tar.bz2 \
		clifford@www.rocklinux.org:htdocs/rocklinux.org/snapshots/

	echo -n "Press enter when you are ready to type password #2."
	read; ssh -t -x -l clifford www.rocklinux.org \
		"cd htdocs/rocklinux.org && touch rock.sed && make"

	cd ..; rm -rf snapshot

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" ] ; then

	[ "$2" ] && export CVSROOT="$2"

	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 "$CVSROOT" > $fn/CVS/Root
		echo "$CVSREPOSITORY/$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:

	snapshot ........ checkout cvs in a temp dir and upload a snapshot
	                  to the ROCK Linux webpage.

	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.

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

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

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

EOT
fi

