'''
Script to add the target paths
Add the targets to unclaimed
'''
#from datetime import *
from tuf.libtuf import *
import distutils.core

# copy subdirectory example
newMetaDir = "/var/www/metadata.staged"
metaDir = "/var/www/metadata"
tagetsDirectory = "/var/www/tagets"
futureTargetsDirectory = "/root/future_tagets"



repo_path = "/var/www"
targets_path = "/var/www/targets/"

#keys should be off-line, example path to USB stick
path_to_target_key = "/root/keys/targets_key"
path_to_root_key = "/root/keys/root_key"
path_to_root_key2 = "/root/keys/root_key2"
path_to_release_key = "/root/keys/timestamp_key"
path_to_timestamp_key = "/root/keys/timestamp_key"

#finds current week
#week = datetime.date(datetime.now()).strftime("%U") 

#check key paths
def checkKey(path_to_key):
	temp = 1 
	key_type = path_to_key.split('/')[-1]
	while(temp):
		try:
			f = open(path_to_key)
			f.close()
			temp = 0
		except:
			print "Could not find " + key_type 
			path_to_key = raw_input("Please enter a absolute path to " + key_type  + " file: ")
	return path_to_key

path_to_target_key = checkKey(path_to_target_key)
path_to_root_key = checkKey(path_to_root_key)
path_to_root_key2 = checkKey(path_to_root_key2)
path_to_release_key = checkKey(path_to_release_key)
path_to_timestamp_key = checkKey(path_to_release_key)


#coppy new targets stop the service while creating new metadata
distutils.dir_util.copy_tree(futureTargetsDirectory, tagetsDirectory)


#there are 2 weeks, there 2 folders that get toggeled every week
#targets_path = targets_path + str(int(week) % 2) +'/'



# Load the repository created in the previous section.  This repository so far contains metadata for
# the top-level roles, but no targets.
repository = load_repository(repo_path)

# Get a list of file paths in a directory, even those in sub-directories.
# This must be relative to an existing directory in the repository, otherwise throw an
# error.
list_of_targets = repository.get_filepaths_in_directory(targets_path,
                                                        recursive_walk=True, followlinks=True) 

# Add the list of target paths to the metadata of the Targets role.  Any target file paths
# that may already exist are NOT replaced.  add_targets() does not create or move target files.
repository.targets.add_targets(list_of_targets)

# Individual target files may also be added.
#repository.targets.add_target("path/to/repository/targets/file3.txt")

# The private key of the updated targets metadata must be loaded before it can be signed and
# written (Note the load_repository() call above).
private_targets_key =  import_rsa_privatekey_from_file(path_to_target_key)

repository.targets.load_signing_key(private_targets_key)

# Due to the load_repository(), we must also load the private keys of the other top-level roles
# to generate a valid set of metadata.
private_root_key = import_rsa_privatekey_from_file(path_to_root_key)



private_root_key2 = import_rsa_privatekey_from_file(path_to_root_key2)

private_release_key = import_rsa_privatekey_from_file(path_to_release_key)

private_timestamp_key = import_rsa_privatekey_from_file(path_to_timestamp_key)


repository.root.load_signing_key(private_root_key)
repository.root.load_signing_key(private_root_key2)
repository.release.load_signing_key(private_timestamp_key)
repository.timestamp.load_signing_key(private_timestamp_key)

# Generate new versions of all the top-level metadata.
repository.write()
distutils.dir_util.copy_tree(newMetaDir, metaDir)





@@ -165,30 +165,205 @@
 ple%0A
-newMetaDir = %22/var/www
+%0Apath_to_server_root = raw_input(%22Enter the path to the server, or press enter for /var/www: %22)%0Aif(not(path_to_server_root)):%0A%09path_to_server_root = %22/var/www%22%0A%0AnewMetaDir = path_to_server_root + %22
 /met
@@ -386,25 +386,39 @@
 taDir = 
-%22/var/www
+path_to_server_root + %22
 /metadat
@@ -438,16 +438,200 @@
 ctory = 
+path_to_server_root + %22/tagets%22%0Apath_to_f_targets = raw_input(%22Enter the path to futer targets, or press enter for /var/www: %22)%0Aif(not(path_to_f_targetstargets)):%0A%09path_to_f_targets = 
 %22/var/ww
@@ -631,23 +631,16 @@
 /var/www
-/tagets
 %22%0Afuture
@@ -658,22 +658,37 @@
 ctory = 
-%22/root
+path_to_f_targets + %22
 /future_
@@ -698,16 +698,19 @@
 ets%22%0A%0A%0A%0A
+%0A%0A
+#
 repo_pat
@@ -724,16 +724,49 @@
 ar/www%22%0A
+repo_path = path_to_server_root%0A#
 targets_
@@ -791,16 +791,53 @@
 argets/%22
+%0Atagets_path = futureTargetsDirectory
 %0A%0A#keys 
@@ -1766,72 +1766,10 @@
 )%0A%0A%0A
-#coppy new targets stop the service while creating new metadata
 %0A
+#
 dist
@@ -3827,76 +3827,250 @@
 ta.%0A
-repository.write()%0Adistutils.dir_util.copy_tree(newMetaDir, metaDir)
+#coppying new targets and metadata. Stop the service while creating new metadata!%0Arepository.write()%0Adistutils.dir_util.copy_tree(newMetaDir, metaDir)%0Adistutils.dir_util.copy_tree(futureTargetsDirectory, tagetsDirectory)%0A#comtinue the service
 %0A%0A%0A%0A

Update add_targets_delegate.py