#!/bin/sh

# Script:              Is This Fiol in the Puppy Linux RAM Disk?
#
#                       is-this-fiol-in-the-puppy-linux-ramdisk
#
# 
# by Apollia - http://apollia.com/ 
#
# License: GNU Affero GPL 3.0
# http://www.gnu.org/licenses/
#
#
# Version 1 completed Jan. 17, 2017 at 5:38 PM EST.
#
# Version 2 completed June 21, 2017 at 4:18 AM EDT.  No longer mistakenly
# assumes mountpoint folders are in the Puppy Linux RAM disk.
#
#
# The word "fiol" refers to something that is either a file or a folder.
#
# The purpose of this script is to quickly tell you whether or not a fiol
# you're inquiring about resides somewhere inside the Puppy Linux RAM disk.
# (The fiol may also be a symlink, and/or its fiolpath may contain one or 
# more symlinks.)
#
#
# Works in Lucid Puppy Linux 5.2.8 version 004:
# http://www.murga-linux.com/puppy/viewtopic.php?t=70855
#
# And Lighthouse 64 Puppy Linux 6.02 Beta 2:
# http://lhpup.org/
#
# Haven't yet tried it in any other Puppy Linux.
#
#
# --------
# Argument
# --------
#
# Accepts only one argument - a fiolpath (filepath or folderpath) or fiolname
# (filename or foldername).
#
#
# -------
# Results
# -------
#
# If the fiol is found in the Puppy Linux RAM disk:
#
#    The script returns exit code 0, and prints to STDOUT either of the
#    following, depending on where the fiol was found:
#
#            /initrd/pup_rw/
#            /initrd/pup_ro2/
#
#
# If Argument 1 is empty:
# If the "realpath" command doesn't work on the fiolpath:
# If the fiol is not found in the Puppy Linux RAM disk:
#
#     The script returns an exit code other than 0, and prints to STDERR
#     whatever went wrong.
#
#
# ------------------
# Exit Code Meanings
# ------------------
#
# 0 - The fiol was found in the Puppy Linux RAM disk.
#
# 1 - Argument 1 was empty.
#
# 2 - The "realpath" command somehow didn't work on the fiolpath (which might
# mean no fiol exists at that fiolpath).
#
# 3 - The fiol definitely exists, but wasn't found in the Puppy Linux RAM disk.
#
# 4 - The fiol is a mountpoint folder, so isn't in the Puppy Linux RAM disk.
#
#
# -------------------------------------
# More details on how this script works
# -------------------------------------
#
# The precise location Argument 1 refers to will (hopefully) be discovered by 
# running "realpath" on Argument 1, which should result in a canonicalized,
# symlinks-dereferenced, absolute fiolpathstring (known below as
# REAL_FIOLPATHSTRING or REAL_FIOLPATH).
#
# Then, two more new fiolpathstrings are made by prefixing the
# REAL_FIOLPATHSTRING with these folderpaths:
#
#      /initrd/pup_ro2/
#           Contains the original, unmodified versions of the
#           files in the Puppy Linux RAM disk.
#
#      /initrd/pup_rw/
#           Contains any new additions to (or modified version of) the
#           files in the Puppy Linux RAM disk.
#
# If something exists at either /initrd/pup_ro2/[REAL_FIOLPATH] or
# /initrd/pup_rw/[REAL_FIOLPATH], and that fiolpath isn't found to be a
# mountpoint folder, the answer to the question
#
# "Is This Fiol in the Puppy Linux RAM Disk?"
#
# ...is assumed to be yes.  So, this script returns exit code 0, and announces
# via STDOUT which of the above folders the fiol was found in first.
# (pup_rw is checked first, then if nothing's there, pup_ro2 is checked).
#
#
# If nothing is found in either pup_ro2 or pup_rw, the answer is assumed to be
# no, so the script returns exit code 3 and announces the results via STDERR.
#
# If the fiol is a mountpoint folder, the script returns exit code 4
# announces the results via STDERR.
#
#------------------------------------------------------------------------------
#
#
# Under the GNU Affero General Public License v3.0.
#
# Copyright (C) 2017  Apollia
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Contact info:     http://astroblahhh.com/contact-apollia.shtml
#
#-----


if [ -z "$1" ]
then
	# 14:08 01/17/2017.  Argument 1 is empty.
	echo
	echo "$0: Aborted, because Argument 1 was not provided.

Please provide a fiolpath (a filepath or folderpath), or a fiolname (a filename or foldername)." 1>&2
		# 07:15 01/17/2017.  Echoed to STDERR.
	exit 1
fi


real_fiolpath=$(realpath "$1")

exit_code="$?"

if [ "$exit_code" != 0 ]
then 
	echo
	echo "$0: Aborted, because realpath command somehow didn't work on Argument 1: $1" 1>&2
		# 14:01 01/17/2017.  Echoed to STDERR.
	exit 2
fi



# 04:12:29 06/21/2017.  Function from:
#
# https://stackoverflow.com/questions/11856054/bash-easy-way-to-pass-a-raw-string-to-grep

ere_quote() {
    sed 's/[]\.|$(){}?+*^]/\\&/g' <<< "$*"
}



grepescaped_real_fiolpath=$(ere_quote "$real_fiolpath")

result_of_check_for_mountpoint=$(df -h "$real_fiolpath" | grep -E "$grepescaped_real_fiolpath\$" | wc -l )

if [ "$result_of_check_for_mountpoint" != 0 ]
then
	echo "Mountpoint - not in Puppy Linux RAM disk"
	
	exit 4
fi



Folder____Original_RAM_Disk_Files="/initrd/pup_ro2/"
Folder____Modified_RAM_Disk_Files="/initrd/pup_rw/"

real_fiolpath_in_Original_RAM_Disk_Files="$Folder____Original_RAM_Disk_Files$real_fiolpath"
real_fiolpath_in_Modified_RAM_Disk_Files="$Folder____Modified_RAM_Disk_Files$real_fiolpath"


if [ -e "$real_fiolpath_in_Modified_RAM_Disk_Files" ]
then
	# 14:13 01/17/2017.  Then the path is a later addition to (or modified version of)
	# stuff in the Puppy Linux RAM disk.
	echo "$Folder____Modified_RAM_Disk_Files"
	
	exit 0
fi

if [ -e "$real_fiolpath_in_Original_RAM_Disk_Files"  ]
then
	# 18:39 01/17/2017.  Then the path is one of the original, unmodified
	# items in the Puppy Linux RAM disk.
	echo "$real_fiolpath_in_Original_RAM_Disk_Files"
	exit 0
fi


echo
echo "Could not find in Puppy Linux RAM disk:

$1

...whose canonical, symlinks-dereferenced location is:

$real_fiolpath" 1>&2

exit 3

# 14:46 01/17/2017.  End of script.