#!/bin/bash

#   Script:      apsk-get-platz
#
#
# by Apollia - http://apollia.com/
#
#
# 03:59:35 10/04/2021.  Before this update, last modified: 2018-05-25 01:47:11 -0400



readonly Command_Name="apsk-get-platz"
readonly Short_Command_Name="platz"

if [ -z "$1" ]
then
	echo "$Command_Name: Arg #1 must be the name (and optionally namespace) of a platz you want to retrieve the path of."
	
	
	#!!!!
	exit 1
fi

# July 28, 2017, 1:30 PM.  Should valicheck the platz to make sure
# it doesn't contain any path badchars or commas.



readonly Fillepath____This_Script=$(realpath "${BASH_SOURCE[0]}")
	readonly Follpath____This_Script=$(dirname "$Fillepath____This_Script")

		readonly Follpath____Config="$Follpath____This_Script/0000-Config"


			readonly Fillepath____Folderpath_of_Platz_Trails="$Follpath____Config/Folderpath of Platz Trails"
				Follpath____Platz_Trails=$(cat "$Fillepath____Folderpath_of_Platz_Trails")
					result_code="$?"


if [ "$result_code" != 0 ]
then
	echo
	echo
	echo "$Command_Name error: Aborting, because it wasn't possible to retrieve the config file containing the folderpath of this app's notes:

	$Fillepath____Folderpath_of_Platz_Trails"
	
	
	#!!!!
	exit 2
fi


if [ ! -d "$Follpath____Platz_Trails" ]
then
	echo "$Command_Name: No platzes have been noted so far!"
	
	
	#!!!!
	exit 3
fi



############
# Function
Parse_Switch_Args()
{
	
#-------------------------------------------------------------------------------
#
# Slightly modified getopt code from:
#
#http://stackoverflow.com/questions/192249/how-do-i-parse-command-line-arguments-in-bash/29754866#29754866
#
# Thanks to the authors, especially for the tip about using eval to properly handle quoting!

	count_of_args_before_parsing_switches="$#"
	
	#echo "count_of_args_before_parsing_switches: $count_of_args_before_parsing_switches"
	

	PARSED_SWITCHES=$(getopt --alternative --options b:,h,n: --longoptions basename:,help,namespace: --name "$Short_Command_Name" -- "$@" )

	if [ $? != 0 ]
	then
		# then getopt has complained about wrong arguments to stdout
		echo
		Help_Message
		
		
		#!!!!
		exit 3
	fi


	eval set -- "$PARSED_SWITCHES"
	# Use eval with "$PARSED_SWITCHES" to properly handle the quoting.
	
	Arg____Platz_Basename_or_Complete_Platz=""
	Arg____Platz_Namespace=""
	

	for arg
	do
		#echo "Arg: $arg"
		case "$arg" in
			
			-b|--basename)
				Arg____Platz_Basename_or_Complete_Platz="$2"
				shift
				shift
				;;
			-n|--namespace)
				Arg____Platz_Namespace="$2"
				shift
				shift
				;;
				
			-h|--help)
				Help_Message
				
				
				#!!!!
				exit 0
				;;
			
			--)
			#	echo "Dashes!"
				shift
				;;
		esac
	done

	Positional_Args=( "$@" )
	count_of_positional_args="$#"
	
	count_of_args_after_parsing_switches="$#"
	
	#echo "count_of_args_after_parsing_switches: $count_of_args_after_parsing_switches"
	#echo "count_of_positional_args: $count_of_positional_args"
	
}
readonly -f Parse_Switch_Args

# End of
# Function
############



############
# Function
Parse_Positional_Args()
{
	# 14:13:20 04/27/2018.  If any of these conflict with switches, these win.

	ARG1=${@:$OPTIND:1}
	ARG2=${@:$OPTIND+1:1}
	#~ ARG3=${@:$OPTIND+2:1}
	#~ ARG4=${@:$OPTIND+3:1}
	#~ ARG5=${@:$OPTIND+4:1}
	
	# 11:30 07/24/2016.  The below are basically abbreviated if/then statements.
	#
	# If ARG1 is not empty, then Arg____File gets set to $ARG1.  Etc.

	[ ! -z "$ARG1" ] && Arg____Platz_Basename_or_Complete_Platz="$ARG1"
	[ ! -z "$ARG2" ] && Arg____Platz_Namespace="$ARG2"

}
readonly -f Parse_Positional_Args

# End of
# Function
############


declare -a Positional_Args

Parse_Switch_Args "$@"

#printf 'pos arg %s\n' "${Positional_Args[@]}"

Parse_Positional_Args "${Positional_Args[@]}"


# 21:17:50 05/24/2018.  Arg #1 can be just the basename of a platz,
# or it can be a complete platz - "Namespace - Basename".
#
# (Though actually, even just a basename alone can qualify as a complete
# platz, because platzes aren't required to have a namespace.)
#
#
# If Arg #2 is provided, then, to assemble the complete platz string,
# Arg #2 will be prepended to Arg #1, followed by " - ".


# 21:28:49 05/24/2018.  For now, since I'd rather keep working on
# APSK Initializer Stage 2, nothing fancy is going to happen with
# namespaces.


if [ ! -z "$Arg____Platz_Namespace" ]
then
	Platz="$Arg____Platz_Namespace - $Arg____Platz_Basename_or_Complete_Platz"
else
	Platz="$Arg____Platz_Basename_or_Complete_Platz"
fi


readonly Follpath____Platz_Path_Notes="$Follpath____Platz_Trails/Platz -> Path Notes"

readonly Fillepath____Platz_Note="$Follpath____Platz_Path_Notes/$Platz"

Platz_Path=$(cat "$Fillepath____Platz_Note"  2>&1 /dev/null )
	result_code="$?"

if [ "$result_code" != 0 ]
then
	echo "Found no path assigned to this platz:

$Platz"


	#!!!!
	exit 3
fi

echo "$Platz_Path"


#!!!!
exit 0