#!/usr/bin/env sh
#
# Exit on error
# set -e

project_dir=$(git rev-parse --show-toplevel)

psrfix() {
    command -v php-cs-fixer >/dev/null 2>&1 || return
    "$(command -v php-cs-fixer)" fix "$project_dir/packages/web" --rules=@PSR2 >/dev/null 2>&1
    git add "$project_dir/packages/web"
}

updateLanguage() {
    command -v xgettext >/dev/null 2>&1 || return
    command -v msgcat   >/dev/null 2>&1 || return
    command -v msgmerge >/dev/null 2>&1 || return
    xgettext --language=PHP --from-code=UTF-8 --output="$project_dir/packages/web/management/languages/messages.pot" --omit-header --no-location $(find "$project_dir/packages/web/" -name "*.php")
    msgcat --sort-output -o "$project_dir/packages/web/management/languages/messages.pot" "$project_dir/packages/web/management/languages/messages.pot"
    for PO_FILE in $(find "$project_dir/packages/web/management/languages/" -type f -name "*.po"); do
        msgmerge --update --backup=none "$PO_FILE" "$project_dir/packages/web/management/languages/messages.pot" 2>/dev/null >/dev/null
        msgcat --sort-output -o "$PO_FILE" "$PO_FILE"
    done
    git add "$project_dir/packages/web/management/languages/"
}

updateLanguage
psrfix

# Get the current branch name
gitbranch=$(git branch --show-current)

# Get the latest tag commit
gitcom=$(git rev-list --tags --no-walk --max-count=1)

# Count commits from the last tag to HEAD (as you had it)
git fetch origin master:master 2>/dev/null || true
gitcount=$(git rev-list master..HEAD --count)

# Extract the first part of the branch name
branchon=$(echo "$gitbranch" | awk -F'-' '{print $1}')

# Extract the second part of the branch name
branchend=$(echo "$gitbranch" | awk -F'-' '{print $2}')

# Define the path to the system file
system_file="$project_dir/packages/web/lib/fog/system.class.php"

current_version=$(grep "define('FOG_VERSION'" "$system_file" | sed "s/.*FOG_VERSION', '\([^']*\)');/\1/")
current_channel=$(grep "define('FOG_CHANNEL'" "$system_file" | sed "s/.*FOG_CHANNEL', '\([^']*\)');/\1/")

verbegin=""
channel="$current_channel"
trunkversion="$current_version"

case "$branchon" in
    dev)
        # Describe the tag and append the commit count correctly
        tagversion=$(git describe --tags "$gitcom")
        baseversion=${tagversion%.*}   # Retain everything before the last segment
        trunkversion="${baseversion}.${gitcount}"
        channel="Patches"
        ;;
    stable)
        # Describe the tag and append the commit count correctly
        tagversion=$(git describe --tags "$gitcom")
        baseversion=${tagversion%.*}   # Retain everything before the last segment
        gitcount=$(git rev-list master..dev-branch --count) # Get the gitcount from dev-branch instead
        trunkversion="${baseversion}.${gitcount}"
        channel="Patches"
        ;;
    working)
        # Generate a version number based on the branchend and commit count for the working branch
        verbegin="${branchend}.0-beta"
        trunkversion="${verbegin}.${gitcount}"
        channel="Beta"
        ;;
    rc)
        channel="Release Candidate"
        version_prefix="${branchend}.0-RC"
        # POSIX regex replacement for [[ ... =~ ... ]] / BASH_REMATCH
        n=$(printf '%s\n' "$current_version" | sed -n "s/^${version_prefix}-\([0-9][0-9]*\)\$/\1/p")
        if [ -n "$n" ]; then
            last_rc_version=$n
            next_rc_version=$((last_rc_version + 1))
            trunkversion="${version_prefix}-${next_rc_version}"
        else
            trunkversion="${version_prefix}-1"
        fi
        ;;
    feature)
        verbegin="${branchend}.0-feature"
        trunkversion="${verbegin}.${gitcount}"
        channel="Feature"
        ;;
esac

# Update the version and channel in the system file
sed -i "s/define('FOG_VERSION',.*);/define('FOG_VERSION', '$trunkversion');/g" "$system_file"
sed -i "s/define('FOG_CHANNEL',.*);/define('FOG_CHANNEL', '$channel');/g" "$system_file"

# Add the modified system file to the staging area
git add "$system_file"

