<commit_before>#Created by nil

require 'rubygems'
require 'json'
require 'pathname'
require 'fileutils'
require 'digest/md5'

def short_name(identifier)
	identifier[0 .. identifier.rindex("_")-1]
end

def build_number(identifier)
	identifier[identifier.rindex("_")+1 .. -1].to_i
end

def print_and_execute_command(command)
	puts command
	`#{command}`
	if $?.exitstatus != 0
		puts "Exit status is a failure from last command.."
		exit 3
	end
end

OUTPUT_PATH = "md_submit_output"
MOD_JSON_NAME = "mods.json"
JSON_PATH = (Pathname.new(OUTPUT_PATH) + MOD_JSON_NAME).to_s

if ARGV.length < 2
	puts "Usage: ruby #{$0} <mod_path> <HaloMD_path>"
	exit 1
end
mod_path = ARGV[0]
halomd_path = ARGV[1]

unless File.exists?(mod_path)
	puts "ERROR: Failed to locate mod at #{mod_path}"
	exit 2
end

mod_identifier = File.basename(mod_path, '.*')

halomd_maps_path = Pathname.new(halomd_path) + "Contents/Resources/Data/DoNotTouchOrModify/Maps/"
stock_maps = ["bloodgulch", "crossing", "barrier"]
stock_map_paths = stock_maps.map {|map| halomd_maps_path + "#{map}.map"}

stock_map_paths.each do |path|
	unless File.exists? path
		puts "ERROR: Failed to locate stock map at #{path}"
		exit 7
	end
end

if File.exists? OUTPUT_PATH
	puts "Removing #{OUTPUT_PATH}..."
	FileUtils.rm_rf(OUTPUT_PATH)
end

FileUtils.mkdir(OUTPUT_PATH)

puts "Fetching mods list..."
print_and_execute_command("curl http://halomd.macgamingmods.com/mods/mods.json -o #{JSON_PATH}")
puts "Downloaded #{JSON_PATH}"

mod_entries = JSON.parse(IO.read(JSON_PATH))
if not mod_entries
	puts "ERROR: Failed to properly parse #{JSON_PATH}"
	exit 4
end

if mod_entries['Mods'].select {|entry| entry['identifier'] == mod_identifier}.count > 0
	puts "ERROR: Mod identifier #{mod_identifier} already exists. Please choose another identifier."
	exit 8
end

puts "What mod name would you like to use:"
mod_name = $stdin.gets.chomp
puts "\n"

if mod_entries['Mods'].select {|entry| entry['name'] != mod_name and short_name(entry['identifier']) == short_name(mod_identifier)} .count > 0
	puts "ERROR: Short name #{short_name(mod_identifier)} is already in use by a different mod!"
	exit 9
end

previous_versions = mod_entries['Mods'].select {|entry| entry['name'] == mod_name}
if previous_versions.count > 0
	puts "Found #{previous_versions.count} previous version(s) of #{mod_name}:"
	previous_items = previous_versions.map {|entry| ["Identifier: #{entry['identifier']}", "Version: #{entry['human_version']}", "Description: #{entry['description']}"].join("\n")}
	puts previous_items.join("\n\n")
else
	puts "No previous versions of #{mod_name} were found."
end

puts "\n"

puts "What is this new mod's version?"
mod_version = $stdin.gets.chomp

if previous_versions.select {|entry| entry['human_version'] == mod_version}.count > 0
	puts "ERROR: Mod version #{mod_version} already exists!"
	exit 5
end

puts "What is this new mod's description?"
puts "Leave blank to use #{mod_name} #{previous_versions[0]['human_version']}'s description" if previous_versions.count > 0

mod_description = $stdin.gets.chomp
if mod_description.length == 0 and previous_versions.count > 0
	mod_description = previous_versions[0]['description']
end

new_build_number = build_number(mod_identifier)

if previous_versions.count > 0
	previous_build_number = build_number(previous_versions[0]['identifier'])
	if new_build_number <= previous_build_number
		puts "ERROR: New build number #{new_build_number} is less than previous build number #{previous_build_number}"
		exit 6
	end
end

puts "Creating new mod...\n\n"
puts "Name: #{mod_name}"
puts "Identifier: #{mod_identifier}"
puts "Version: #{mod_version}"
puts "Description: #{mod_description}"
puts "\n"

stock_map_patch_choice = 0
if previous_versions.count > 0
	previous_patches = previous_versions[0]['patches']
	stock_patches = previous_patches.select {|patch| stock_maps.include? patch['base_identifier']}
	if stock_patches.count > 0
		stock_map_patch_choice = stock_maps.index(stock_patches[0]['base_identifier']) + 1
	end
end

if stock_map_patch_choice == 0
	puts "What stock map would you like to create a patch from?"

	(['None'] + stock_maps).each_with_index do |stock_map, index|
		puts "[#{index}] #{stock_map}"
	end

	stock_map_patch_choice = $stdin.gets.chomp.to_i
	if stock_map_patch_choice == 0
		puts "Not creating patch from any stock map"
	else
		puts "Selected stock map #{stock_maps[stock_map_patch_choice-1]}"
	end
end

patch_identifiers = previous_versions.map {|entry| entry['identifier']}
unless stock_map_patch_choice == 0
	stock_map_patch = stock_maps[stock_map_patch_choice-1]
	puts "Selected stock map #{stock_map_patch} for patching...\n\n"
	patch_identifiers << stock_map_patch
end

new_patch_entries = []

if patch_identifiers.count == 0
	puts "Not creating any patches; not necessary."
else<commit_msg>Only patch from up to 3 versions back<commit_after>#Created by nil

require 'rubygems'
require 'json'
require 'pathname'
require 'fileutils'
require 'digest/md5'

