#!/bin/bash
# This script was auto generated using the code-cleanup package http://forumubuntusoftware.info/viewtopic.php?f=23&t=9480
# Do a search and replace for ultimateedition.info followed by a search and replace smt with the name of your program & delete this line.

# ==============================================================================
# title			:smt
# description	:YOURDESCRIPTION
# author		:theemahn <theemahn@ultimateedition.info>
# date			:09/24/2024
# version		:1.0
# usage			:smt --help
# manual		:man smt
# notes			:See change-log below for further information.
# ==============================================================================
# Change-log: 1.0: unreleased
# ==============================================================================

# Modify the below information to correlate with the software you are designing.
APPNAME="smt"
PROGNAME="smt"
PROGRAMMER="TheeMahn"
BUILDDATE="09/24/2024"
VERSION="1.0"
DOMAIN="ultimateedition.info"
WEBSITE="http://${DOMAIN}/"
EMAIL="<${PROGRAMMER}@${DOMAIN}>"


# Pull in common software
if [[ -f "ultimate-common" ]]; then
	source ultimate-common
elif [[ -f "/usr/share/ultimate_edition/ultimate-common" ]]; then
	source /usr/share/ultimate_edition/ultimate-common
else
	echo "No Ultimate Edition common source. Please install ultimate-edition-common."
	exit 0;
fi

# Turn off end user prompting
export DEBIAN_FRONTEND=noninteractive

# Help system - I have re-wrote this section to work with code-cleanup.
Help () {

	if [[ "${2}" == "" ]]; then
		VersionDump
		PRAM="ALL"
	else
		if ! [[ "${3}" ]]; then
			VersionDump
			PRAM="${2}"
		else
			PRAM="${2}"
		fi
	fi

	case "${PRAM}" in
		ALL)
		Encapsulate "Usage: ${PROGNAME} -<-COMMAND> [OPTION]"
		FullBar
		Encapsulate "Mandatory arguments to long options are identical for short options."
		Encapsulate "  possible commands..."
		Encapsulate " "
		Encapsulate "  -h       --help        this help message."
		Encapsulate "  -v       --version     dump version info and exit."
		Encapsulate " "
		FullBar
		Center "${PROGNAME} --help [COMMAND] for further information."
		FullBar;;

		# Version Help
		ALL|v|version)
		Encapsulate "Usage version;"
		FullBar
		Center "${PROGNAME} -v"
		FullBar
		Encapsulate "Displays ${PROGNAME}s version number and exits."
		FullBar;;

		# Help Help
		ALL|h|help|\?)
		Encapsulate "Usage Help [COMMAND]"
		FullBar
		Center "${PROGNAME} -h [COMMAND]"
		FullBar
		Encapsulate "Displays this message. For futher information ${PROGNAME} help [COMMAND]"
		Encapsulate "or refer to the manpages."
		Encapsulate " "
		Encapsulate "man ${PROGNAME}"
		Encapsulate " "
		Encapsulate "Example: ${PROGNAME} -h version"
		Encapsulate "Will display help about the command switch version"
		FullBar
	esac
	exit 0
}

SMT () {
	if [[ -f "/sys/devices/system/cpu/smt/active" ]]; then
		STATUS=$(cat /sys/devices/system/cpu/smt/active)
	fi
	if [[ "${STATUS}" ]]; then
		Center "SMT STATUS: ${STATUS}"
		if [[ "${1}" == "ENABLE" ]]; then
			Encapsulate "Enabling SMT (Simultaneous Multithreading / Hyper-Threading)"
			echo "on" | sudo tee /sys/devices/system/cpu/smt/control 1>/dev/null
		fi
		if [[ "${1}" == "DISABLE" ]]; then
			Encapsulate "Disabling SMT (Simultaneous Multithreading / Hyper-Threading)"
			echo "off" | sudo tee /sys/devices/system/cpu/smt/control 1>/dev/null
		fi
	else
		Error "Unable to determine current SMT status."
	fi
}

# Command line pre-processor - this section of code is where your functions are called.
shopt -u nullglob
Timer "Start" "${APPNAME}"

case "${1}" in
      -d|disable) VersionDump "$@"; CheckRoot "$@"; SMT "DISABLE"; exit 0;;
      -e|enable) VersionDump "$@"; CheckRoot "$@"; SMT "ENABLE"; exit 0;;
	  -s|status) VersionDump "$@"; SMT "$@"; exit 0;;
      -h|help|-\?) Help "$@"; exit 0;; # TODO: Enhance this section.
      -v|version) VersionDump "$@"; exit 0;;
      *) Help; exit 0;;
    esac


