# Copyright Argent Linux Authors 2015-2025
# License: GPLv2

#!/usr/bin/env bash

source /usr/lib64/epkg/libepkg

if [[ "$1" != "search" && "$1" != "s" ]]; then
    checkroot
fi

checksystemmode

action=$1
if [ $# -gt 0 ]; then
    shift
fi

case "$action" in
    install|i)
        if [ "$1" == "--check" ] ; then
            checksync
            shift
            emerge -aq "${@}"
        else
            emerge -aq "${@}"
        fi
        ;;
	install-no-collision)
		if [ "$1" == "--check" ] ; then
			checksync
			shift
			FEATURES="parallel-fetch -collision-protect -protect-owned candy compressdebug splitdebug" emerge -av "$@"
		else
			FEATURES="parallel-fetch -collision-protect -protect-owned candy compressdebug splitdebug" emerge -av "$@"
		fi
		;;
	install-bin-pkg|ibin)
		if [ "$1" == "--check" ] ; then
			checksync
			shift
			EMERGE_DEFAULT_OPTS="${EMERGE_DEFAULT_OPTS} --getbinpkgonly " emerge -av "$@"
		else
			EMERGE_DEFAULT_OPTS="${EMERGE_DEFAULT_OPTS} --getbinpkgonly " emerge -av "$@"
		fi
		;;
	remove|r)
        if [ "$1" == "--check" ] ; then
            checksync
            shift
            emerge --depclean -av "$@"
        else
            emerge --depclean -av "$@"
        fi
        ;;
    configure|cfg)
        if [ "$1" == "--check" ] ; then
            checksync
            shift
            emerge --config "$@"
        else
            emerge --config "$@"
        fi
        ;;
	backuppkg)
		PKGDIR="${PKGDIR:-}" backuppkg "$@"
		;;
	forceremove)
		if [ "$1" == "--check" ] ; then
			checksync
			emerge --unmerge -av "$@"
		else
			emerge --unmerge -av "$@"
		fi
		;;
	clean|c)
		if [ "$1" == "--check" ] ; then
			checksync
			shift
			emerge --depclean -av
		else
			emerge --depclean -av
		fi
		;;
	cleanoutdated)
		cleanoutdated
		;;
	cleanlocalpkg)
		cleanlocalpkg
		;;
	upgrade|ug)
		if [ "$1" == "--check" ] ; then
			checksync
			shift
			PKGDIR="${PKGDIR:-}" DISTDIR="${DISTDIR:-}" set_var_cache_dir
			emerge -uDaN @system @world "$@"
		else
			PKGDIR="${PKGDIR:-}" DISTDIR="${DISTDIR:-}" set_var_cache_dir
			emerge -uDaN @system @world "$@"
		fi
		;;
	upgrade-full|ugf)
		if [ "$1" == "--check" ] ; then
			checksync
			shift
			emerge -uDvaN --with-bdeps=y @system @world "$@" --backtrack=150
		else
			emerge -uDvaN --with-bdeps=y @system @world "$@" --backtrack=150
		fi
		;;
	upgrade-specific)
		if [ "$1" == "--check" ] ; then
            checksync
            shift
            emerge -uDvaN "$@" --backtrack=150
        else
            emerge -uDvaN "$@" --backtrack=150
        fi
		;;
	upgrade-nocol)
		checksync
		FEATURES="parallel-fetch -collision-protect -protect-owned candy compressdebug splitdebug" emerge -uDvaN --with-bdeps=y @system @world "$@" --backtrack=150
		;;
	autoinstall|ai)
		if [ "$1" == "--update" ] ; then
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			emerge -q "$@"
		else
			emerge -q "$@"
		fi
		;;
	autoinstall-no-collision)
		if [ "$1" == "--update" ] ; then
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			FEATURES="parallel-fetch -collision-protect -protect-owned candy compressdebug splitdebug" emerge -v "$@"
		else
			FEATURES="parallel-fetch -collision-protect -protect-owned candy compressdebug splitdebug" emerge -v "$@"
		fi
		;;
	autoinstall-bin-pkg|aibin)
		if [ "$1" == "--update" ] ; then
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			EMERGE_DEFAULT_OPTS="${EMERGE_DEFAULT_OPTS} --getbinpkgonly " emerge -v "$@"
		else
			EMERGE_DEFAULT_OPTS="${EMERGE_DEFAULT_OPTS} --getbinpkgonly " emerge -v "$@"
		fi
		;;
	autoremove|ar)
		if [ "$1" == "--update" ] ; then
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			emerge --depclean -v "$@"
		else
			emerge --depclean -v "$@"
		fi
		;;
	reinstall-depend)
		reinstall_depend "$@"
		;;
	backupallpkg)
		PKGDIR="${PKGDIR:-}" backupallpkg $( qlist -IvC ) "$@"
		;;
	module-rebuild)
		EMERGE_DEFAULT_OPTS="--rebuilt-binaries " emerge -av1 @module-rebuild
		;;
	compile)
		compilepkg "$@"
		;;
	compile-preserved)
		EMERGE_DEFAULT_OPTS=" --rebuilt-binaries" emerge @preserved-rebuild -av
		;;
	recompile|re)
		recompilepkg "$@"
		;;
	recompilepkg)
		recompilepkg "$@"
		;;
	recompileallpkg)
		if [[ "${1}" == "--update" ]] ; then
			argentsync
			shift
			recompileallpkg "$@"
		else
			recompileallpkg "$@"
		fi
		;;
	recompile-depend)
		recompile_depend "$@"
		;;
	recompile-depend-new)
		recompile_depend_new "$@"
		;;
	src-newuse)
		src_newuse "$@"
		;;
	buildpkgonly)
		PKGDIR="${PKGDIR:-}" buildpkgonly "$@"
		;;
	buildinstall)
		PKGDIR="${PKGDIR:-}" buildinstall "$@"
		;;
	autobuildinstall)
		PKGDIR="${PKGDIR:-}" autobuildinstall "$@"
		;;
	autoreinstall-depend)
		autoreinstall_depend "$@"
		;;
	automodule-rebuild)
		EMERGE_DEFAULT_OPTS="--rebuilt-binaries " emerge -v1 @module-rebuild
		;;
	autocompile)
		if [[ "${1}" == "--update" ]] ; then
			argentsync
			shift
			autocompilepkg "$@"
		else
			autocompilepkg "$@"
		fi
		;;
	autocompile-preserved)
		if [ "$1" == "--update" ] ; then
			checksync
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			EMERGE_DEFAULT_OPTS=" --rebuilt-binaries" emerge @preserved-rebuild -v
		else
			EMERGE_DEFAULT_OPTS=" --rebuilt-binaries" emerge @preserved-rebuild -v
		fi
		;;
	autorecompile|are)
		if [[ "${1}" == "--update" ]] ; then
			argentsync
			shift
			autorecompilepkg "$@"
		else
			autorecompilepkg "$@"
		fi
		;;
	autorecompilepkg)
		if [[ "${1}" == "--update" ]] ; then
			argentsync
			shift
			autorecompilepkg "$@"
		else
			autorecompilepkg "$@"
		fi
		;;
	autorecompile-depend)
		autorecompile_depend "$@"
		;;
	autorecompile-depend-new)
		cleanoutdated
		autorecompile_depend_new "$@"
		;;
	autorecompileallpkg)
		argentsync
		cleanoutdated
		autorecompileallpkg
		;;
	autoforceremove)
		argentsync
		emerge --unmerge -v "$@"
		;;
	autoclean|ac)
		if [ "$1" == "--update" ] ; then
			checksync
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			emerge --depclean -q
		else
			emerge --depclean -q
		fi
		;;
	autobuildpkgonly)
        if [ "$1" == "--update" ] ; then
            argentsync
            cleanoutdated
            shift
            PKGDIR="${PKGDIR:-}" autobuildpkgonly "$@"
        else
            PKGDIR="${PKGDIR:-}" autobuildpkgonly "$@"
        fi
        ;;
	autoupgrade|aug)
		if [ "$1" == "--update" ] ; then
			checksync
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			emerge -uDN @system @world "$@"
		else
			emerge -uDN @system @world "$@"
		fi
		;;
	autoupgrade-full|augf)
		if [ "$1" == "--update" ] ; then
			checksync
			if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
				fetchminimalsync
			fi
			shift
			emerge -uDvN --with-bdeps=y @system @world "$@"
		else
			emerge -uDvN --with-bdeps=y @system @world "$@"
		fi
		;;
	autoupgrade-nocol)
		if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
			fetchminimalsync
		fi
		FEATURES="parallel-fetch -collision-protect -protect-owned candy compressdebug splitdebug" emerge -uDvN --with-bdeps=y @system @world "$@"
		;;
	upgrade-specific)
		emerge -uDvN "$@"
		;;
	remove-orphans)
		remove_orphans
		;;
	fetchsourcepkg)
		fetchsourcepkg "$@"
		;;
	fetchbinpkg)
		fetchbinpkg "$@"
		;;
	search|s)
		emerge -s "$@"
		;;
    update|u)
        PKGDIR="${PKGDIR:-}" DISTDIR="${DISTDIR:-}" set_var_cache_dir
        if [ "$1" == "--clean" ] ; then
            shift
            if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
                fetchminimalsync
                git_clean_history
            else
                emerge --sync
            fi
        else
            if [[ "${EPKG_DISTRO_SET}" == "argent" ]]; then
                fetchminimalsync
            else
                emerge --sync
            fi
        fi
        ;;
	devsync)
		argentsync
		;;
	force-sync)
		forcesync
		;;
	sysinfo)
		emerge --info
		;;
	resume)
		emerge --resume -v1
		;;
	oneshot|o)
		emerge --oneshot -va1 "$@"
		;;
	autooneshot|ao)
		emerge --oneshot -v1 "$@"
		;;
	*)
		cat <<-"EOH"
			Usage: epkg command [package(s)]

			epkg is a simple wrapper around portage, gentoolkit, and portage-utils that provides an
			apt-get/yum-alike interface to these commands, to assist people transitioning from
			Debian/RedHat-based systems to Gentoo. It is a fork of unmaintained github.com/jdhore/epkg.

			Commands :
				install|i - Install new packages
				remove|r - Remove packages safely
				configure|cfg - Configure packages as per ebuilds settings
				backuppkg - Backup specific packages with own configurations for later use
				forceremove - *Unsafely* remove packages
				clean|c - Remove packages that are no longer needed
				cleanoutdated - Remove local saved and outdated packages
				cleanlocalpkg - Remove all locally saved package ( including index ) usually in /usr/portage/packages

				upgrade|ug -  Upgrade system. No build dependencies are upgraded
				upgrade-full|ugf - Upgrade system. All build dependencies included for upgrade
				upgrade-nocol - Upgrade system without collision protection
				upgrade-specific - Upgrade specific package(s)
				install-no-collision - Install a package forcefully without collision protection
				install-bin-pkg|ibin - Install only a precompiled binary package if available
				compile-preserved - Recompile packages that are categorized as preserved-rebuilds

				autoinstall|ai - Install new packages (no confirmation)
				autoinstall-no-collision - Install new package forcefully without collision protection ( no confirmation )
				autoinstall-bin-pkg|aibin - Install only a precompiled binary package if available ( no confirmation )
				autoremove|ar - Remove packages safely (no confirmation)
				reinstall-depend - Reinstall all dependencies of given package(s) based on current version/revision
				src-newuse - Check a specific package if it has to be reinstalled due to USE flag change ( portage-specific )
				devsync - Upgrade all Portage overlay structure with needed ebuilds for development / compilation
				module-rebuild - Rebuild all kernel modules that are currently installed

				backupallpkg - Backup the whole system. Usually in /usr/portage/packages
				recompile|re - Recompile and install packages with self-defined options ( man portage )
				recompilepkg - Recompile and install packages with self-defined options ( man portage )
				recompileallpkg - Recompile, install and save all the packages locally ( usually in /usr/portage/packages/ )
				recompile-depend - Compile/Recompile all currently installed packages that depend on given package(s)
				recompile-depend-new - Compile/Recompile all newly installed packages that depend on given package(s)
				buildpkgonly - Build/Recompile package(s) and export them locally ( usually in /usr/portage/packages )
				buildinstall - Recompile/Build package(s) and install them immediately ( usually in /usr/portage/packages )

				autoupgrade|ug - Upgrade system (no confirmation). No build dependencies are upgraded
				autoupgrade-full|ugf - Upgrade system ( no confirmation ). All build dependencies included for upgrade
				autoupgrade-nocol - Upgrade system without collision protection ( no confirmation )
				autoupgrade-specific - Upgrade specific package(s) ( no confirmation )

				autoreinstall-depend - Reinstall all dependencies of given package(s) based on current version/revision ( no confirmation )
				autorecompile|are - Recompile packages with self-defined options ( no confirmation )
				autorecompilepkg - Recompile packages with self-defined options ( no confirmation )
				autorecompileallpkg - Recompile and save all the packages in the system ( no confirmation )
				autorecompile-depend - Compile/Recompile all currently installed packages that depend on given package(s) ( no confirmation )
				autorecompile-depend-new - Compile/Recompile all newly-installed packages that depend on given package(s) ( no confirmation )
				autoforceremove - *Unsafely* remove packages (no confirmation)
				autocompile-preserved - Recompile packages that are categorized as preserved-rebuilds
				automodule-rebuild - Rebuild all kernel modules that are currently installed ( no confirmation )

				autoclean - Remove packages that are no longer needed (no confirmation)
				autobuildpkgonly - Build/Recompile package(s) and export them locally ( no confirmation )
				autobuildinstall - Build/Recompile package(s) and autoinstall them immediately ( no confirmation )

				remove-orphans - Remove orphaned packages ( with confirmation )
				fetchbinpkg - Downloads only binaries from remote
				fetchsourcepkg - Downloads the source of the program. Usually in /usr/portage/distfiles/
				search - Search for packages
				update - Resync portage tree, portage config && argent-ws overlay
				sysinfo - Display information about installed core packages and portage configuration
				resume - Resume the last failed emerge operation(s)
				oneshot|o - Install package(s) as one-shot package(s)
				autooneshot|ao - Install package(s) as one-shot package(s) (no confirmation)
		EOH
		;;
esac
