"""

Copies names (value 1 in 1:2:3) from source file to target file 
Requires manual use
	
"""

import os, sys

verbose = True  # If True, print every replacement
pretend = False # If True, don't actually write

# The two files to be worked with
#source = "../../../mhag-read-only/src/org/mhag/model/data/mh3g/armor.dat"
#target = "../src/org/mhag/model/data/mh3g/armor.dat"

source = "../src/org/mhag/model/data/mh3g/armor.dat"
target = "../src/org/mhag/model/data/mh3g/armor_item.dat"

# Prevent cross contamination
try:
	os.remove(source + ".new")
	os.remove(target + ".new")
except OSError:
	pass

# Read both files, line by line
with open(source) as f:
    source_data = f.readlines()
with open(target) as f:
	target_data = f.readlines()

# Generate list of armor names from source
source_names = []	
for line in source_data:
	# Exclude comments and blank lines
	if line[0] != "#" and line[0] != "\n":
		source_names.append(line.split(":")[0])	
source_data = None

# Replace the names in the target
i = 0
for line in target_data:
	# Exclude comments and blank lines
	if line[0] != "#" and line[0] != "\n":
		name = line.split(":")[0]
		if verbose: print(name + "|" + source_names[i])
		new_line = line.replace(name, source_names[i])
	
		# Write replaced line to temporary file
		with open(target + ".new", "a") as f:
			f.write(new_line)
			
		# Increment
		i += 1	
		
# Replace target file with temporary file			
if not pretend:	
	os.remove(target)
	os.rename(target + ".new", target)
