#!/bin/bash

#   Script:      apsk-note-platz
#
#
# by Apollia - http://apollia.com/
#
#
# 03:59:59 10/04/2021.  Before this update, last modified: 2018-05-29 05:15:08 -0400


readonly Command_Name="apsk-note-platz"
readonly Short_Command_Name="noteplatz"


if [ -z "$1" ]
then
	echo "$Command_Name: Aborting because Arg #1 must be the platz you wish to assign a path to, and Arg #1 was empty."
	
	
	#!!!!
	exit 1
fi


if [ -z "$2" ]
then
	echo "$Command_Name: Aborting because Arg #2 must be the path you wish to assign to platz \"$1\", and Arg #2 was empty."
	
	
	#!!!!
	exit 2
fi



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"


slash='/'
backslash='\' 
#'



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 3
fi


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



############
# Function
Make_Folder_or_Abort()
{
	local follpath="$1"
	local exit_code="$2"
	
	[ -z "$exit_code" ] && exit_code=99
	
	if [ ! -d "$follpath" ]
	then

		mkdir --parents "$follpath"	
		result_code="$?"
		
		
		if [ "$result_code" != 0 ]
		then
			echo
			echo
			echo "$Command_Name error: Aborting, because somehow it wasn't possible to create this folder:
		
	$follpath"
			
			
			#!!!!
			exit "$exit_code"
		
		fi
	fi
}
readonly -f Make_Folder_or_Abort

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


Make_Folder_or_Abort "$Follpath____Platz_Path_Notes" 4
Make_Folder_or_Abort "$Follpath____Path_Platz_Notes" 5




############
# 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:,path: --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
				;;
			
			--path)
				Arg____Path="$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____Path="$ARG2"
	[ ! -z "$ARG3" ] && Arg____Platz_Namespace="$ARG3"

}
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 and " - " will be prepended to Arg #1.


# 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.


#readonly Platz="$1"
# July 28, 2017, 1:30 PM.  Should valicheck to make sure
# it doesn't contain any path badchars or commas.

#readonly Platz_Path="$2"
# July 28, 2017, 1:46 PM.  Should valicheck to make sure it has no path badchars or commas.


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


readonly Platz_Path="$Arg____Path"



readonly Fillepath____Platz_Path_Note="$Follpath____Platz_Path_Notes/$Platz"

echo -n "$Platz_Path" > "$Fillepath____Platz_Path_Note"



readonly Platz_Path_with_Backslashes=${Platz_Path//$slash/$backslash}

readonly Follpath____Path_Folder_Containing_Platzes="$Follpath____Path_Platz_Notes/$Platz_Path_with_Backslashes"

readonly Fillepath____Platz_Note_for_Path_Folder="$Follpath____Path_Folder_Containing_Platzes/$Platz"

Make_Folder_or_Abort "$Follpath____Path_Folder_Containing_Platzes" 6

touch "$Fillepath____Platz_Note_for_Path_Folder"

echo
echo
echo
echo "------------------------------"
echo "$Short_Command_Name: Noted a platz:

	$Platz

...whose path is:

	$Platz_Path"
	
	

#echo -n "$Platz" > "$Fillepath____Path_Platz_Note"

#echo "Platz_Path_with_Backslashes: $Platz_Path_with_Backslashes"




# July 28, 2017, 9:27 PM.  OK, to deal with actual backslashes in paths... hmm, convert them to
# two in a row?  Then when converted back to slashes, we can look for two slashes in a row, and
# convert those to backslashes.

# 9:41 PM.  OK, we can also have multi-platzes, separately from the platzes, though.
# Multi-platzes will be associated with multiple paths... but how to order them?...
# oh, i guess i could just name them 1, 2, 3, etc... but symlink them to the platzpath text files...
# and then the path to platz version of multiplatz... well, it'll just contain the names of multi-platzes that refer to those paths.
#
# 9:44 PM.  oh, and i guess multiplatzes can refer to other multiplatzes... but, better make
# sure we avoid loops somehow.  9:45 PM.  i guess it could also be possible to refer to a particular
# number of a multiplatz, like the 2nd path... 1, 2, etc...
#
# 9:46 PM.  And preplatz is the term for unresoplatzes... platzes are already resolved... platzes
# can also have associated preplatzes so you can recalculate them later if you want... so, yeah,
# my work with json, etc. hopefully will go to good use at last!  
#
# 10:58 PM.  Any platzes which have preplatzes, there ought to be a way to easily find out what components were in their preplatzes, so if any component gets updated, all the associated platzes can be updated as well... so, some sort of notes folder somewhere... with folders containing files naming the components used.