#!/bin/bash
UPGRADE_SCRIPTS_DIR=/usr/share/cpbx-upgrade/scripts/

# Run upgrade scripts where needed
#
# Usage $me <new package version> [<old version>]
#
# * rpm can tell the package version by using %{version} in the
#   postinst script.
# * dpkg can tell the currently installed version: a parameter to the
#   postinst script.
# * dpkg was forced to tell the package version by pre-processing the
#   postinst script at buil time (see debian/rules and @@VERSION@@).
# * Thus for rpm we give a somewhat sane default.
# * And anyway, we can also use the contents of the version file.

# If you need to have another case of doing something on a version >= $nnn,
# add the case for $nnn in upgrade_from(), and in its end update curr_v to
# $nnn + 1.

ver_real=$1
curr_v=${2:-5.0.0}

# Version number consists of several parts:
# p1.p2.p3.p4-p5  (5.2.33,  5.2.33.1, 5.2.33-1, 5.2.33-xor1, 5.2.33.1-xor1) 
# Parts p4 and p5 are optional
# Parts before the '-' characters comprise the version number and the part after the '-' character is a release number.
#
# version_compare returns: 0 - versions are equal; 1 - $1 > $2; 3 - $1 < $2 
version_compare() {
	v1=$1
	v2=$2

	v1_version=${v1%%-*}    # Discard everything after first '-'. Example: 5.2.31.2-1 => 5.2.31.2
	v1_release=${v1#*-}     # Discard everything before first '-'. Example: 5.2.31.2-1 => 1
    [ "$v1_version" == "$v1_release" ] && v1_release=""
	v1_p1=${v1_version%%.*} # Discard everything after first '.'. Example: 5.2.31.2 => 5

	v1_p2p3p4=${v1_version#*.}    # Discard everything before first '.'. Example: 5.2.31.2 => 2.31.2
	v1_p2=${v1_p2p3p4%%.*}        # Discard everything after first '.'. Example: 2.31.2 => 2 
	v1_p3p4=${v1_p2p3p4#*.}       # Discard everything before first '.'. Example: 2.31.2 => 31.2
	v1_p3=${v1_p3p4%%.*}          # Discard everything after first '.'. Example: 31.2 => 31
	if [ "$v1_p3p4" != "$v1_p3" ]; then   # 31.2 != 31
		v1_p4=${v1_p3p4#*.}       # Discard everything before first '.". Example: 31.2 => 2
	else
		v1_p4=0
	fi

	v2_version=${v2%%-*}
	v2_release=${v2#*-}
    [ "$v2_version" == "$v2_release" ] && v2_release=""
	v2_p1=${v2_version%%.*}

	v2_p2p3p4=${v2_version#*.}
	v2_p2=${v2_p2p3p4%%.*}
	v2_p3p4=${v2_p2p3p4#*.}
	v2_p3=${v2_p3p4%%.*}
	if [ "$v2_p3p4" != "$v2_p3" ]; then
		v2_p4=${v2_p3p4#*.}
	else
		v2_p4=0
	fi

	res=0
	if [ "${v1_p1}" -gt "${v2_p1}" ]; then 
		res=1
	elif [  "${v1_p1}" -lt "${v2_p1}" ]; then
		res=3
	elif [  "${v1_p2}" -gt "${v2_p2}" ]; then
		res=1
	elif [  "${v1_p2}" -lt "${v2_p2}" ]; then
		res=3
	elif [  "${v1_p3}" -gt "${v2_p3}" ]; then
		res=1
	elif [  "${v1_p3}" -lt "${v2_p3}" ]; then
		res=3
	elif [  "${v1_p4}" -gt "${v2_p4}" ]; then
		res=1
	elif [  "${v1_p4}" -lt "${v2_p4}" ]; then
		res=3
	elif [  "${v1_release}" \> "${v2_release}" ]; then
		res=1
	elif [  "${v1_release}" \< "${v2_release}" ]; then
		res=3
	fi
}

is_ver_less_then() {
	version_compare "$1" "$2"
	if [ "$res" -eq 3 ]; then
		return 0
	else
		return 1
	fi
}

upgrade_from() {
	if is_ver_less_then "${curr_v}" "5.0.30"; then
		${UPGRADE_SCRIPTS_DIR}/to_5_0_30
		curr_v="5.0.30"
	elif is_ver_less_then "${curr_v}" "5.0.41"; then
		${UPGRADE_SCRIPTS_DIR}/to_5_0_41
		curr_v="5.0.41"
	elif is_ver_less_then "${curr_v}" "5.0.51"; then
		${UPGRADE_SCRIPTS_DIR}/to_5_0_51
		curr_v="5.0.51"
	elif is_ver_less_then "${curr_v}" "5.2.3"; then
		${UPGRADE_SCRIPTS_DIR}/to_5_2_3
		curr_v="5.2.3"
	else 
		curr_v=""
	fi

}
[ ! -d /etc/cpbx ] && mkdir /etc/cpbx || true
[ -f /etc/cpbx/cpbx-version ] && curr_v=$(cat /etc/cpbx/cpbx-version) || true
while [ -n "$curr_v" ]; do
	upgrade_from
done
${UPGRADE_SCRIPTS_DIR}/set_version "${ver_real}"
