#!/bin/bash

first_char_of_uncanon_folpath=$(echo "${Full_Uncanon_Unreal_Folpath____This_Script:0:1}")


if [ "$first_char_of_uncanon_folpath" != "/" ]
then
	readonly Full_Canon_Unreal_Folpath____This_Script=$(readlink -m "$Original_Working_Dir/$Full_Uncanon_Unreal_Folpath____This_Script")
else
	readonly Full_Canon_Unreal_Folpath____This_Script=$(readlink -m "$Full_Uncanon_Unreal_Folpath____This_Script")
fi



if [ "$Full_Canon_Unreal_Folpath____This_Script" != "$Follpath____This_Script" ]
then
	readonly Follpath____Config=$(readlink -m "$Full_Canon_Unreal_Folpath____This_Script/../0000-Config")
else
	readonly Follpath____Config=$(readlink -m "$Follpath____This_Script/../0000-Config")
fi



if [ ! -d "$Follpath____Config" ]
then
	echo "Error: No Ramize-Physave config folder was found at:

	$Follpath____Config

Aborting."

	echo
	echo
	if [ "$Follpath____This_Script" != "$Full_Canon_Unreal_Folpath____This_Script" ]
	then
		echo "By the way, it appears $Script_Name might have been launched via a symlink, because

Follpath____This_Script:
	$Follpath____This_Script

...differs from...

Full_Uncanon_Unreal_Folpath____This_Script:
	$Full_Uncanon_Unreal_Folpath____This_Script
	
and

Full_Canon_Unreal_Folpath____This_Script:
	$Full_Canon_Unreal_Folpath____This_Script

To make a shortcut to $Script_Name, it's best to use a shell script rather than a symlink, because the location from which $Script_Name is launched from actually makes a difference in what it does.

That's to make it so you only need one installed copy of Ramize-Physave, instead of many duplicate full copies of Ramize-Physave inside every repo you want to use Ramize-Physave in."
		exit 10
	else
		echo "Since it looks like you might not have launched $Script_Name via a symlink, it's possible your Ramize-Physave installation is missing its 0000-Config folder."
		exit 11
	fi
	
	exit 12
	
	
fi



readonly Follpath____Ramize_Physave=$(dirname "$Follpath____Config")
readonly Foldername____Ramize_Physave="${Follpath____Ramize_Physave##*/}"


if [ -z "$Follpath____Helpers" ]
then
	echo "Error: Follpath____Helpers must be set to something!

Aborting."
	exit 7
fi


readonly Fillepath____is_this_fiol_in_the_puppy_linux_ramdisk="$Follpath____Helpers/is-this-fiol-in-the-puppy-linux-ramdisk"


if [ ! -f "$Fillepath____is_this_fiol_in_the_puppy_linux_ramdisk" ]
then
	echo "Error: Aborting, because this script requires $Fillepath____is_this_fiol_in_the_puppy_linux_ramdisk, and it was not found!"
	
	exit 8
fi
	
	
should_maybe_update_physave_path=false


if [ "$Script_Name" = "Physave" ]
then
	should_get_datetime_of_last_physave=true
fi


if [ "$Script_Name" = "Ramize" ]
then
	
	should_get_datetime_of_last_ramize=true


	cd "$Follpath____Helpers"
	
	result=$(bash "$Fillepath____is_this_fiol_in_the_puppy_linux_ramdisk" "$Full_Canon_Unreal_Folpath____This_Script" 2>&1 )
	
	retcode="$?"
	
	
	if [ "$retcode" != 0 ]
	then
		# 02:41:16 05/04/2017.  If the Ramize script is NOT in the RAM disk,
		# we possibly should update the physave path.
		
		should_maybe_update_physave_path=true
		
	fi
fi

readonly Fillepath____Ramize_Path="$Follpath____Config/Ramize Path"

readonly Fillepath____Ramize_Rsync="$Follpath____Config/Ramize Rsync"
readonly Fillepath____Physave_Rsync="$Follpath____Config/Physave Rsync"





############
# Function
Retrieve_Config()
{
	local what_to_retrieve="$1"
	local filepath_to_retrieve_from="$2"
	
	
	##############################
		
	retrieved_text=$(cat "$filepath_to_retrieve_from")
	retcode="$?"
}
readonly -f Retrieve_Config

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



############
# Function
Abort_if_Retrieve_Failed()
{
	local retcode="$1"
	local what_to_retrieve="$2"
	local filepath_to_retrieve_from="$3"
	
	
	##############################
		
	if [ "$retcode" != 0 ]
	then 
		echo "$Script_Name error: Somehow couldn't retrieve $what_to_retrieve from config file:

$filepath_to_retrieve_from

Aborting."


		#!!!!
		exit 1
	fi
}
readonly -f Abort_if_Retrieve_Failed

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



