#!/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 se with the name of your program & delete this line.

# ==============================================================================
# title			:se
# description	:YOURDESCRIPTION
# author		:theemahn <theemahn@os-builder.com>
# date			:11/29/2025
# version		:1.0
# usage			:se --help
# manual		:man se
# 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="Super Extract"
PROGNAME="se"
PROGRAMMER="TheeMahn"
BUILDDATE="11/29/2025"
VERSION="1.0"
DOMAIN="os-builder.com"
WEBSITE="${DOMAIN}"
EMAIL="<${PROGRAMMER}@${DOMAIN}>"


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."
	if [[ "${ISARCH}" ]]; then
		Encapsulate "sudo pacman -Syuu ultimate-edition-common"
	else
		Encapsulate "sudo apt-get install ultimate-edition-common"
	fi
	exit 0;
fi
CURRENT="${PWD}"
Exit () {
	if ! [[ "${SUPPRESS}" ]]; then
		Timer "Stop" "${APPNAME}"
		FullBar
	fi
}

trap Exit EXIT

# Turn off end user prompting non Arch
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 "  -t       --test        test functions."
		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;;

		# Test functions
		ALL|x|extract)
		Encapsulate "Usage extract;"
		FullBar
		Center "${PROGNAME} -x"
		FullBar
		Encapsulate "Extracts *.zip *.tar *.gz *.tgz *.bz2 *.tbz2 *.xz *.txz *.rar *.7z *.tar dives into subfolders repeats the process 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
}
Folders () {
	declare -a FOLDERS=( */ )
	for EACH in "${FOLDERS[@]}"
	do
		Encapsulate "Moving to folder: ${EACH}, scanning for compressed files."
		cd "${EACH}"
		SuperExtract "@"
		cd "${CURRENT}"
	done
	exit 0;
}

SuperExtract () {
	declare -i TCOUNT;
	declare -a COMPRESSED=();
	declare -a COMPRESSIONTYPES=();
	TCOUNT=0
	TAR=(type -p tar)
	RAR=(type -p unrar)
	shopt -s nullglob
	COMPRESSED=(*.zip *.tar *.gz *.tgz *.bz2 *.tbz2 *.xz *.txz *.rar *.7z *.pkg.tar.zst)
	COMPRESSIONTYPES=( "zip compressed" "tarball archive" "gunzip archive" "tarball gzip archive" "XZ compression" "Rar (Roshal Archive)" "7zip" "Zstandard FaceBook compression")
	EXTENSION="${filename##*.}"
	if [[ "${#COMPRESSED[@]}" -gt 0 ]]; then
		for EACH in "${COMPRESSED[@]}";
		do
			EXTENSION="${EACH##*.}"
			Encapsulate "Extension: ${EXTENSION}"
			FS=$(type -p stat)
			if [[ "${FS}" ]]; then
				if [[ -f "${EACH}" ]]; then
					FILESIZE=$(stat --printf="%s" "${EACH}")
				fi
			fi

			#FormatNUM " ${TCOUNT} of" "${#ARCHPACKAGES[@]}"
			TOTALBYTES=$((TOTALBYTES+FILESIZE))
			Thousands "${FILESIZE}"
			FILESIZE="${FORMATTED}"
			TCOUNT=$(( TCOUNT +1 ))
			TOTAL="${#COMPRESSED[@]}"
			PCOMP=$((100*TCOUNT / TOTAL))
			Timer "Start" "Extracting Archive: ${EACH} ${TCOUNT} of ${#COMPRESSED[@]}"
			ProgressBar "${PCOMP}" "Extracting Archive: ${EACH}. (${FILESIZE} bytes) ${TCOUNT} of ${#COMPRESSED[@]}"
			case "${EXTENSION}" in
			tar)
				if [[ "${TAR}" ]]; then
					tar xf "${EACH}" >/dev/null
				else
					Encapsulate "Install tar"
				fi;;
			rar)
				unrar x "${EACH}" >/dev/null;;
			zip)
				unzip "${EACH}" >/dev/null;;
			gz)
				gunzip "${EACH}" >/dev/null;;
			*) # Default case for any other input
				echo "That's an interesting color choice!"
			;;
			esac
			Timer "Stop" "Extracting Archive: ${EACH} ${TCOUNT} of ${#COMPRESSED[@]}"
		done
		Thousands "${TOTALBYTES}"
		FILESIZE="${FORMATTED}"
		Encapsulate "Total Extracted ${FILESIZE} bytes."
	fi
	Folders "@"
}

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

case "${1}" in
	-x|--extract) VersionDump "$@"; SuperExtract "$@"; Timer "Stop" "${APPNAME}"; exit 0;;
	-h|--help|-\?) Help "$@"; exit 0;; # TODO: Enhance this section.
	-v|--version) VersionDump "$@"; exit 0;;
	*) Help; exit 0;;
esac


