#!/bin/bash

###########################################################################
#
#	Shell program to add a new module to SourceForge.net CVS.
#
#	Copyright 2001-2002, William Shotts <bshotts@users.sourceforge.net>.
#
#	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. 
#
#	This program is distributed in the hope that it will be useful, but
#	WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#	General Public License for more details.
#
#	SourceForge.net is a trademark of VA Software, Inc.
#
#	This software is part of the LinuxCommand.org project, a site for
#	Linux education and advocacy devoted to helping users of legacy
#	operating systems migrate into the future.
#
#	You may contact the LinuxCommand.org project at:
#
#		http://www.linuxcommand.org
#
#	Description:
#
#	This is a program to add a new module to your project's
#	SourceForge.net CVS.  It will import the contents of an existing
#	module directory into your cvs tree at SourceForge.net.  After
#	performing the cvs import, it will perform the initial cvs
#	checkout.  See the SourceForge.net CVS HOW-TO for details on
#	using cvs on SourceForge.net.
#
#
#	Usage:
#
#		sft_new_cvs_module -h | --help | -m module
#
#	Options:
#
#		-h, --help	Display this help message and exit.
#		-m  module      module name
#
#
#	Revisions:
#
#	02/03/2001	File created by lc_new_script v.2.0.6
#	03/07/2002	Various cosmetic updates (0.0.2)
#
#	$Id: sft_new_cvs_module,v 1.4 2002/03/09 16:50:43 bshotts Exp $
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.0.2"

PROJECT_NAME="your_project"	# Put your project name here!
PROJECT_ROOT=~/$PROJECT_NAME
SF_USER=${USER:-$LOGNAME}	# Replace "${USER:-$LOGNAME}" with your SF
				# shell account name if it is not the same
				# as your local user name.


###########################################################################
#	Functions
###########################################################################


function clean_up
{

	#####	
	#	Function to remove temporary files and other housekeeping
	#	No arguments
	#####

	# Restore module directory if program terminated early

	if [ "$module" != "" ]; then	
		if [ -d $PROJECT_ROOT/modules/${module}.bak ]; then
			if [ ! -d $PROJECT_ROOT/modules/${module} ]; then
				mv $PROJECT_ROOT/modules/${module}.bak $PROJECT_ROOT/modules/${module}
			fi
		fi
	fi
}


function error_exit 
{
	#####	
	# 	Function for exit due to fatal program error
	# 	Accepts 1 argument
	#		string containing descriptive error message
	#####

	
	echo "${PROGNAME}: ${1:-"Unknown Error"}" >&2
	clean_up
	exit 1
}


function graceful_exit
{
	#####
	#	Function called for a graceful exit
	#	No arguments
	#####

	clean_up
	exit
}


function term_exit
{
	#####
	#	Function to perform exit if termination signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Terminated"
	clean_up
	exit
}


function int_exit
{
	#####
	#	Function to perform exit if interrupt signal is trapped
	#	No arguments
	#####

	echo "${PROGNAME}: Aborted by user"
	clean_up
	exit
}


function usage
{
	#####
	#	Function to display usage message (does not exit)
	#	No arguments
	#####

	echo "Usage: ${PROGNAME} -h | --help | -m module"
}


function helptext
{
	#####
	#	Function to display help message for program
	#	No arguments
	#####
	
	local tab=$(echo -en "\t\t")
		
	cat <<- -EOF-

	${PROGNAME} ver. ${VERSION}	
	This is a program to add a new module to SourceForge.net cvs.
	
	$(usage)
	
	Options:
	
	-h, --help	Display this help message and exit.
	-m  module      module name
			
	
		
-EOF-
}	


###########################################################################
#	Program starts here
###########################################################################
	
##### Initialization And Setup #####

# Trap TERM, HUP, and INT signals and properly exit

trap term_exit TERM HUP
trap int_exit INT


##### Command Line Processing #####

if [ "$1" = "--help" ]; then
	helptext
	graceful_exit
fi

# Some sanity checks

if [ "$PROJECT_NAME" = "your_project" -o "$PROJECT_NAME" = "" ]; then
	echo >&2 "
PROJECT_NAME has not been set.  Please edit this
script and assign your project name to the
PROJECT_NAME constant."
	error_exit "Configuration error"
fi

if [ ! -d $PROJECT_ROOT ]; then
	echo >&2 "
Project directory does not exist.
Perhaps project name (PROJECT_NAME) is misspelled."
	error_exit "Configuration error"
fi

module=

while getopts ":hm:" opt; do
	case $opt in
		m )	module=$OPTARG
			;;
		h )	helptext
			graceful_exit
			;;
		* )	usage
			clean_up
			exit 1
	esac
done


##### Main Logic #####

# Check if CVS_RSH is set

if [ "$CVS_RSH" != "ssh" ]; then
	error_exit "CVS_RSH environment variable not properly set"
fi

# check if module exists

cd ${PROJECT_ROOT}/modules || error_exit "cannot cd to modules directory"

if [ "$module" = "" ]; then
	error_exit "module name must be specified. Try '-h' for help"
fi

if [ ! -d $module ]; then
	error_exit "no such directory \"$module\""
fi

# Cd into module's directory in preparation of cvs import

cd $module || error_exit "cannot cd to $module"

# Check if module already has a CVS directory

if [ -d "CVS" ]; then
	error_exit "module already in CVS!"
fi

echo "Performing CVS import..."

# Import contents of current directory

cvs -d:ext:${SF_USER}@cvs.${PROJECT_NAME}.sourceforge.net:/cvsroot/${PROJECT_NAME} import -m "Initial import of module: $module" $module vendor start || error_exit "cvs import failed"

echo "Performing initial checkout..."

# Get back to $PROJECT_ROOT/modules

cd ..

# Create backup of existing module directory before new checkout

mv $module ${module}.bak

# Checkout module.  This creates a new directory.

cvs -d:ext:${SF_USER}@cvs.${PROJECT_NAME}.sourceforge.net:/cvsroot/${PROJECT_NAME} co $module || error_exit "cvs checkout failed"

graceful_exit


syntax highlighted by Code2HTML, v. 0.9.1