#!/bin/bash
# ==============================================================================
# title			:donors
# description	:Splits csv (Comma Seperated Value)files to manage list of donors automously
# author		:Glenn Cady <TheeMahn@ultimateedition.info>
# date			:05/18/2016
# version		:1.9.7
# usage			:donors --help
# manual		:man donors
# notes			:See change-log below for further information.
# ==============================================================================
# Change-log: 1.0: unreleased
#
#	1.9.3:	Initiated full automous control.
#	1.9.4:	Version Bump
#	1.9.5:	Version Bump
#	1.9.5:	Version Bump
#	1.9.6:	Version Bump
#	1.9.7:	Closed the LTS (Long Term Supported) Gap.
#	2.0.7	Added initial support for "Jammy Jellyfish" / Ultimate Edition 7.4 & 7.5
# ==============================================================================

# Modify the below information to correlate with the software you are designing.
PROGNAME="donors"
PROGRAMMER="TheeMahn"
BUILDDATE="12/02/2021"
VERSION="2.0.7"
WEBSITE="os-builder.com"
EMAIL="<$PROGRAMMER@$WEBSITE>"

source /usr/share/ultimate_edition/ultimate-common

# Declare Arrays for storing data
declare -a DATES
declare -a COUNTRYS
declare -a AMOUNTS

ARRAY=0

# Set default donors file
FILETOPROCESS="donors.csv"

# Begin programming here.
SplitData() {
	VersionDump "$@"
	# Allow for custom file processing.
	if [[ "${2}" != "" ]]; then
		if ! [[ -f "${2}" ]]; then
			echo "File not found ${2}. Exiting."
			exit 1;
		else
			FILETOPROCESS="${2}"
		fi
	else
		if ! [[ -f "${FILETOPROCESS}" ]]; then
			echo "File not found ${FILETOPROCESS}. Exiting."
			exit 1;
		fi
	fi

	# Populate arrays with data.  Date the donor donated.
	FILETOPROCESS="temp.csv"
	readarray -t DATES < <(cut -d'"' -f2 "${FILETOPROCESS}")
	DATES=("${DATES[@]#*,}")
	DATES=("${DATES[@]%%,*}")
	#printf '%s\n' "${DATES[0]}"

	# Populate arrays with data.  Name of the awesome individual who donated.
	readarray -t NAMES < <(cut -d'"' -f8 "${FILETOPROCESS}")
	NAMES=("${NAMES[@]#*,}")
	NAMES=("${NAMES[@]%%,*}")
	#printf '%s\n' "${NAMES[0]}"

	# Populate arrays with data.  Country donated from.
	readarray -t COUNTRYS < <(cut -d'"' -f18 "${FILETOPROCESS}")
	COUNTRYS=("${COUNTRYS[@]#*,}")
	COUNTRYS=("${COUNTRYS[@]%%,*}")
	#printf '%s\n' "${COUNTRYS[0]}"

	# Populate arrays with data.  The amount the donor donated.
	readarray -t AMOUNTS < <(cut -d, -f3 "${FILETOPROCESS}")
	AMOUNTS=("${AMOUNTS[@]#*,}")
	AMOUNTS=("${AMOUNTS[@]%%,*}")
	#printf '%s\n' "${AMOUNTS[0]}"

	# Generate individual columns with it's perspective data of donors.
	# Each person's Name as it cycles through the entire array of $NAMES[@]
	if [[ -f "donors.csv" ]]; then
		rm "donors.csv"
	fi
	FullBar
	Columnize
	FullBar
	for PERSON in "${NAMES[@]}"
	do
		DATE="${DATES[${ARRAY}]}"
		TDATE=$(date --date="$DATE" "+%m/%d/%Y")
		COUNTRY="${COUNTRYS[${ARRAY}]}"
		PERSON=$(echo "${PERSON}" | sed -r 's/\<./\U&/g')
		echo "${TDATE},${PERSON},N/A,\$${COUNTRY}" >> donors.csv
		#Columnize -t "DATE DONOR COUNTRY AMOUNT"

		# Increment the value of the current array to coincide with the
		# value of the next person to process.
		ARRAY=$((ARRAY + 1))
		PERSON=${PERSON// /_}
		#PERSON=$(echo "${PERSON}" | sed "s/ /_/g")
		Columnize "${ARRAY} ${TDATE} ${PERSON} N/A \$${COUNTRY}"
	done
	Center "Processing of ${FILETOPROCESS} complete."
}

# 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..."
		FullBar
		Encapsulate ""
		Encapsulate "  -h       --help          this help message"
		Encapsulate "  -s       --split         Splits into chunks for label processing."
		Encapsulate "  -v       --version       dump version info and exit"
		Encapsulate ""
		FullBar
		Center "$PROGNAME --help [COMMAND] for further information."
		FullBar;;
		ALL|s|split)
		FullBar
		Encapsulate ""
		Encapsulate "Usage split;"
		Center "$PROGNAME -s [FILENAME]"
		Encapsulate "Splits [FILENAME] into chunks for label processing."
		Encapsulate "Not specifying then [OPTIONAL] filename will search for inv.csv"
		FullBar;;
		ALL|v|version)
		echo -e ""
		Encapsulate "Usage version;"
		Center "$PROGNAME -v"
		Encapsulate "Displays $PROGNAME version number and exits."
		FullBar;;
		ALL|h|help|\?)
		FullBar
		Encapsulate ""
		Encapsulate "Useage Help [COMMAND];"
		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 version"
		FullBar
	esac
	exit 0
}

shopt -u nullglob
Timer "Start" "${APPNAME}"

# Command line pre-processor - this section of code is where your functions are called.

    case "$1" in
      -h|--help|-\?) Help "$@"; exit 0;;
      -v|--version) VersionDump; exit 0;;
      -s|--split) SplitData "$@"; exit 0;;
      *) Help; exit 0;;
    esac


