#!/bin/bash
# set -x
# ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
# title			:scandrives													▒
# description	:Auto build fstab management script							▒
# author		:Glenn Cady <theemahn@repostorm.com>						   ▒
# date			:07/24/2014													▒
# version		:1.8.6														 ▒
# usage			:repostorm --help											  ▒
# manual		:man scandrives												 ▒
# notes			:See change-log below for further information.				 ▒
# bash_version	:4.2.8(1)-release											  ▒
# ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
# Change-log: 1.8.6:	Currently unreleased / unleashed					   ▒
# ▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
# Version Control
PROGNAME="Scan Drives"
APPNAME="scandrives"
PROGRAMMER="TheeMahn"
BUILDDATE="02/22/2025"
VERSION="2.1.1"
WEBSITE="os-builder.com"
EMAIL="<$PROGRAMMER@$WEBSITE>"
COMMANDEXEC="$*"

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 1;
fi

Main () {
	LIVE=$(getent passwd | grep "ultimate")
	if [[ "${LIVE}" ]]; then
		Encapsulate "Running Live, exiting."
		exit 0 ;
	fi
	DRIVES=();
	DRIVES=$(sudo blkid)
	TDRIVES=$(sudo blkid -t TYPE=vfat -sUUID -ovalue)
	shopt -s nullglob
	shopt -s dotglob
	SAVEIFS=$IFS
	# Change IFS to new line.
	IFS=$'\n'
	DRIVES=($DRIVES)
	TDRIVES=($TDRIVES)
	IFS="$SAVEIFS"

	for EACH in "${DRIVES[@]}"
	do
		#echo "DEBUG: ${EACH}"
		TESTIT=$(echo "${EACH}" | grep -i "UUID_SUB")
		if ! [[ "${TESTIT}" ]]; then
			UUID=$(echo "${EACH}" | grep -o 'UUID=.*')
			UUID=$(echo "${UUID}" | sed "s/UUID=//g" | sed "s/\"//g")
			UUID=$(echo "${UUID}" | cut -d " " -f1)
			TYPE=$(echo "${EACH}" | grep -o 'TYPE.*')
			TYPE=$(echo "${TYPE}" | sed "s/TYPE=//g" | sed "s/\"//g")
			TYPE=$(echo "${TYPE}" | cut -d " " -f1)
			LABEL=$(echo "${EACH}" | grep -o 'LABEL=.*')
			LABEL=$(echo "${LABEL}" | sed "s/LABEL=//g" | sed "s/\"//g")
			LABEL=$(echo "${LABEL}" | cut -d " " -f1)
			if [[ "${LABEL}" ]]; then
				if ! [[ "${TYPE}" == "linux_raid_member" || "${TYPE}" == "exfat" ]]; then
					EXISTS=$(grep -i "${UUID}" /etc/fstab)
					if ! [[ "${EXISTS}" ]]; then
						MOUNTPOINT=$(echo "${LABEL}" | cut -d " " -f2)
						SKIP=0
						for TDRIVE in "${TDRIVES[@]}"
						do
							if [[ "${TDRIVE}" == "${UUID}" ]]; then
								Encapsulate "USB Drive found: ${UUID} not adding to fstab."
								SKIP=1
							fi
						done
						if [[ "${SKIP}" == "0" ]]; then
							if ! [[ -d "/media/${MOUNTPOINT}" ]]; then
								Encapsulate "Creating folder: /media/${MOUNTPOINT}"
								sudo mkdir -p "/media/${MOUNTPOINT}"
								Encapsulate "Chowning folder: /media/${MOUNTPOINT} to ${IUSER}"
								sudo chown ${IUSER}:${IUSER} "/media/${MOUNTPOINT}"
								Encapsulate "Setting permissions of folder: /media/${MOUNTPOINT}"
								sudo chmod 777 -R "/media/${MOUNTPOINT}"
							fi
								Encapsulate "Adding: ${UUID} /media/${MOUNTPOINT} ${TYPE} defaults 0 0 to /etc/fstab"
								echo "UUID=${UUID} /media/${MOUNTPOINT} ${TYPE} defaults 0 0" | sudo tee -a /etc/fstab > /dev/null
							fi
						fi
					fi
				fi
			fi
		done
	Encapsulate "Auto mounting drives."
	sudo mount -a > /dev/null
	sudo systemctl daemon-reload
	if ! [[ "${SUPPRESS}" ]]; then
		Information "$@"
	fi
}

