#usr/bin/python3

# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
# 
#  http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

# Import libaries
import os.path

# Define constants
include_folder = "include/"
header_file = "version.hpp"
file_path = include_folder + header_file
license_header = ("/**\n"
	" * Licensed to the Apache Software Foundation (ASF) under one\n"
	" * or more contributor license agreements.  See the NOTICE file\n"
	" * distributed with this work for additional information\n"
	" * regarding copyright ownership.  The ASF licenses this file\n"
	" * to you under the Apache License, Version 2.0 (the\n"
	" * \"License\"); you may not use this file except in compliance\n"
	" * with the License.  You may obtain a copy of the License at\n"
	" * \n"
	" *  http://www.apache.org/licenses/LICENSE-2.0\n"
	" * \n"
	" * Unless required by applicable law or agreed to in writing,\n"
	" * software distributed under the License is distributed on an\n"
	" * \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n"
	" * KIND, either express or implied.  See the License for the\n"
	" * specific language governing permissions and limitations\n"
	" * under the License.\n"
	" */\n")
warning = ("// This file is autogenerated. Do NOT edit it!\n")

'''
Get the last build version
@param file the file to check
@return build version
'''
def get_buildver(filename) :
	# Open the file
	file = open(filename, "r")

	# Build version
	buildver = 0

	# Get file content
	content = file.readlines()
 
	# Strips the newline character
	for line in content :
		line = line.split(" ")

		# Get the build version from the file
		if "BUILDVER"in line :
			buildver =  int(line[2])
			break

	# Close the file after the successful operation
	file.close()

	# Return 1 if there is no such line
	return buildver

'''
Writing the version as a C/C++ "define"
@param filename the file to be written to
@param buildver current version of the software
'''
def set_buildver(filename, buildver) :
	# Open the file
	file = open(filename, "w")

	# Write the content of the file
	file.write("{0}\n{1}\n#pragma once\n#define BUILDVER {2}".format(
		license_header,
		warning,
		buildver))

	# Close the file after the successful operation
	file.close()

'''
The main function
'''
def main() :
	# The case when there is no include header 
	# or the version is no specified
	buildver = 1

	# Try to read the previous build version
	print("[update_build.py] Trying to read the previous build version...")
	if os.path.isfile(file_path) :
		print("[update_build.py] File found. Reading...")
		buildver = (get_buildver(file_path) + 1)
	else :
		print("[update_build.py] Previous version was not found. Using the default BUILDVER value")

	# Build info
	print("[update_build.py] Build version : ", buildver)

	# Save the new value of the build version
	print("[update_build.py] Writing...")
	set_buildver(file_path, buildver)
	print("[update_build.py] The new build version was value saved!")

if __name__ == "__main__":
	main()