def short_name(identifier)
	identifier[0 .. identifier.rindex("_")-1]
end

def build_number(identifier)
	identifier[identifier.rindex("_")+1 .. -1].to_i
end

def print_and_execute_command(command)
	puts command
	`#{command}`
	if $?.exitstatus != 0
		puts "Exit status is a failure from last command.."
		exit 3
	end
end

OUTPUT_PATH = "md_submit_output"
MOD_JSON_NAME = "mods.json"
JSON_PATH = (Pathname.new(OUTPUT_PATH) + MOD_JSON_NAME).to_s

if ARGV.length < 2
	puts "Usage: ruby #{$0} <mod_path> <HaloMD_path>"
	exit 1
end
mod_path = ARGV[0]
halomd_path = ARGV[1]

unless File.exists?(mod_path)
	puts "ERROR: Failed to locate mod at #{mod_path}"
	exit 2
end

mod_identifier = File.basename(mod_path, '.*')

halomd_maps_path = Pathname.new(halomd_path) + "Contents/Resources/Data/DoNotTouchOrModify/Maps/"
stock_maps = ["bloodgulch", "crossing", "barrier"]
stock_map_paths = stock_maps.map {|map| halomd_maps_path + "#{map}.map"}

stock_map_paths.each do |path|
	unless File.exists? path
		puts "ERROR: Failed to locate stock map at #{path}"
		exit 7
	end
end

if File.exists? OUTPUT_PATH
	puts "Removing #{OUTPUT_PATH}..."
	FileUtils.rm_rf(OUTPUT_PATH)
end

FileUtils.mkdir(OUTPUT_PATH)

puts "Fetching mods list..."
print_and_execute_command("curl http://halomd.macgamingmods.com/mods/mods.json -o #{JSON_PATH}")
puts "Downloaded #{JSON_PATH}"

mod_entries = JSON.parse(IO.read(JSON_PATH))
if not mod_entries
	puts "ERROR: Failed to properly parse #{JSON_PATH}"
	exit 4
end

if mod_entries['Mods'].select {|entry| entry['identifier'] == mod_identifier}.count > 0
	puts "ERROR: Mod identifier #{mod_identifier} already exists. Please choose another identifier."
	exit 8
end

puts "What mod name would you like to use:"
mod_name = $stdin.gets.chomp
puts "\n"

if mod_entries['Mods'].select {|entry| entry['name'] != mod_name and short_name(entry['identifier']) == short_name(mod_identifier)} .count > 0
	puts "ERROR: Short name #{short_name(mod_identifier)} is already in use by a different mod!"
	exit 9
end

previous_versions = mod_entries['Mods'].select {|entry| entry['name'] == mod_name}
if previous_versions.count > 0
	puts "Found #{previous_versions.count} previous version(s) of #{mod_name}:"
	previous_items = previous_versions.map {|entry| ["Identifier: #{entry['identifier']}", "Version: #{entry['human_version']}", "Description: #{entry['description']}"].join("\n")}
	puts previous_items.join("\n\n")
else
	puts "No previous versions of #{mod_name} were found."
end

puts "\n"

puts "What is this new mod's version?"
mod_version = $stdin.gets.chomp

if previous_versions.select {|entry| entry['human_version'] == mod_version}.count > 0
	puts "ERROR: Mod version #{mod_version} already exists!"
	exit 5
end

puts "What is this new mod's description?"
puts "Leave blank to use #{mod_name} #{previous_versions[0]['human_version']}'s description" if previous_versions.count > 0

mod_description = $stdin.gets.chomp
if mod_description.length == 0 and previous_versions.count > 0
	mod_description = previous_versions[0]['description']
end

new_build_number = build_number(mod_identifier)

if previous_versions.count > 0
	previous_build_number = build_number(previous_versions[0]['identifier'])
	if new_build_number <= previous_build_number
		puts "ERROR: New build number #{new_build_number} is less than previous build number #{previous_build_number}"
		exit 6
	end
end

puts "Creating new mod...\n\n"
puts "Name: #{mod_name}"
puts "Identifier: #{mod_identifier}"
puts "Version: #{mod_version}"
puts "Description: #{mod_description}"
puts "\n"

stock_map_patch_choice = 0
if previous_versions.count > 0
	previous_patches = previous_versions[0]['patches']
	stock_patches = previous_patches.select {|patch| stock_maps.include? patch['base_identifier']}
	if stock_patches.count > 0
		stock_map_patch_choice = stock_maps.index(stock_patches[0]['base_identifier']) + 1
	end
end

if stock_map_patch_choice == 0
	puts "What stock map would you like to create a patch from?"

	(['None'] + stock_maps).each_with_index do |stock_map, index|
		puts "[#{index}] #{stock_map}"
	end

	stock_map_patch_choice = $stdin.gets.chomp.to_i
	if stock_map_patch_choice == 0
		puts "Not creating patch from any stock map"
	else
		puts "Selected stock map #{stock_maps[stock_map_patch_choice-1]}"
	end
end

patch_identifiers = (previous_versions.map {|entry| entry['identifier']})[0, 3]
unless stock_map_patch_choice == 0
	stock_map_patch = stock_maps[stock_map_patch_choice-1]
	puts "Selected stock map #{stock_map_patch} for patching...\n\n"
	patch_identifiers << stock_map_patch
end

new_patch_entries = []

if patch_identifiers.count == 0
	puts "Not creating any patches; not necessary."
else
{'fraction_non_alphanumeric': 0.12792624818727988, 'fraction_numerical': 0.009011808576755749, 'mean_word_length': 5.862117981520966, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1850295', 'n_tokens_mistral': 3432, 'n_tokens_neox': 3321, 'n_words': 887}