#!/usr/bin/python
#
#Copyright (c) 2003, 2004, 2005, Olivier Sessink
#All rights reserved.
#
#Redistribution and use in source and binary forms, with or without
#modification, are permitted provided that the following conditions 
#are met:
#  * Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above 
#    copyright notice, this list of conditions and the following 
#    disclaimer in the documentation and/or other materials provided 
#    with the distribution.
#  * The names of its contributors may not be used to endorse or 
#    promote products derived from this software without specific 
#    prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
#LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
#ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
#POSSIBILITY OF SUCH DAMAGE.
#

import shutil
import sys
import os.path
import os
import getopt
import string

LIBDIR='/usr/share/jailkit'
sys.path.append(LIBDIR)
import jk_lib

def listlibraries(config,file):
	return jk_lib.lddlist_libraries(file)
	

def if_nexists_create_dir(config, dst):
	if (not os.path.exists(dst)):
		if (config['verbose']):
			print 'creating directory '+dst
		os.mkdir(dst)

#def copyfileanddirectory(config,src,dst):
#	dstbase = os.path.dirname(dst)
#	if (not os.path.exists(dstbase)):
#		indx = string.find(dstbase,'/',1)
#		while (indx != -1):
#			if_nexists_create_dir(config,dstbase[:indx])
#			indx = string.find(dstbase,'/',indx+1)
#		if_nexists_create_dir(config,dstbase)
#	if (config['verbose']):
#		print 'copying '+src+' to '+dst
#	jk_lib.copy_with_permissions(src,dst,config['verbose'])
#	shutil.copyfile(src,dst)

def startcopy(config, chroot, filestocopy, checklibs=1):
#	for file in filestocopy:
#		if ((config['force'] == 0) and os.path.isfile(chroot+file)):
#			if (config['verbose']):
#				print ''+chroot+file+' exists'
#		else:
	jk_lib.copy_binaries_and_libs(chroot,filestocopy,config['force'] , config['verbose'])
#			copyfileanddirectory(config,file, chroot+file)
#			if (checklibs):
#				libs = listlibraries(config,file)
#				startcopy(config,chroot,libs,0)

def usage():
	print "Usage: "+sys.argv[0]+" [OPTIONS]"
	print ""
	print "-h --help              : this help screen"
	print "-v, --verbose          : show what is being copied"
	print "-f, --force            : overwrite existing files"
	print ""

def testargs(args):
	if (len(args) < 2):
		print "ERROR: need at least a chroot directory and a file to copy"
		print ""
		usage()
		sys.exit(2)
	if (not os.path.isdir(args[0])):
		print "ERROR: "+args[0]+" is not a directory"
		print ""
		usage()
		sys.exit(3)
	for file in args[1:]:
		if (not os.path.isfile(file)):
			print "ERROR: "+file+" does not exist"
			print ""
			usage()
			sys.exit(4)

def main():
	try:
		opts, args = getopt.getopt(sys.argv[1:], 'fhv', ['help', 'verbose', 'force'])
	except getopt.GetoptError:
		usage()
		sys.exit(1)
	config = {}
	config['verbose'] = 0
	config['force'] = 0
	for o, a in opts:
		if o in ("-h", "--help"):
			usage()
			sys.exit()
		if o in ("-v", "--verbose"):
			config['verbose'] = 1
		if o in ("-f", "--force"):
			config['force'] = 1
	testargs(args)
	if (args[0][-1] != '/'): args[0] = args[0]+'/'
	jk_lib.chroot_is_safe(args[0])
	startcopy(config, args[0], args[1:])

if __name__ == "__main__":
    main()
