#! /usr/bin/python
"""
#+
# NAME:
#	smeicvs
# PURPOSE:
#	Updates working copies in the $SMEI tree from the
#	SMEI CVS repository.
# CATEGORY:
#	gen/python
# CALLING SEQUENCE:
#	smeicvs.py -import <directory>
#	smeicvs.py -checkout <directory>
#	smeicvs.py <directory>
# INPUTS:
#	<directory>	fully-qualified directory in the SMEI tree
# OPTIONAL INPUT PARAMETERS:
#	-import		import into repository
#				Sets up all code in the tree attached to <directory>
#				in the SMEI CVS repository.
#	-checkout	check out of repository into $SMEI tree
# OUTPUTS:
# CALLS:
#	tiny.is_there, tiny.args
# SEE ALSO:
# EXAMPLE:
#	smeicvs.py -import $SMEI/ucsd/camera/for/pattern
#		was used import the pattern software into the
#		repository
#	smeicvs.py -checkout $SMEI/ucsd/camera/for/pattern
#		was used to extract the pattern software from the
#		repository and put in the SMEI tree (after removing
#		the original directory used in the import).
#	smeicvs.py $SMEI/ucsd/camera/for/pattern
#		After the initial import or export, this command
#		uses the repository to update the SMEI tree
# PROCEDURE:
#	The -import and -checkout options should be needed only once
#	when setting up the a subdirectory in $SMEI.
#	After that a call 'smeicvs.py <directory>' will update everything.
#
#	The repository is $SMEI/.cvscode, i.e. the repository itself 
#	is part of the SMEI tree).
#
#	A subdirectory in $SMEI is imported using the fully-qualified
#	directory name with slashes replaced by underscores,
#	e.g. $SMEI/ucsd/gen/for will become project ucsd_gen_for.
#
#	For the initial import and for subsequent updates the subdirectory
#	is renamed from e.g. $SMEI/ucsd/gen/for to $SMEI/ucsd/gen/ucsd_gen_for.
#	Then cvs is called to do the import or update. Finally the
#	the directory is renamed to its original name.
# MODIFICATION HISTORY:
#	DEC-2004, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#-
"""
import sys, os
from tiny import is_there, args, hide_env

if __name__ == '__main__':

	arg  = args( sys.argv )
	narg = len(arg)

	init     = is_there('-import'  ,sys.argv)
	checkout = is_there('-checkout',sys.argv)

	if narg == 2:				# Directory specified in arg[1]
		subdir = arg[1]
		if subdir == '.':		# Translate '.' to working directory
			subdir = os.getcwd()
	else:						# No directory specified; prompt
		subdir = raw_input('SMEI subdirectory: ')
		if subdir[0] == '$':	# Translate env var (starts with $)
			slash = subdir.find('/')
			if slash == -1:
				slash = len(subdir)
			env = subdir[1:slash]
			env = os.environ[env]
			subdir = env+subdir[slash:]

	if checkout:				# Initial checkout into SMEI tree
		if os.path.isdir(subdir):
			print 'Directory already exists:', hide_env(subdir)
			sys.exit()

	else:
		if not os.path.isdir(subdir):
			print 'Not a directory:', hide_env(subdir)
			sys.exit(1)

	if subdir[len(subdir)-1] == '/':
		subdir = subdir[0:len(subdir)-1]

	smei = os.environ['SMEI']+'/'
	p = subdir.find(smei)

	if p == -1:
		print 'Not a SMEI subdirectory:', hide_env(subdir)
		sys.exit()

	project = subdir[len(smei):]

	p = project.find('/')
	while p != -1:
		project = project[0:p]+'_'+project[p+1:]
		p = project.find('/')

	parent = (os.path.split(subdir))[0]
	tmpdir = os.path.join(parent,project)

	cvscode = os.path.join(smei,'.cvscode')
	rename = 0

	if init:

		# Rename subdirectory to match CVS module name

		if subdir != tmpdir:
			sts = os.rename(subdir,tmpdir)
			rename = 1

		sts = os.chdir(tmpdir)
		sts = os.spawnlp( os.P_WAIT, 'cvs', 'cvs', '-d', cvscode, 'import', project, project, 'v1_0' )

	elif checkout:

		rename = 1

		sts = os.chdir(parent)
		sts = os.spawnlp( os.P_WAIT, 'cvs', 'cvs', '-d', cvscode, 'checkout', project )

	else:

		if subdir != tmpdir:
			sts = os.rename(subdir,tmpdir)
			rename = 1

		sts = os.chdir(parent)
		sts = os.spawnlp( os.P_WAIT, 'cvs', 'cvs', '-d', cvscode, 'update', project )

	# Rename to original name

	if rename:
		sts = os.rename(tmpdir,subdir)

	sys.exit()