############
# Function
Retrieve_Config_and_Abort_if_Failed()
{
	local what_to_retrieve="$1"
	local filepath_to_retrieve_from="$2"
	
	
	##############################
		
	Retrieve_Config "$what_to_retrieve" "$filepath_to_retrieve_from"
	Abort_if_Retrieve_Failed "$retcode" "$what_to_retrieve" "$filepath_to_retrieve_from"
}
readonly -f Retrieve_Config_and_Abort_if_Failed

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


Retrieve_Config_and_Abort_if_Failed "Ramize Path" "$Fillepath____Ramize_Path"

readonly Retrieved____Ramize_Path="$retrieved_text"


readonly Actual_Physave_Folder=$(realpath "$Full_Canon_Unreal_Folpath____This_Script/../..")



############
# Function
Standardize_Path_with_readlink()
{
	local path="$1"
	
	
	##############################

	retval=$(readlink -m "$1" 2>&1)

	retcode="$?"

}
readonly -f Standardize_Path_with_readlink

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



############
# Function
Run_Realpath_on()
{
	local path="$1"
	
	
	##############################

	retval=$(realpath "$1" 2>&1)

	retcode="$?"
}
readonly -f Run_Realpath_on

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



############
# Function
Standardize_Path_and_Abort_if_Failed()
{
	local desc_of_path_to_standardize="$1"
	local path_to_standardize="$2"


	##############################
	
	
	Standardize_Path_with_readlink "$path_to_standardize"
	
	if [ "$retcode" != 0 ]
	then
		echo "$Script_Name error: Couldn't standardize $desc_of_path_to_standardize, which was:

	$path_to_standardize

Result:

	$retval

Aborting."


		#!!!!
		exit 2
	fi
	
	standardized_path="$retval"
	retcode=0
}
readonly -f Standardize_Path_and_Abort_if_Failed

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



Standardize_Path_and_Abort_if_Failed "Ramize Path" "$Retrieved____Ramize_Path"

readonly Standardized____Ramize_Path="$standardized_path"



readonly Ramized_Follpath____Config="$Standardized____Ramize_Path/$Foldername____Ramize_Physave/0000-Config"

if [ ! -e "$Ramized_Follpath____Config" ]
then
	mkdir --parents "$Ramized_Follpath____Config"
fi

readonly Ramized_Fillepath____Physave_Path="$Ramized_Follpath____Config/Physave Path"


if [ "$should_get_datetime_of_last_physave" = true ]
then
	readonly Ramized_Fillepath____Datetime_of_Last_Physave="$Ramized_Follpath____Config/Datetime of Last Physave"

	if [ -e "$Ramized_Fillepath____Datetime_of_Last_Physave" ]
	then
		Retrieve_Config_and_Abort_if_Failed "Datetime of Last Physave" "$Ramized_Fillepath____Datetime_of_Last_Physave"
		
		Retrieved____Datetime_of_Last_Physave="$retrieved_text"
	fi
fi


if [ "$should_get_datetime_of_last_ramize" = true ]
then
	readonly Ramized_Fillepath____Datetime_of_Last_Ramize="$Ramized_Follpath____Config/Datetime of Last Ramize"
	if [ -e "$Ramized_Fillepath____Datetime_of_Last_Ramize" ]
	then
		Retrieve_Config_and_Abort_if_Failed "Datetime of Last Ramize" "$Ramized_Fillepath____Datetime_of_Last_Ramize"
		
		Retrieved____Datetime_of_Last_Ramize="$retrieved_text"
	fi
fi


if [ -e "$Ramized_Fillepath____Physave_Path" ]
then
	Retrieve_Config_and_Abort_if_Failed "Physave Path" "$Ramized_Fillepath____Physave_Path"

	Retrieved____Physave_Path="$retrieved_text"
else
	should_definitely_update_physave_path=true
fi


if [ "$should_maybe_update_physave_path" = true ]
then
if [ "$Actual_Physave_Folder" != "$Retrieved____Physave_Path" ]
	then
		should_definitely_update_physave_path=true
		
		Retrieved____Physave_Path="$Actual_Physave_Folder"
	fi		
fi


Standardize_Path_and_Abort_if_Failed "Physave Path" "$Retrieved____Physave_Path"

readonly Standardized____Physave_Path="$standardized_path"
		

cd "$Follpath____Helpers"

if [ ! -d "$Standardized____Physave_Path" ]
then
	echo "$Script_Name error: No folder exists at the intended physave path:

	$Standardized____Physave_Path
	
Must abort."
	
	
	#!!!!
	exit 15
	
fi


result=$(bash "$Fillepath____is_this_fiol_in_the_puppy_linux_ramdisk" "$Standardized____Physave_Path" 2>&1 )

result_code="$?"


