#!/bin/sh

# everest-project-init - initialize a project
#
# Copyright (C) 2023   Everest Linux
#
# everest-project-init 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.
#
# everest-project-init 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 everest-project-init.  If not, see <https://www.gnu.org/licenses/>.

red="\033[1;31m"
green="\033[1;32m"
yellow="\033[1;33m"
blue="\033[1;34m"
reset="\033[m"
check="\xE2\x9C\x93"

#
# PARAMETERS:
# "usage: ${0} [-h] [-v] [-i] [-l] DIR LANG \n"
#          ^     ^______________^   ^    ^
#          0             1          2    3
#

usage () {
	printf "${0} - Initialize a project\n"
	printf "usage: ${0} [-h] [-v] [-i] [-l] DIR LANG\n"
	printf "\n"
	printf "${0} {-h --help}	Show this message\n"
	printf "${0} {-v --version}	Show the current version\n"
	printf "${0} {-i --init}	Initialize in DIR with template LANG\n"
	printf "${0} {-l --list}	List all supported language templates\n"
	printf "\n"
	printf "Ensure DIR is a complete directory path.\n"
	printf "\n"
	printf "everest-project-init is free software.\n"
	printf "See the GNU GPL version 3 for details.\n"
}

usage_small() {
	printf "usage: ${0} [-h] [-v] [-i] [-l] DIR LANG\n"
}

mkprojectdir() {
	printf "${blue}[i]${reset} Creating project directory...\n"
	mkdir -v ./${2}
	printf "${green}[${check}]${reset} Directory created.\n"
}

mkskel() {
	printf "${blue}[i]${reset} Creating project directory skeleton...\n"
	case ${3} in
	"C")
		mkdir -v ${2}/{src,include}
	;;
	"Bash")
		mkdir -v ${2}/{src,install,man}
		mkdir -v ${2}/src/{bin,etc,man}
	esac
	printf "${green}[${check}]${reset} Project directory skeleton created.\n"
}
		

dload_license() {
	LICENSE_URL="https://gnu.org/licenses/gpl-3.0.txt"
	printf "${blue}[i]${reset} Using license 'GPL 3' for this project.\n"
	printf "${blue}[i]${reset} Downloading license for project...\n"
	curl ${LICENSE_URL} -o ${2}/LICENSE
	printf "${green}[${check}]${reset} License downloaded.\n"
}

mkheader() {
	printf "${blue}[i]${reset} Generating file header...\n"
	ROOT_URL="https://git.everestlinux.org/EverestLinux/project-init-file-headers/raw/branch/main"
	cd ${2}
	case ${3} in
	"C")
		curl ${ROOT_URL}/template.c -o ${2}/src
		if [ "$?" != 0 ]; then
			printf "${red}[x]${reset} Curl exited with non-zero status.\n"
			exit 1
		fi
	;;
	"Bash")
		curl ${ROOT_URL}/template.sh -o ${2}/src
		if [ "$?" != 0 ]; then
			printf "${red}[x]${reset} Curl exited with non-zero status.\n"
			exit 1
		fi
	;;
	esac
	printf "${blue}[i]${reset} File header generated successfully.\n"
}

case $1 in
	-h|--help)
		usage "$@"
		exit 0
	;;
	-v|--version)
		printf "everest-project-init v0.1rc\n"
		exit 0
	;;
	-i|--init)
		mkprojectdir "$@"
		mkskel "$@"
		dload_license "$@"
		mkheader "$@"
		printf "${green}[${check}]${reset} Project generated successfully.\n"
		exit 0
	;;	
	-l|--list)
		printf "Available templates:\n"
		printf "C\n"
		printf "Bash/POSIX sh\n"
		exit 0
	;;
	-*|--*)
		usage_small "$@"
		exit 1
	;;
	*)
		usage_small "$@"
		exit 1
	;;
esac

usage_small "$@"
exit 1
