#!/usr/bin/python
"""
#+
# NAME:
#	rsync_ssw
# PURPOSE:
#	Python cgi script to synchronize master SMEI tree with the SSW mirror 
# CALLING SEQUENCE:
#	rsync_ssw.py
# OPTIONAL INPUT PARAMETERS:
#	-nodoc		if set, no new documentation is generated
# PROCEDURE:
#	List of subdirectories in $SMEI to be mirrored is hardcoded
#	Uses rsync
# CALLS:
#	tiny.is_there
# MODIFICATION HISTORY:
#	JAN-2002, Paul Hick (UCSD/CASS)
#	FEB-2002, Paul Hick (UCSD/CASS)
#		Removed $SMEI/pro from SSW_SMEI master
#	AUG-2002, Paul Hick (UCSD/CASS)
#		Output file is now created in subdirectory ssw_mirror
#		of the directory where this script is located.
#		Added call to 'idl run_ssw_smei.pro' to update the html
#		documentation before calling rsync.
#	JAN-2003, Paul Hick (UCSD/CASS)
#		Added -nodoc keyword
#	APR-2003, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		Added Fortran treee
#-
"""
import os, sys 
from tiny import is_there

if __name__ == '__main__':

	# List of subdirs in $SMEI to be mirrored

	#subdirs = ['setup','for','ucsd']
	subdirs = ['setup','ucsd']

	# The SSW_SMEI mirror should be located in the subdirectory
	# ssw_smei of the root directory of the anonymous ftp server.

	mirror = os.path.join(os.environ['FTP_ANON'],'ssw_smei')
	smei   = os.environ['SMEI']		# Loc of SMEI master

	nodoc = is_there( '-nodoc', sys.argv )

	if not nodoc:

		# The documentation will be updated. To avoid clogging the rsync
		# results we delete the doc directory here.

		docdir = os.path.join(mirror,'ucsd','doc')
		sts = os.spawnlp(os.P_WAIT, 'rm','rm','-rf', docdir)

	print ""
	print smei, " ---> ", mirror
	print ""

	for i in range(len(subdirs)):		# Turn subdirs into full paths
		subdirs[i] = os.path.join(smei,subdirs[i])

	# Make output file name in subdirectory ssw_mirror.
	# sys.argv[0] contains the file name of this script.

	file = os.path.join(os.path.dirname(sys.argv[0]),'ssw_mirror','rsync_ssw_'+( (os.popen('date +%Y_%m_%d')).read() )[0:10]+'.lst')

	rsync = 'rsync --archive --verbose --delete '

	iu = open(file, 'w')

	for i in subdirs:			# Loop over all subdirectories
		print i
		iu.write( '\n\n ===== '+i+'\n\n' )

		cmd = rsync+i+' '+mirror
		iu.write(  (os.popen(cmd)).read()  )

	iu.close()

	print 'Results to '+file

	if nodoc:
		print 'No new documentation generated'
	else:
		print 'Generating new documentation'

		# Create the doc directory (we deleted it earlier).
		# Then create the documentation.

		sts = os.mkdir(docdir)
		sts = os.spawnlp(os.P_WAIT, 'idl','idl','-quiet','run_ssw_smei.pro')

	print 'done'