if [ "$result_code" = 0 ]
then
	echo "
*******************************************************************************
WARNING: The physave path appears to be located in the Puppy Linux RAM disk!

	$Standardized____Physave_Path

That might mean the disk you wish to physave to unexpectedly got dismounted
at some point.

Aborting.
*******************************************************************************
"
	exit 14

fi



############
# Function
If_Necessary__Update_Physave_Path()
{

	if [ "$should_definitely_update_physave_path" = true ]
	then
		echo "Actual physave path differs from the physave path in the config file!  Updating config to reflect the change."
		echo
	
		# 11:06:44 05/16/2017.  Decided I'd rather have it just update the ramized Config rather than the physaved Config.
		#echo -n "$Standardized____Physave_Path" > "$Full_Filepath____Physave_Path"
	
		echo -n "$Standardized____Physave_Path" > "$Ramized_Fillepath____Physave_Path"
	
	fi

	
}
readonly -f If_Necessary__Update_Physave_Path

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


If_Necessary__Update_Physave_Path



############
# Function
Count_Things_in_String()
{
	local thing="$1"
	local string="$2"


	##############################
	
	retval=$(echo "$string" | awk -F"$thing" '{print NF-1}')
	

	if [ "$retval" == "-1" ]
	then
		retval=0
	fi
}
readonly -f Count_Things_in_String

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



############
# Function
Count_Newlines_in_String()
{
	local string="$1"


	##############################
	
	retval=$(echo -n "$string" | wc -l)
	
}
readonly -f Count_Newlines_in_String

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



############
# Function
Abort_if_Path_Contains_Any_Newlines()
{
	local desc_of_path_to_check="$1"
	local path_to_check="$2"
	

	##############################

	
	Count_Newlines_in_String "$path_to_check"

	
	if [ "$retval" -gt 0 ]
	then
		echo "$Script_Name error: $desc_of_path_to_check must not contain any newlines!

But it contained $retval newline:

	$path_to_check

Aborting."
		
		
		#!!!!
		exit 3
	fi
}
readonly -f Count_Newlines_in_String

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



Abort_if_Path_Contains_Any_Newlines "Physave Path" "$Standardized____Physave_Path"
Abort_if_Path_Contains_Any_Newlines "Ramize Path" "$Standardized____Ramize_Path"



############
# Function
Abort_if_Path_Contains_Less_Than_x_Slashes()
{
	local desc_of_path_to_check="$1"
	local path_to_check="$2"
	local minimum_num_of_slashes="$3"


	##############################
	
	Count_Things_in_String "/" "$path_to_check"
	
	
	if [ "$retval" -lt "$minimum_num_of_slashes" ]
	then
		echo "$Script_Name error: $desc_of_path_to_check must contain at least $minimum_num_of_slashes slashes!

But it contained $retval slashes:

	$path_to_check

Aborting."
		
		
		#!!!!
		exit 4
	fi
	
}
readonly -f Abort_if_Path_Contains_Less_Than_x_Slashes

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


Abort_if_Path_Contains_Less_Than_x_Slashes "Physave Path" "$Standardized____Physave_Path" 3

Abort_if_Path_Contains_Less_Than_x_Slashes "Ramize Path" "$Standardized____Ramize_Path" 2



############
# Function
Abort_if_Path_Does_Not_Exist()
{
	local desc_of_path_to_check="$1"
	local path_to_check="$2"


	##############################
	
	if [ ! -e "$path_to_check" ]
	then
		echo "$Script_Name error: $desc_of_path_to_check must exist, but nothing was found there:

	$path_to_check

Aborting."
		
		
		#!!!!
		exit 5
	fi
	
}
readonly -f Abort_if_Path_Does_Not_Exist

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


if [ "$Script_Name" = "Ramize" ]
then
	Abort_if_Path_Does_Not_Exist "Physave Path" "$Standardized____Physave_Path"
fi




############
# Function
Update_Datetime_of_Last_Ramize_to_Now()
{


	##############################
	
	if [ "$should_get_datetime_of_last_ramize" = true ]
	then
		now_datetime=$(date +"%c")

		echo -n "$now_datetime" > "$Ramized_Fillepath____Datetime_of_Last_Ramize"
		
		result_code="$?"
		return "$result_code"
	fi
}
readonly -f Update_Datetime_of_Last_Ramize_to_Now

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



############
# Function
Update_Datetime_of_Last_Physave_to_Now()
{


	##############################
	
	if [ "$should_get_datetime_of_last_physave" = true ]
	then
		now_datetime=$(date +"%c")

		echo -n "$now_datetime" > "$Ramized_Fillepath____Datetime_of_Last_Physave"
		
		result_code="$?"
		return "$result_code"
	fi
}
readonly -f Update_Datetime_of_Last_Physave_to_Now

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