#!/bin/sh

# Include commonly used functions
. /usr/local/share/sysinfo/common.subr

# Initialize variables to their default values
init_params() {
	# We want to export all following variables
	set -o allexport

	E_NOOPT=1	# No options provided
	E_UNKOPT=2	# No such option supported
	E_NOCONF=3	# Confing file not found

	# Include configuration
	if check_privs /usr/local/etc/sysinfo.conf; then
		. /usr/local/etc/sysinfo.conf
	else
		err $E_NOCONF "Configuration file has not been found."
	fi

	# Set LC_ALL in order to avoid problems with character ranges like [A-Z].
	LC_ALL=C

	# Set colors used in script messages
	if checkyesno COLORS; then
		set_colors 1
	fi

	# No more exporting
	set +o allexport

	VERSION="1.0.1"
	MODS=""
}

# This is being displayed upon error or request
usage() {
	echo -e "${C_CYAN_S}SysInfo v$VERSION by Daniel Gerzo for FreeBSD Operating System${C_CYAN_E}\n"

	cat <<EOF
Usage: `basename $0` [-a] [-c] [-d] [-h] [-v] [-v [level]] module ...
The following options are available
-a		Display complete system information
-c		Toggle colorful output (useful when redirecting the output to the file)
		The default value can be set in the configuration file
-d		Toggle debug mode
		The default value can be set in the configuration file
-h		Print this help
-i		Toggle info messages
		The default value can be set in the configuration file
-v [level]	Verbose level (0-1), default is 0 (no verbose output)
		The default value can be set in the configuration file
-V		Display version and exit
module		Display the system information related to the specified subsystem type
		The following values are supported:
   bios		BIOS information
   cpu		CPU details
   mem		Memory details
   misc		Miscellaneous information
   network	Network related details
   os		Operating system details
   packages	Information about installed packages
   services	Information about system services
   storage	Details about storage system
   system	System information (hardware related)
   user		User related information

The configuration variables can be set in the /usr/local/etc/sysinfo.conf file.
For more information please see the sysinfo(8) manual page.

EOF
}

# Parse the command line
# XXX: may be possible to utilize getopt(1)
parse_cmdline() {
	while [ $# -gt 0 ]; do
		case "$1" in
		# Set the requested verbose level
		-v | --verbose | verbose)
			# Make sure that the supplied argument is a number
			expr $2 : '^[0-9]*$' > /dev/null 2>&1

			# if it is, the next value is verbose level
			if [ $? -eq 0 ]; then
				shift
				set_verbose $1
			# if it isn't it's probably a next argument
			else
			   	set_verbose
			fi
			;;
		# Print usage information
		-h | --help | help)
			usage
			exit 0
			;;
		# Run all modules
		-a | --all | all)
			MODS=$ALLMODS
			;;
		# Set/unset colors
		-c | --color | color)
			if checkyesno COLORS; then
				set_colors 0;
			else
			   	set_colors 1;
			fi
			;;
		-d | --debug | debug)
			if checkyesno DEBUG; then
				export DEBUG="NO"
			else
				export DEBUG="YES"
			fi
			;;
		-i | --info | info)
			if checkyesno INFO_MSGS; then
				export INFO_MSGS="NO"
			else
				export INFO_MSGS="YES"
			fi
			;;
		-V | --version | version)
			echo "SysInfo v$VERSION by Daniel Gerzo"
			exit 0
			;;
		# Everything else / probably list of modules
		*)
			# Check whether this argument is call for a module
			if echo $ALLMODS | grep -wq $1; then
				# we don't want to run the same module more then once
				if ! echo $MODS | grep -wq $1; then
					MODS="$MODS $1"
				fi
			else
				# Check whether this arg isn't a shortened version of a module
				# This means that it must match exactly one name of a module.
				matched=$(ls -l $MODPATH | awk '{ print $9 }' | grep -c "^$1")
				debug "matched: $matched"

				if [ $matched -eq 1 ]; then
					_mod=$(ls -l $MODPATH | awk '{ print $9 }' | grep "^$1")
					MODS="$MODS $_mod"
				else
				# Argument either matched more than one module or didn't match
				# any of them. Display help message and die.
					usage
					err  $E_UNKOPT "Illegal option: $1."
				fi
			fi
			;;
		esac
		shift
	done

	# Make sure at least one module will be run
	if [ -z "${MODS}" ]; then
		usage
		err $E_NOOPT 'Not enough arguments provided.'
	fi
}

#
# set_verbose level
#	Sets the verbose level used throughout the script
#
set_verbose() {
   	if [ -z $1 ]; then VERBOSE=1;
	elif [ $1 -lt 0 ]; then VERBOSE=0;
	else VERBOSE="$1";
	fi

	export VERBOSE;
}

# set default colors used in script
set_colors() {
	case $1 in
	0)
		debug "Unsetting colors"
		unset   C_CYAN_S C_CYAN_E C_GREEN_S C_GREEN_E C_YELLOW_S \
			C_YELLOW_E C_RED_S C_RED_E C_BOLD_S C_BOLD_E \
			C_MAGENTA_S C_MAGENTA_E
		;;
	1)
		debug "Setting colors"
		C_BOLD_S="\033[1m";	C_BOLD_E="\033[0m"
		C_CYAN_S="\e[1;36m";	C_CYAN_E="\e[1;36m\e[0m"
		C_GREEN_S="\e[1;32m";	C_GREEN_E="\e[1;32m\e[0m"
		C_YELLOW_S="\e[1;33m";	C_YELLOW_E="\e[1;33m\e[0m"
		C_RED_S="\e[1;31m";	C_RED_E="\e[1;31m\e[0m"
		C_MAGENTA_S="\e[1;35m";	C_MAGENTA_E="\e[1;35m\e[0m"
		;;
	*)
		debug "Unknown argument for set_colors()"
		;;
	esac
}

#
# run_module module
# 	Runs a module of name module if that module exists and its file
#	is executable
#
run_module() {
	# Safty check
	if [ -x $MODPATH/$1 ]; then
		$MODPATH/$1
	else
		warn "Module $1 does not exist or is not set up properly."
	fi
}

#
# start script
#
init_params
parse_cmdline $@

echo "Generated by SysInfo v$VERSION by Daniel Gerzo"

# Run requested modules
for MOD in ${MODS}; do
	echo
	run_module $MOD
done

echo
