From 709e8e54dbbab25f548c6eaa2dcbd35849e5682e Mon Sep 17 00:00:00 2001
From: Vitaly Lipatov <lav@etersoft.ru>
Date: Sat, 23 Apr 2016 19:55:32 +0300
Subject: [PATCH] release-upgrade: rewrite to support p8 -> Sisyphus and vice
 versa

---
 bin/epm-release_upgrade | 133 +++++++++++++++++++++++++++-------------
 bin/epm-repofix         |   4 +-
 2 files changed, 92 insertions(+), 45 deletions(-)

diff --git a/bin/epm-release_upgrade b/bin/epm-release_upgrade
index 64be2235..a52991d0 100644
--- a/bin/epm-release_upgrade
+++ b/bin/epm-release_upgrade
@@ -28,18 +28,23 @@ __replace_text_in_alt_repo()
 	done
 }
 
+__wcount()
+{
+	echo "$*" | wc -w
+}
+
 __detect_alt_release_by_repo()
 {
 	local BRD=$(cat /etc/apt/sources.list /etc/apt/sources.list.d/*.list | \
-		grep -v "^#" | grep "p[5-9]/branch" | sed -e "s|.*\(p[5-9]\)/branch.*|\1|g" | sort -u )
-	if [ $(estrlist count $BRD) = "1" ] ; then
+		grep -v "^#" | grep "p[5-9]/branch/" | sed -e "s|.*\(p[5-9]\)/branch.*|\1|g" | sort -u )
+	if [ $(__wcount $BRD) = "1" ] ; then
 		echo "$BRD"
 		return
 	fi
 
 	local BRD=$(cat /etc/apt/sources.list /etc/apt/sources.list.d/*.list | \
-		grep -v "^#" | grep "Sisyphus" | sed -e "s|.*\(Sisyphus\).*|\1|g" | sort -u )
-	if [ $(estrlist count $BRD) = "1" ] ; then
+		grep -v "^#" | grep "Sisyphus/" | sed -e "s|.*\(Sisyphus\).*|\1|g" | sort -u )
+	if [ $(__wcount $BRD) = "1" ] ; then
 		echo "$BRD"
 		return
 	fi
@@ -51,24 +56,28 @@ __replace_alt_version_in_repo()
 {
 	local i
 	assure_exists apt-repo
-	echo "Upgrading $DISTRNAME from $1 to $2 ..."
-	docmd apt-repo list | sed -e "s|\($1/branch\)|{\1}->{$2/branch}|g" | egrep --color -- "$1/branch"
+	#echo "Upgrading $DISTRNAME from $1 to $2 ..."
+	docmd apt-repo list | sed -e "s|\($1\)|{\1}->{$2}|g" | egrep --color -- "$1"
 	confirm "Are these correct changes?" || fatal "Exiting"
-	__replace_text_in_alt_repo "/^ *#/! s!$1/branch!$2/branch!g"
+	__replace_text_in_alt_repo "/^ *#/! s!$1!$2!g"
 	docmd apt-repo list
 }
 
-__update_alt_repo_to_next_distro()
+__alt_repofix()
 {
-	case "$DISTRVERSION" in
-		p6)
-			docmd epm install apt-conf-branch altlinux-release-p6 || fatal
-			load_helper epm-repofix
-			pkg_filenames= epm_repofix
-			__replace_alt_version_in_repo p6 p7
-			__replace_text_in_alt_repo "/^ *#/! s!\[p6\]![updates]!g"
+	load_helper epm-repofix
+	pkg_filenames= epm_repofix
+	__replace_text_in_alt_repo "/^ *#/! s!\[p[6-9]\]![updates]!g"
+}
+
+__update_to_the_distro()
+{
+	__alt_repofix
+	case "$1" in
+		p7)
 			docmd epm update || fatal
-			docmd epm install apt rpm apt-conf-branch || fatal "Check an error and run epm release-upgrade again"
+			docmd epm install apt rpm apt-conf-branch altlinux-release-p7 || fatal "Check an error and run epm release-upgrade again"
+			__alt_repofix
 			__replace_text_in_alt_repo "/^ *#/! s!\[updates\]![p7]!g"
 			docmd epm update || fatal
 			docmd epm upgrade || fatal "Check an error and run epm release-upgrade again"
@@ -76,18 +85,14 @@ __update_alt_repo_to_next_distro()
 			info "Done."
 			info "Run epm release-upgrade again for update to p8"
 			;;
-		p7)
-			docmd epm install apt-conf-branch altlinux-release-p7 || fatal
-			load_helper epm-repofix
-			pkg_filenames= epm_repofix
-			__replace_alt_version_in_repo p7 p8
-			__replace_text_in_alt_repo "/^ *#/! s!\[p7\]![updates]!g"
+		p8)
 			docmd epm update || fatal
-			if ! docmd epm install apt rpm apt-conf-branch ; then
+			if ! docmd epm install apt rpm apt-conf-branch altlinux-release-p8 ; then
 				# error: execution of %post scriptlet from glibc-core-2.23-alt1.eter1
 				docmd epm erase glibc-core-2.17 || fatal "Check an error and run epm release-upgrade again"
-				docmd epm install apt rpm apt-conf-branch || fatal "Check an error and run epm release-upgrade again"
+				docmd epm install apt rpm apt-conf-branch altlinux-release-p8 || fatal "Check an error and run epm release-upgrade again"
 			fi
+			__alt_repofix
 			__replace_text_in_alt_repo "/^ *#/! s!\[updates\]![p8]!g"
 			docmd epm update || fatal
 			if is_installed systemd && is_active_systemd systemd ; then
@@ -98,8 +103,53 @@ __update_alt_repo_to_next_distro()
 			docmd epm update-kernel || fatal
 			info "Done."
 			;;
+		Sisyphus)
+			docmd epm update || fatal
+			docmd epm install apt rpm apt-conf-sisyphus altlinux-release-sisyphus || fatal "Check an error and run again"
+			#__replace_text_in_alt_repo "/^ *#/! s!\[updates\]![alt]!g"
+			__alt_repofix
+			docmd epm update || fatal
+			docmd epm upgrade || fatal "Check an error and run epm release-upgrade again"
+			docmd epm update-kernel || fatal
+			info "Done."
+			;;
+		*)
+	esac
+}
+
+__update_alt_to_next_distro()
+{
+	local FROMTO=$(echo "$*" | sed -e "s| | to |")
+	info
+ 	case "$*" in
+		"p6"|"p6 p7")
+			info "Upgrade $DISTRNAME from p6 to p7 ..."
+			docmd epm install apt-conf-branch || fatal
+			__replace_alt_version_in_repo p6/branch/ p7/branch/
+			__update_to_the_distro p7
+			;;
+		"p7"|"p7 p8")
+			info "Upgrade $DISTRNAME from p7 to p8 ..."
+			docmd epm install apt-conf-branch altlinux-release-p7 || fatal
+			__replace_alt_version_in_repo p7/branch/ p8/branch/
+			__update_to_the_distro p8
+			;;
+		"Sisyphus p8")
+			info "Downgrade $DISTRNAME from Sisyphus to p8 ..."
+			docmd epm install apt-conf-branch || fatal
+			__replace_alt_version_in_repo Sisyphus/ p8/branch/
+			__update_to_the_distro p8
+			;;
+		"p8 Sisyphus")
+			info "Upgrade $DISTRNAME from p8 to Sisyphus ..."
+			docmd epm install apt-conf-branch || fatal
+			docmd epm upgrade || fatal
+			__alt_repofix
+			__replace_alt_version_in_repo p8/branch/ Sisyphus/
+			__update_to_the_distro Sisyphus
+			;;
 		*)
-			info "Have no idea how to update from $DISTRNAME $DISTRVERSION."
+			warning "Have no idea how to update from $DISTRNAME $DISTRVERSION."
 			info "Try run f.i. # epm release-upgrade p8"
 			return 1
 	esac
@@ -113,27 +163,22 @@ epm_release_upgrade()
 
 	case $DISTRNAME in
 	ALTLinux)
+		__alt_repofix
 		docmd epm update
 		docmd epm install apt rpm
-		case "$pkg_filenames" in
-		"p7")
-			DISTRVERSION=p6 __update_alt_repo_to_next_distro
-			;;
-		"p8")
-			DISTRVERSION=p7 __update_alt_repo_to_next_distro
-			;;
-		"Sisyphus")
-			fatal "Does not realized yet"
-			;;
-		*)
-			if [ "$DISTRVERSION" = "Sisyphus" ] || [ -z "$DISTRVERSION" ] ; then
-				# try to detect current release by repo
-				DISTRVERSION=$(__detect_alt_release_by_repo) __update_alt_repo_to_next_distro
-			else
-				__update_alt_repo_to_next_distro
-			fi
-			;;
-		esac
+
+		# try to detect current release by repo
+		if [ "$DISTRVERSION" = "Sisyphus" ] || [ -z "$DISTRVERSION" ] ; then
+			DISTRVERSION="$(__detect_alt_release_by_repo)"
+			[ "$DISTRVERSION" != "Sisyphus" ] && info "Detected running $DISTRNAME $DISTRVERSION (according to using repos)"
+		fi
+
+		# check forced target
+		if [ -n "$pkg_filenames" ] ; then
+			[ "$(__wcount $pkg_filenames)" = "1" ] || fatal "Too many args: $pkg_filenames"
+		fi
+
+		__update_alt_to_next_distro $DISTRVERSION $pkg_filenames
 		return
 		;;
 	*)
diff --git a/bin/epm-repofix b/bin/epm-repofix
index 1fe8decb..ab65b938 100644
--- a/bin/epm-repofix
+++ b/bin/epm-repofix
@@ -29,8 +29,10 @@ __fix_apt_sources_list()
 		# TODO: only for uncommented strings
 		#sed -i -r -e "$SUBST_ALT_RULE" $i
 		regexp_subst "/^ *#/! $SUBST_ALT_RULE" $i
+
+		# add signs
 		local br
-		for br in p6 p7 p8 ; do
+		for br in $DISTRVERSION ; do
 			# sed -r -e "/ALTLinux\/p8\/branch/s/rpm *([fhr])/rpm [p8] \1/"
 			regexp_subst "/ALTLinux\/$br\/branch/s/^rpm *([fhr])/rpm [$br] \1/" $i
 			regexp_subst "/Etersoft\/$br\/branch/s/^rpm *([fhr])/rpm [etersoft] \1/" $i
-- 
2.24.1