#!/bin/bash

readonly Script_Title="Detect Puppy Distro"
readonly Short_Command_Name="apsk-detect-puppy-distro"


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

readonly Fillepath____Running_Puppy_s_Distro_Specs="/etc/DISTRO_SPECS"


readonly Follpath____Notes="/tmp/APSK/Detect Puppy Distro/Notes"
	readonly Note_Fillepath____Detected_Puppy_Distro="$Follpath____Notes/Detected Puppy Distro"



############
# Function
Valicheck_Distro_Abbrev()
{
	local distro_abbrev="$1"
	
	case "$distro_abbrev" in
		
		"LH64_602_B2")
		
		
			#!!!!
			return 0
			;;
			

		"Unknown")
		
		
			#!!!!
			return 1
			;;
			

		*)
		
			#!!!!
			return 2
			;;
			
	esac
}
readonly -f Valicheck_Distro_Abbrev

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



############
# Function
Display_Long_Name_of_Distro()
{
	Valicheck_Distro_Abbrev "$detected_distro"
		result_code="$?"
		
	case "$detected_distro" in
		"LH64_602_B2")
			echo "Lighthouse 64 Puppy Linux 6.02 Beta 2"
			
			;;
		"Unknown")
			echo "Unknown"
			
			;;
		*)
			echo "$detected_distro (Unrecognized distro abbreviation)"
			
			;;
	esac
	
	return "$result_code"
	
}
readonly -f Display_Long_Name_of_Distro

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



Display_Short_Name_of_Distro()
{
	Valicheck_Distro_Abbrev "$detected_distro"
		result_code="$?"
	
	
	case "$result_code" in

		0|1)
			# 05:45:37 10/14/2018.  0 is valid, 1 = "Unknown"
			echo "$detected_distro"
			;;

		
		2)
			# 05:45:43 10/14/2018.  Anything else.
			echo "$detected_distro (Unrecognized distro abbreviation)"
			;;

	esac
	
	
	#!!!!
	return "$result_code"
}



############
# Function
Print_Distro_Name()
{
	if [ ! -z "$1" ]
	then
		Display_Long_Name_of_Distro
			return "$?"
	else
		Display_Short_Name_of_Distro
			return "$?"
		
	fi
}
readonly -f Print_Distro_Name

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



if [ -f "$Note_Fillepath____Detected_Puppy_Distro" ]
then
	detected_distro=$(cat "$Note_Fillepath____Detected_Puppy_Distro")
	
	Print_Distro_Name "$@"
	
	
	#!!!!
	exit "$?"
	
fi


#------------------------------------------------------------------------------
#
# 05:18:14 10/14/2018.  The rest of this script only gets executed
# if the distro note file hasn't been created yet.
#


if [ ! -d "$Follpath____Distro_Specs" ]
then
	echo "$Short_Command_Name error: Aborting - missing Distro Specs folder!"
	
	
	#!!!!
	exit 1
fi


if [ ! -f "$Fillepath____Running_Puppy_s_Distro_Specs" ]
then
	echo "$Short_Command_Name error: Aborting because a needed file was missing - the running Puppy Linux's distro specs file:

	$Fillepath____Running_Puppy_s_Distro_Specs"
	
	
	#!!!!
	exit 2
fi


readonly EOL=$'\n'

readonly File_Size____Running_Distro_Specs=$(stat -c "%s" "$Fillepath____Running_Puppy_s_Distro_Specs")

Stringlist____Distro_Specs_Files=$(find "$Follpath____Distro_Specs" -type f -not -name "*$EOL*" -size "$File_Size____Running_Distro_Specs"c -printf "%P")



############
# Function
Convert_Newline_Separated_String_to_Array()
{
	string="$1"

	# 2016-03-15 04:45:41.  addlater.  should trim final newline before processing.
	
#	echo "Orig string: \"$string\""
	string="${string%$'\n'}"

	#string="${string%\$'\\n'}"

#	echo "Trimmed string: \"$string\""	
	
	
	retcount=0
	retval=()
	
	local array=()
	
	if [ -z "$string" ]
	then
		retcount=0
		return 0
	fi
	
	while read -r line; do
		#echo "Line: '$line' "
		array+=("$line")
	done <<< "$string"
	
	retval=( "${array[@]}" )
	# 2016-01-29 04:58:07.  Yep, it's that tough in Bash just to set a variable equal to an array.
	
	
	retcount=${#retval[@]}		
#	arraystring=$(printf '%s\n' "${array[@]}")
#	echo "array: $array"
#	echo "arraystring: $arraystring"
}
readonly -f Convert_Newline_Separated_String_to_Array

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



Convert_Newline_Separated_String_to_Array "$Stringlist____Distro_Specs_Files"


readonly list_of_files=( "${retval[@]}" )


cd "$Follpath____Distro_Specs"

for file in "${list_of_files[@]}"
do
	diff "$file" "$Fillepath____Running_Puppy_s_Distro_Specs"
	diff_result_code="$?"
	
	if [ "$diff_result_code" = 0 ]
	then
		detected_distro="$file"


		#!!!!
		break
	fi
done


if [ -z "$detected_distro" ]
then
	detected_distro="Unknown"
fi



mkdir --parents "$Follpath____Notes"

echo -n "$detected_distro" > "$Note_Fillepath____Detected_Puppy_Distro"


Print_Distro_Name "$@"