#!/bin/sh
# emk - everest make - clear and concise build system
# for Everest Linux - build script template

# Output should follow this general format:
# emk INFO | output here
# emk WARN | output here
# emk ERR  | output here

# Colored output should be impletemted where possible.
# In /etc/emk.conf, colors are defined by variable, ex:
# ${red} 	for errors
# ${yellow}	for warnings
# ${blue}	for general info
# ${reset}	for removing colors

# Define working directory and source directory
# WORKING_DIR should not be changed.
# SRC_DIR should be changed to a directory which contains all
# source files and subdirectories
WORKING_DIR=$(pwd)
SRC_DIR=$(pwd)/../src # from working dir, go back and go into src

# Parse a local file containing settings for the installation
printf "emk ${blue}INFO${reset} | parsing install.conf\n"
	. ${WORKING_DIR}/install.conf
		if [ "$?" != 0 ]; then
			printf "emk ${red}ERR${reset}  | install.conf not found\n"
			exit 1
		fi

# Define functions for different tasks

### NECESSARY FUNCTIONS ################################################################

### default function ###
# $ emk make
# If package needs to be compiled from source code,
# put needed commands here, else leave output as is
compile_pkg() {
	printf "emk ${yellow}WARN${reset} | no jobs to be done for task ''\n"
	exit 0
}

### install function ###
# $ emk install
# This should be used to install binaries just compiled by emk,
# or prebuilt binaries
install_pkg() {
	printf "emk ${blue}INFO${reset} | starting task 'install esv'\n"
	printf "emk ${blue}INFO${reset} | installing binary 'esv'\n"
	mv -v $SRC_DIR/esv/esv $PREFIX/bin
		if [ "$?" != 0 ]; then
			printf "emk ${yellow}WARN${reset} | using fallback prefix\n"
			mv -v $SRC_DIR/esv/esv $PREFIX
		fi
	printf "emk ${blue}INFO${reset} | installing configuration file 'esv.conf'\n"
	mv -v $SRC_DIR/esv/esv.conf $CONFDIR
		if [ "$?" != 0 ]; then
			printf "emk ${red}ERR${reset}  | $CONFDIR does not exist\n"
			exit 1
		fi
	printf "emk ${blue}INFO${reset} | finished task 'install esv'\n"
	printf "emk ${blue}INFO${reset} | starting task 'install gpc'\n"
	printf "emk ${blue}INFO${reset} | installing binary 'gpc'\n"
	mv -v $SRC_DIR/gpc/gpc $PREFIX/bin
		if [ "$?" != 0 ]; then
			printf "emk ${yellow}WARN${reset} | using fallback prefix\n"
			mv -v $SRC_DIR/gpc/gpc $PREFIX
		fi
	printf "emk ${blue}INFO${reset} | installing configuration file 'gpc.conf'\n"
	mv -v $SRC_DIR/gpc/gpc.conf $CONFDIR
		if [ "$?" != 0 ]; then
			printf "emk ${red}ERR${reset}  | $CONFDIR does not exist\n"
			exit 1
		fi
	printf "emk ${blue}INFO${reset} | finished task 'install gpc'\n"
	printf "emk ${yellow}WARN${reset} | skipping task 'install emk'\n"
	printf "emk ${yellow}WARN${reset} | skipping task 'install ecrypt\n"
	printf "emk ${blue}INFO${reset} | all tasks finished\n"
	exit 0
}

### OPTIONAL FUNCTIONS #################################################################

### clean function ###
# $ emk clean
# Clean all files generated by other commands
clean_pkg() {
	printf "emk ${yellow}WARN${reset} | no jobs to be done for task 'clean'\n"
	exit 0
}

### distclean function ###
# $ emk distclean
# Clean all files not included in the source
# There are multiple ways of doing this -- experiment
# and see which works the best for you
distclean_pkg() {
	printf "emk ${yellow}WARN${reset} | no jobs to be done for task 'distclean'\n"
	exit 0
}

### custom functions ###
# Add any desired custom functions
# customfunc_pkg() {}

########################################################################################

# Command line flags
# These should not be modified unless adding support for custom funtions

case $1 in
	showhelp) # Put a list of all available tasks here
		#printf "make - compile program\n"
		printf "install - install every tool under this repository\n"
		#printf "clean - clean all files generated by make\n"
		#printf "distclean - clean all files not in the source tree\n"
		#exit 0
	;;
	make)
		compile_pkg "$@"
		exit 0
	;;
	install)
		install_pkg "$@"
		exit 0
	;;
	clean)
		clean_pkg "$@"
		exit 0
	;;
	distclean)
		distclean_pkg "$@"
		exit 0
	;;
	#*)
	#	printf "emk ${red}ERR${reset}  | no such task exists\n"
	#	exit 1
	#;;
esac
