#!/bin/sh
# emk - simple and concise build system
# Copyright (C) 2022  Everest Linux Developers

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

#. /etc/emk.conf
. $(pwd)/emk.conf

am_i_root() {
	if [[ $(/usr/bin/id -u) -ne 0 ]]; then
        	printf "emk ${yellow}WARN${reset} | not running emk as root\n"
        	printf "emk ${yellow}WARN${reset} | THIS MAY PRODUCE UNEXPECTED RESULTS\n"
        	printf "emk ${yellow}WARN${reset} | however, this is NOT an error\n"
        	printf "emk ${yellow}WARN${reset} | and should not be treated as such.\n"
	else
        	printf "emk ${blue}INFO${reset} | running emk as root\n"
	fi
}

case $1 in
	-h|--help)
		printf "emk - simple and concise build system\n"
		printf "usage: emk [help] [-h/--help] [-v/--version]\n"
		printf "\n"
		printf "emk {help}		show available tasks from emkfile\n"
		printf "emk {-h/--help}		show this mesage and exit\n"
		printf "emk {-v/--version}	show the version and exit\n"
		printf "\n"
		printf "This program is free software.\n"
		printf "See the GNU GPL version 3 for details.\n"
		exit 0
	;;
	-v|--version)
		printf "${blue}emk v1.0.0${reset}\n"
		exit 0
	;;
	help)
		./emkfile showhelp
			if [ "$?" != "0" ]; then
				printf "emk ${red}ERR${reset}  | failed to get help from emkfile.\n"
				exit 1
			fi
	;;
	*)
		am_i_root "$@"
		./emkfile $2
			if [ "$?" != "0" ]; then
				printf "emk ${red}ERR${reset}  | subproccess finished with non-zero status.\n"
				exit 1
			fi	
esac
