#!/bin/bash

readonly Script_Name="Ramize Any Physaved Folder"

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

# 05:54:15 06/21/2017.
#
#	Fillepath	=	Full Filepath
#	Follpath	=	Full Folderpath


readonly Follpath____Minimal_Ramize_Physave="/root/apbin/Minimal Ramize-Physave/In Use"

if [ ! -d "$Follpath____Minimal_Ramize_Physave" ]
then
	echo "Error: Couldn't find the copy of Minimal Ramize-Physave intended for installation into the ramized folder!

It should have been here:

	$Follpath____Minimal_Ramize_Physave"
	
	exit 7
	
fi



if [ -z "$1" ]
then
	echo "Error: Arg #1 was empty, but it must be a physaved folderpath you want to ramize!"
	exit 1
fi

if [ -z "$2" ]
then
	echo "Error: Arg #2 was empty, but it must be the destination folder in the RAM disk where you want to ramize the physaved folderpath in Arg #1!"
	
	exit 2
fi


possible_folder_to_ramize="$1"
possible_dest_folder="$2"

if [ ! -d "$possible_folder_to_ramize" ]
then
	echo "Error: Arg #1 (the physaved folderpath you want to ramize) does not lead to a folder!
		
	$possible_folder_to_ramize"
	
	exit 3
fi



if [ ! -d "$possible_dest_folder" ]
then
	echo "Error: Arg #2 (the destination folder in the RAM disk) does not lead to a folder!
	
	$possible_dest_folder"
	exit 4
fi



readonly Original_Working_Dir=$(pwd)

readonly Follpath____Helpers=$(readlink -m "$Follpath____This_Script/../Helpers")

cd "$Follpath____Helpers"


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


result=$(bash "$Fillepath____is_this_fiol_in_the_puppy_linux_ramdisk" "$possible_dest_folder" 2>&1 )
result_code="$?"


if [ "$result_code" != 0 ]
then
	echo "Error: Aborting, because the destination folder in Arg #2 isn't actually in the Puppy Linux RAM disk!
	
	$possible_dest_folder"
	
	exit 6
fi



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

result_code="$?"



if [ "$result_code" = 0 ]
then
	echo "Error: Aborting, because the folder to minimal-ramize in Arg #1 was found in the Puppy Linux RAM disk!
	
	$possible_folder_to_ramize"
	
	exit 6
fi


folder_to_ramize=$(readlink -m "$possible_folder_to_ramize" )
dest_folder=$(readlink -m "$possible_dest_folder")


cd "$Original_Working_Dir"


Follpath____Dest_Unmirrored_Minimal_Ramize_Physave="$dest_folder/0000-Unmirrored Minimal Ramize-Physave"
Follpath____Dest_Ramize_Physave="$dest_folder/Ramize-Physave"
Follpath____Dest_Minimal_Ramize_Physave="$dest_folder/Minimal Ramize-Physave"


if [ -e "$Follpath____Dest_Unmirrored_Minimal_Ramize_Physave" ] || [ -e "$Follpath____Dest_Minimal_Ramize_Physave" ] || [ -e "$Follpath____Dest_Ramize_Physave" ]
then
	echo "Error: It seems something was already ramized to this folder:

	$dest_folder

Aborting."

	exit 8
fi

Follpath____Dest_for_Whatever_Ramize_Physave="$Follpath____Dest_Unmirrored_Minimal_Ramize_Physave"


rsync --archive --verbose --update --recursive --exclude ".hg*" "$Follpath____Minimal_Ramize_Physave/" "$Follpath____Dest_for_Whatever_Ramize_Physave"

Follpath____Dest_Config="$Follpath____Dest_for_Whatever_Ramize_Physave/0000-Config"

Fillepath____Dest_Config____Ramize_Path="$Follpath____Dest_Config/Ramize Path"
Fillepath____Dest_Config____Physave_Path="$Follpath____Dest_Config/Physave Path"

echo -n "$dest_folder" > "$Fillepath____Dest_Config____Ramize_Path"
echo -n "$folder_to_ramize" > "$Fillepath____Dest_Config____Physave_Path"


Fillepath____Dest_Script____Ramize="$Follpath____Dest_for_Whatever_Ramize_Physave/Ramize/Ramize"

bash "$Fillepath____Dest_Script____Ramize"