Help () {
	#CONTAINS=$(echo "$*" | grep -w '\-h\|\-\-help')
	if [[ "${DEBUG}" ]]; then
		echo "DEBUGGING HELP FUNCTION: 1:${1} 2:${2} 3:${3} CONTAINS:${CONTAINS}"
	fi
	if ! [[ "${2}" == "" && "${CONTAINS}" ]]; then
		if [[ "${2}" != "" ]]; then
			PRAM="${2}"
		else
			PRAM="ALL"
		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 "  -s   --scan      scans for drive(s) not mounted, detects the file system & automounts them."
		Encapsulate "  -S   --service   installs or removes ${APPNAME} as a service"
		Encapsulate "  -v   --version   dump version info and exit."
		Encapsulate " "
		FullBar
		Center "${PROGNAME} --help [COMMAND] for further information."
		FullBar;;
		h|help)
		FullBar
		Encapsulate "${APPNAME} -h [COMMAND]"
		Encapsulate "Display help for <COMMAND>"
		Encapsulate "  -h   --help      this help message"
		Encapsulate "  -s   --scan      scans for drive(s) not mounted, detects the file system & automounts them."
		Encapsulate "  -S   --service   installs or removes ${APPNAME} as a service"
		Encapsulate "  -v   --version   dump version info and exit."
		Center "${PROGNAME} --help [COMMAND] for further information."
		FullBar;;
		S|service)
		FullBar
		FullBar
		Encapsulate "${APPNAME} -S <on|off>"
		FullBar
		FormatText "Installs or removes based on second switch to set ${APPNAME} as a service. Once set as a service ${APPNAME} will monitor any drive(s) being installed and scan and add to your fstab automatically."
		FullBar
		Encapsulate "Example: ${APPNAME} --service on"
		FormatText "Will install ${PROGNAME} as a service, autoscan & automount drive(s)."
		FullBar;;
		s|scan)
		FullBar
		Encapsulate "Usage scan;"
		Encapsulate "${APPNAME} -s]"
		FullBar
		FormatText "Scans all drive(s) reporting drive(s) not in your fstab. This should be the first step you take when dealing with large amounts of drive(s)."
		FullBar
		Encapsulate "Example: ${APPNAME} -scan"
		Encapsulate "Will scan all drive(s) and report drive(s) not in fstab"
		FullBar;;
		v|version)
		FullBar
		Encapsulate "Usage version;"
		Encapsulate "${APPNAME} -v"
		FullBar
		Encapsulate "Displays ${APPNAME}s version number and exits.";;
		h|help|\?)
		FullBar
		Encapsulate "Usage help;"
		Encapsulate "${APPNAME} -h [COMMAND]"
		FullBar
		Encapsulate "Displays this message. For futher information ${APPNAME} --help <COMMAND>"
		Encapsulate "or refer to the manpages."
		Encapsulate "man ${APPNAME}"
		FullBar
		Encapsulate "Example: ${APPNAME} -h service"
		Encapsulate "Will display help for service function.";;
		*)
		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 "  -s	   --scan		  scans for drive(s) not mounted, detects the file system & automounts them."
		Encapsulate "  -S	   --service	   installs or removes ${APPNAME} as a service"
		Encapsulate "  -v	   --version	   dump version info and exit."
		Encapsulate " ";;
	esac
	exit 0
}

if ! [[ "${SUPPRESS}" ]]; then
	VersionDump "$@"
	Timer "Start" "${APPNAME}"
	if ! [[ "${ISULTIMATE}" ]]; then
		Encapsulate "This software was written to work with Ultimate Edition. You are running ${OS_NAME}."
		Encapsulate "This means the software has NOT been tested for your O/S."
		Encapsulate "Run this at your own risk. The author takes no responsibily if you bork your O/S."
		FullBar
		Encapsulate "Press enter to continue or Ctrl+C to exit"
		FullBar
		read -rs key
	fi
fi

if ! [[ "${IUSER}" == "ultimate" ]]; then
	Encapsulate "User detected as: ${IUSER}"
else
	Error "User detected as ultimate, will not run in a live ISO, exiting."
	exit 0;
fi

Service () {
	ONOFF=$(echo "$@" | grep -i "on")
	if [[ "${ONOFF}" ]]; then
		Encapsulate "Turnning on service for ${APPNAME}."
		sudo systemctl enable scandrives.service
		Encapsulate "Starting service: scandrives.service"
		sudo systemctl start scandrives.service
		Encapsulate "Status:"
		sudo systemctl status scandrives.service
	else
		Encapsulate "Turning off service for ${APPNAME}."
		sudo systemctl disable scandrives.service
	fi
}

case "${1}" in
	-h|--help|-\?) Help "$@";;
	-s|--scan) Main "$@";;
	-S|--service) Service "$@";;
	*) Help "$@";;
esac
