#!/bin/sh
#
# Copyright (C) 2021  Etersoft
# Copyright (C) 2021  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_repo()
{
	local CMD="$1"
	[ -n "$CMD" ] && shift
	case $CMD in
	"-h"|"--help"|help)               # HELPCMD: help
		get_help HELPCMD $SHAREDIR/epm-repo
cat <<EOF
Examples:
  epm repo set p9
  epm repo add autoimports
  epm repo list
  epm repo change yandex
EOF
		;;
	""|list)                          # HELPCMD: list packages
		load_helper epm-repolist
		epm_repolist "$@"
		;;
	fix)                              # HELPCMD: fix paths in sources lists (ALT Linux only)
		load_helper epm-repofix
		epm_repofix "$@"
		;;
	change)                           # HELPCMD: <mirror>: switch sources to the mirror (supports etersoft/yandex/basealt): rewrite URLs to the specified server
		load_helper epm-repofix
		epm_repofix "$@"
		;;
	set)                              # HELPCMD: <mirror>: remove all existing sources and add mirror for the branch
		epm repo rm all
		epm addrepo "$@"
		;;
	switch)                           # HELPCMD: switch repo to <repo>: rewrite URLs to the repo
		load_helper epm-repofix
		epm_reposwitch "$@"
		;;
	clean)                            # HELPCMD: remove temp. repos (tasks and CD-ROMs)
		# TODO: check for ALT
		sudocmd apt-repo $dryrun clean
		;;
	save)
		load_helper epm-reposave
		epm_reposave "$@"
		;;
	restore)
		load_helper epm-reposave
		epm_reporestore "$@"
		;;
	reset)
		load_helper epm-reposave
		epm_reporeset "$@"
		;;
	add)                              # HELPCMD: add package repo (etersoft, autoimports, archive 2017/12/31); run with param to get list
		load_helper epm-addrepo
		epm_addrepo "$@"
		;;
	Add)                              # HELPCMD: like add, but do update after add
		load_helper epm-addrepo
		epm_addrepo "$@"
		epm update
		;;
	rm|remove)                           # HELPCMD: remove repository from the sources lists (epm repo remove all for all)
		load_helper epm-removerepo
		epm_removerepo "$@"
		;;
	comment)                             # HELPCMD: comment out repository line from the sources lists
		load_helper epm-commentrepo
		epm_commentrepo "$@"
		;;
	*)
		fatal "Unknown command $ epm repo '$CMD'"
		;;
esac

}