#!/usr/bin/python
"""
#+
# NAME:
#	rsync_loc
# PURPOSE:
#	Python script to synchronize a master directory with a 
#	local copy accessible through anonymous ftp.
# INPUTS:
#	Content of <source>
# OPTIONAL INPUT ARGUMENTS:
#	source=<source>			default: $SMEI
#		source directory to be mirrored to <source>
#	destination=<destination>	default: loc_smei' (only if $SMEI is source)
#		name of destination subdirectory in the root directory
#		of the oftp server.
#	include=<include>		default: all subdirectories in <source>
#		comma-separated list of subdirectories in <source> to be mirrored.
#		Entries in this list are appended to <destination> and tested for
#		existence.
#	exclude=<exclude>		default: none
#		comma-separated list of subdirectories in <source> to be excluded.
#		Entries in this list are appended to <source> and tested for
#		existence.
# CALLS:
#	clean_loc_smei, tiny.start
# PROCEDURE:
#	If the include and exclude arguments are not used (i.e. everything in
#	<source> is mirrored than calling rsync_loc is essentially the same as
#	executing rsync directly as:
#
#	rsync --archive --delete --verbose <source> <parent of <destination>> 
# MODIFICATION HISTORY:
#	JAN-2002, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
#		More generic replacement of rsync_ssw allowing for exclusion
#		and/or inclusion of subdirectories in the source directory.
#-
"""
import os, sys 
from tiny import start

if __name__ == '__main__':

	smei = os.environ['SMEI']

	# Pick up source directory. Check for existence.

	source = start( 'source=', sys.argv )
	if source == '':
		source = smei		# Def source is SMEI master

	if not os.path.isdir(source):
		print 'Source is not an existing directory: ', source
		sys.exit()

	# Pick up destination directory

	destination = start( 'destination=' , sys.argv )
	if destination == '':
		if source == smei:
			destination = 'loc_smei'

	if destination == '':
		print 'No destination specified'
		sys.exit()

	# If the input destination is not an existing directory, then
	# determine the ftp root directory, and prepend to destination.

	if not os.path.isdir(destination):

		# The destination mirror directory should be located the root directory
		# of the anonymous ftp server. This root is set in /etc/init.d/oftd

		ftproot = os.environ['FTP_ANON']
		destination = os.path.join(ftproot, destination)

	# Check for existence of destination

	if not os.path.isdir(destination):
		print 'Destination is not an existing directory: ', destination
		sys.exit()

	if source == destination:
		print 'Source and destination are identical'
		sys.exit()

	#print '\n', source, ' ---> ', destination, '\n'

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

	# List of subdirs in source dir to be mirrored

	incls = start( 'include=', sys.argv )
	if incls == '':
		incls = os.listdir(source)
	else:
		incls = incls.split(',')

	excls = start( 'exclude=', sys.argv )
	excls = excls.split(',')

	for incl in incls:				# Loop over all subdirectories
		if excls.count(incl) == 0:		# Check whether i is on exclude list
			incldir = os.path.join(source,incl)
			if os.path.isdir(incldir):	# Check whether i exists
				#print incldir
				result = (os.popen(rsync+incldir+' '+destination)).read()

	# Hope to remove this sometime

	#if source == smei:
	#	sts = os.spawnlp(os.P_WAIT, 'idl','idl','-quiet','clean_loc_smei.pro')

	#print 'done'
