#!/bin/sh
#
# Copyright (C) 2012, 2017  Etersoft
# Copyright (C) 2012, 2017  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/>.
#

load_helper epm-sh-altlinux

__epm_addrepo_altlinux()
{
	local repo="$@"
	case "$1" in
		etersoft)
			info "add Etersoft's addon repo"
			load_helper epm-query
			epm install --skip-installed apt-conf-etersoft-common apt-conf-etersoft-hold
			# TODO: ignore only error code 22 (skipped) || fatal
			local branch="$DISTRVERSION/branch"
			[ "$DISTRVERSION" = "Sisyphus" ] && branch="$DISTRVERSION"
			# FIXME
			[ -n "$DISTRVERSION" ] || fatal "Empty DISTRVERSION"
			# TODO: func?
			local arch=$(uname -m)
			[ "$arch" = "i686" ] && arch="i586"
			# TODO: use apt-repo add ?
			echo "" | sudocmd tee -a /etc/apt/sources.list
			echo "# added with eepm addrepo etersoft" | sudocmd tee -a /etc/apt/sources.list
			echo "rpm [etersoft] http://download.etersoft.ru/pub/Etersoft LINUX@Etersoft/$branch/$arch addon" | sudocmd tee -a /etc/apt/sources.list
			if [ "$arch" = "x86_64" ] ; then
				echo "rpm [etersoft] http://download.etersoft.ru/pub/Etersoft LINUX@Etersoft/$branch/$arch-i586 addon" | sudocmd tee -a /etc/apt/sources.list
			fi
			echo "rpm [etersoft] http://download.etersoft.ru/pub/Etersoft LINUX@Etersoft/$branch/noarch addon" | sudocmd tee -a /etc/apt/sources.list
			repo="$DISTRVERSION"
			return 0
			;;
		autoimports)
			[ -n "$DISTRVERSION" ] || fatal "Empty DISTRVERSION"
			repo="$repo.$(echo "$DISTRVERSION" | tr "[:upper:]" "[:lower:]")"
			;;
		archive)
			datestr="$2"
			echo "$datestr" | grep -Eq "^20[0-2][0-9]/[01][0-9]/[0-3][0-9]$" || fatal "use follow date format: 2017/12/31"
			# TODO: func?
			local arch=$(uname -m)
			[ "$arch" = "i686" ] && arch="i586"
			echo "" | sudocmd tee -a /etc/apt/sources.list
			local distrversion="$(echo "$DISTRVERSION" | tr "[:upper:]" "[:lower:]")"
			local rpmsign='[alt]'
			[ "$distrversion" != "sisyphus" ] && rpmsign="[$distrversion]"
			echo "rpm $rpmsign http://ftp.altlinux.org/pub/distributions archive/$distrversion/date/$datestr/$arch classic" | sudocmd tee -a /etc/apt/sources.list
			if [ "$arch" = "x86_64" ] ; then
				echo "rpm $rpmsign http://ftp.altlinux.org/pub/distributions archive/$distrversion/date/$datestr/$arch-i586 classic" | sudocmd tee -a /etc/apt/sources.list
			fi
			echo "rpm $rpmsign http://ftp.altlinux.org/pub/distributions archive/$distrversion/date/$datestr/noarch classic" | sudocmd tee -a /etc/apt/sources.list
			return 0
			;;
	esac

	assure_exists apt-repo

	if tasknumber "$repo" >/dev/null ; then
		sudocmd apt-repo add $(tasknumber "$repo")
		return
	fi

	if [ -z "$repo" ] ; then
		info "Add branch repo. Use follow params:"
		sudocmd apt-repo add branch
		echo "etersoft (for LINUX@Etersoft repo)"
		echo "archive 2018/02/09 (for archive from that date)"
		return
	fi

	sudocmd apt-repo add "$repo"

}

epm_addrepo()
{
local repo="$(eval echo "$quoted_args")"

case $DISTRNAME in
	ALTLinux)
		__epm_addrepo_altlinux $repo
		return
		;;
esac

case $PMTYPE in
	apt-dpkg)
		assure_exists apt-add-repository software-properties-common
		if echo "$repo" | grep -q "https://" ; then
			assure_exists apt-transport-https
		fi
		sudocmd apt-add-repository "$repo"
		info "Check file /etc/apt/sources.list if needed"
		;;
	aptitude-dpkg)
		info "You need manually add repo to /etc/apt/sources.list (TODO)"
		;;
	yum-rpm)
		assure_exists yum-utils
		sudocmd yum-config-manager --add-repo "$repo"
		;;
	urpm-rpm)
		sudocmd urpmi.addmedia "$repo"
		;;
	zypper-rpm)
		sudocmd zypper ar "$repo"
		;;
	emerge)
		sudocmd layman -a "$repo"
		;;
	pacman)
		info "You need manually add repo to /etc/pacman.conf"
		# Only for alone packages:
		#sudocmd repo-add $pkg_filenames
		;;
	npackd)
		sudocmd npackdcl add-repo --url="$repo"
		;;
	slackpkg)
		info "You need manually add repo to /etc/slackpkg/mirrors"
		;;
	*)
		fatal "Have no suitable command for $PMTYPE"
		;;
esac

}