#!/bin/sh
#
# Copyright (C) 2013, 2016  Etersoft
# Copyright (C) 2013, 2016  Vitaly Lipatov <lav@etersoft.ru>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

__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"
	confirm "Are these correct changes?" || fatal "Exiting"
	for i in /etc/apt/sources.list /etc/apt/sources.list.d/*.list ; do
		[ -s "$i" ] || continue
		# TODO: only for uncommended strings
		#sed -i -r -e "s!$1/branch!$2/branch!g" $i
		regexp_subst "/^ *#/! s!$1/branch!$2/branch!g" $i
		
		# TODO: start with improve to [p8] - install some package firstly?
		regexp_subst "/^ *#/! s s!\[$1\]![alt]/branch!g" $i
	done
	docmd apt-repo list
}

__update_alt_repo_to_next_distro()
{
	case "$DISTRVERSION" in
		p6)
			__replace_alt_version_in_repo p6 p7
			info "Run epm release-upgrade again for update to p8"
			;;
		p7)
			__replace_alt_version_in_repo p7 p8
			;;
		*)
			info "Have no idea how to update from $DISTRNAME $DISTRVERSION"
			return 1
	esac
}

epm_release_upgrade()
{
	assure_root
	info "Starting upgrade whole system to the next release"
	info "Check also http://wiki.etersoft.ru/Admin/UpdateLinux"

	case $DISTRNAME in
	ALTLinux)
		docmd epm update
		docmd epm install apt rpm
		pkg_filenames= epm_repofix
		__update_alt_repo_to_next_distro || exit
		docmd epm Upgrade
		docmd epm update-kernel
		return
		;;
	*)
		;;
	esac

	case $PMTYPE in
	apt-rpm)
		docmd epm update
		info "Have no idea how to upgrade $DISTRNAME"
		;;
	*-dpkg)
		sudocmd do-release-upgrade -d
		;;
	yum-rpm)
		docmd epm install rpm yum
		sudocmd yum clean all
		# TODO
		showcmd rpm -Uvh http://mirror.yandex.ru/fedora/linux/releases/16/Fedora/x86_64/os/Packages/fedora-release-16-1.noarch.rpm
		docmd epm Upgrade
		;;
	urpm-rpm)
		sudocmd urpmi.removemedia -av
		# TODO
		showcmd urpmi.addmedia --distrib http://mirror.yandex.ru/mandriva/devel/2010.2/i586/
		sudocmd urpmi --auto-update --replacefiles
		;;
	zypper-rpm)
		docmd epm repolist
		# TODO
		# sudocmd zypper rr <номер_репозитория>
		showcmd rr N
		showcmd epm ar http://mirror.yandex.ru/opensuse/distribution/11.1/repo/oss 11.1oss
		showcmd zypper ref
		docmd epm update
		docmd epm install rpm zypper
		docmd epm upgrade
		;;
	pacman)
		epm Upgrade
		;;
	conary)
		epm Upgrade
		;;
	emerge)
		epm Upgrade
		;;
	guix)
		sudocmd guix pull --verbose
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
	esac

}