#!/usr/bin/env bash
# Say Hello to Vasile, a modular script to build Redcore Linux packages && ISO images using a clean squashfs + overlayfs chroot
# Main author : Ghiunhan Mamut (aka V3n3RiX)
# LICENSE: GPL v2 Ghiunhan Mamut & Rogentos Team ( original license authors )
# Copyright 2015 Ghiunhan Mamut & Rogentos Team ( original license authors )
# Copyright 2026 Argent Authors
# Dependencies : kernel built with squashfs + overlayfs + loopback support && sys-fs/grub:2 && sys-fs/squashfs-tools && dev-libs/libisoburn && sys-fs/mtools
#

# Import our variables and functions

export AVASILE_VERBOSE="${AVASILE_VERBOSE:-0}"
if [[ "${AVASILE_VERBOSE}" -eq 1 ]]; then
	set -x
fi

source /lib64/avasile/libavasile

# Vasile need root privileges and a proper kernel to run
# Also, running it in live mode is a really bad idea

checkiflive

export AVASILE_SKIP_KEYS="${AVASILE_SKIP_KEYS:-0}"
export AVASILE_BRANCH="${AVASILE_BRANCH:-}"
_avasile_args=()
while [[ $# -gt 0 ]]; do
	case "$1" in
		--skip-keys|--no-keys) AVASILE_SKIP_KEYS=1 ;;
		--branch)
			if [[ -z "$2" || "$2" == --* ]]; then
				eerror "error: --branch needs a branch name, e.g. --branch dev"
				exit 1
			fi
			AVASILE_BRANCH="$2"
			shift
			;;
		--branch=*) AVASILE_BRANCH="${1#--branch=}" ;;
		*) _avasile_args+=("$1") ;;
	esac
	shift
done
set -- "${_avasile_args[@]}"

case $1 in
	--status)
		checkmode
		;;
	--makepkg)
		kernelconfig
		makepkg
		;;
	--makeiso)
		kernelconfig
		makeiso "$@"
		;;
	--resetmode)
		resetmode
		;;
	--regen-keys)
		regen_pkg_keys
		;;
	--usermode)
		binmode
		[[ -n "${AVASILE_BRANCH}" ]] && switch_repo_branch
		;;
	--devmode)
		mixedmode
		[[ -n "${AVASILE_BRANCH}" ]] && switch_repo_branch
		;;
	--srcmode)
		srcmode
		[[ -n "${AVASILE_BRANCH}" ]] && switch_repo_branch
		;;
	--custom)
		srccustom
		[[ -n "${AVASILE_BRANCH}" ]] && switch_repo_branch
		;;
	--decentralize-user)
		decentralized_user
		;;
	--decentralize-dev)
		decentralized_devel
		;;
	--decentralize-source)
		decentralized_source
		;;
	--decentralize-custom)
		decentralized_custom
		;;
	--decentralize-gentoo)
		decentralized_gentoo
		;;
	--help)
		showhelp
		;;
	""|--force|gentoo|argent-ws|argentws-build)
		if [[ -n "${AVASILE_BRANCH}" ]]; then
			checkroot
			switch_repo_branch "$@"
		else
			eerror "error: no operation specified, use --help for help"
		fi
		;;
	*)
		eerror "error: no operation specified, use --help for help"
		;;
esac

exit 0
