<commit_before>#!/usr/bin/env bash

# 
# works with a file called VERSION in the above directory, the contents of which 
# should be a semantic version string.
# 
# this script will display the current version, suggest a "minor" version update, 
# and ask for input to use the suggestion, or a newly entered value.
#
# once the new version number is determined, a git tag will be created and pushed.
#

# todo - instead (or in addition) of updating the VERSION file, grep the project files
# todo - and look for @@VERSION@@, or VERSION= or something, to replace variable values inside project files
# todo - this is all preparation for git-flux-version, we want it to be able to get the right version 
# todo - after installation + on command (git flux version)

main() {
	local source_dir="$( cd "$(dirname "${BASH_SOURCE}")" ; pwd -P )"
	local version_file=${source_dir}/../VERSION

	local commit_message_template; local suggested_version
	if [[ -f $version_file ]]; then
        local current_version="$(get_current_version ${version_file})"
		log "current version : $current_version"
		
		suggested_version="$(get_suggested_version "$current_version")"
		commit_message_template="version bump to %s"
	else # todo - possibly add check for remote tags, to initialize VERSION file with the latest available release
		suggested_version="0.1.0"
		commit_message_template="add VERSION file, initial version is %s"
	fi

	local new_version="$(prompt_new_version "$suggested_version")"
	local commit_message="$(printf "$commit_message_template" "$new_version")"

	log "new version will be set to '$new_version'"
	write_new_version "$version_file" "$new_version"
	submit_new_version "$version_file" "$commit_message" "$new_version"
}

prompt_new_version() {
	local suggested_version="$1"
	read -r -p "$(log "enter the new version [$suggested_version]: ")" input_version
	echo "${input_version:-$suggested_version}"
}

get_current_version() {
	local version_file=${1}
	echo "$(cat ${version_file})"
}

get_suggested_version() {
	local current_version="$1"
	local v_segments=($(echo "$current_version" | tr '.' ' '))
	local v_major=${v_segments[0]}; local v_minor=${v_segments[1]}; local v_patch=${v_segments[2]}

	v_minor=$((v_minor + 1))
	v_patch=0
	
	echo "$v_major.$v_minor.$v_patch"
}

write_new_version() {
	local version_file="$1"; local new_version="$2"
	echo "$new_version" > ${version_file}
}

submit_new_version() {
	local version_file="$1"; local commit_message="$2"; local new_version="$3"
	git add ${version_file}
	git commit -m "$commit_message"
	git tag -a -m "tagging version $new_version" "v$new_version"
	git push origin --tags
}

log() {
	echo " > $1"
}


main<commit_msg>update bump-version script<commit_after>#!/usr/bin/env bash


main() {

	ensure_version_file

	case "$1" in
		help|-h)
			usage
		;;
	esac

	local bump_segment="$1"

	local current_version="$(get_current_version)"
	log "current version: ${current_version:-[none]}"

	local commit_message_template="$(get_commit_message_template "$current_version")"
	local suggested_version="$(get_suggested_version "$current_version" "$bump_segment")"

	local new_version="$(prompt_new_version "$suggested_version")"
	local commit_message="$(printf "$commit_message_template" "$new_version")"

	log "new version will be set to '$new_version'"
	write_version_file "$new_version"
	publish_version "$commit_message" "$new_version"
}

usage() {
	echo "
usage: [environment] bump-version.sh [major|minor|patch]
environment:
   VERSION_FILE=$VERSION_FILE
	"
	exit 0
}

ensure_version_file() {
	local git_root="$(git rev-parse --show-toplevel)"
	VERSION_FILE="${VERSION_FILE:-$git_root/VERSION}"
}

get_current_version() {
	local current_tag="$(git describe --abbrev=0 --tags 2>/dev/null)"
	local current_version="${current_tag#v}" # assume a tag format of "vX.X.X"
	printf "%s" "$current_version"
}

get_commit_message_template() {
	local current_version="$1"
	local template
	if [[ $current_version ]]; then
		template="bump version to %s"
	else
		template="initialize version to %s"
	fi
	if [[ ! -f $VERSION_FILE ]]; then
		template="add version file, $template"
	fi
	printf "%s" "$template"
}

get_suggested_version() {
	local current_version="$1"; local bump_segment="$2"
	local suggested_version
	if [[ $current_version ]]; then
		suggested_version="$(increment_version "$current_version" "$bump_segment")"
	else
		suggested_version="0.1.0"
	fi
	printf "%s" "$suggested_version"
}

increment_version() {
	local current_version="$1"; local segment="${2:-minor}"
	local v_segments=($(echo "$current_version" | tr '.' ' '))
	local v_major=${v_segments[0]}; local v_minor=${v_segments[1]}; local v_patch=${v_segments[2]}

	let "v_$segment += 1"
	if [[ $segment = minor || $segment = major ]]; then
		v_patch=0
	fi
	if [[ $segment = major ]]; then
		v_minor=0
	fi

	echo "$v_major.$v_minor.$v_patch"
}

prompt_new_version() {
	local suggested_version="$1"
	read -r -p "$(log "enter the new version [$suggested_version]: ")" answer
	echo "${answer:-$suggested_version}"
}

publish_version() {
	local commit_message="$1"; local version="$2"
	git add ${VERSION_FILE}
	git commit --only -m "$commit_message" -- ${VERSION_FILE}
	git tag -a -m "tagging version $version" "v$version"
	git push origin --tags
}

write_version_file() {
	echo "$1" > ${VERSION_FILE}
}

log() {
	echo " > $1"
}


main "$@"
{'fraction_non_alphanumeric': 0.1430171769977595, 'fraction_numerical': 0.0076549663928304704, 'mean_word_length': 5.430972388955582, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4358693', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1770, 'n_words': 486}