#!/bin/bash
#                       /usr/local/bin/limits
#  http://crystalfaeries.net/posix/bin/limits
# celeste:crystalfaery LIMITS 2021-03-29 09:29:13+00:00
# WITHOUT option limits displays the fullness of /dev/mapper/fae--vg-home
# as a percentage graph 100 characters long.
# WITH -l option we textually display	the limits themselves
# WITH -e option we edit in vi		the limits themselves
# the limits files should each be exactly one line long of only the percentage number, no "%" trailing
# Capital letters = disk is this full
# Lower Case = disk space not yet occupied
#...except we show time+dir in the lower range of "partition fullness",
# expecting it to be of no concern that disk usage below 26% is indeterminate.
# 2020-09-12 17:14:57 /home LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLBBBBBDDDDDSSSSSfffff

#LIMIT_FILE_DESCRIPTOR %FULL	# FUNCTION_BEING_LIMITED, ACCORDING TO LIMIT: UTILIZED_BY_EXAMPLE_SCRIPT
#		96 - 100	f = FULL! the filesystem is not efficient.
# /etc/rsnapshotlimit: 95	# Delete oldest rsnapshots     down to limit: /usr/local/bin/rsnapshotlimit.fey
#		91 - 94		s = Snapshot space for rsnapshot.
# /etc/downloadlimit:  90	# Download from the internet	 up to limit: /usr/local/bin/youtubedownload
#		86 - 89		d = Download Space available for downloads.
# /etc/darlimit:       85	# Delete oldest .dar backups   down to limit: /usr/local/bin/darlimit.fey
#		81 - 84		b = Backup Space for *.dar
# /etc/restorelimit:   80	# Restore audio from media to HD up to limit: /usr/local/bin/sync{sd,usb}
#		00 - 79		L = Library Space we auto-fill from backups/library removable media

let help=26 # (line number of this line) - 1

while [[ $# -gt 0 ]]
do  # have option to handle
   case "${1}" in
--version | -v )
	head -n 4 $0 | tail -n 1 >&2
	exit	0
	;;
-h )
	head -n ${help} ${0} >&2
	echo	"$0 --help <------- for extended help"
	exit	0
	;;
--help )
	echo	"$0 -h <------- for help"
cat	<<EOF

celeste@fey:~$ limits;df --full
2021-03-29 09:28:08 /home LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLBBBBBDDDddsssssfffff <------- 88% full
Filesystem                1024-blocks  Used      Available  Capacity  Mounted_on
/dev/mmcblk0p1            15322472     14523508  912        100%      /media/celeste/035d6072-b07f-42a4-b934-0ef6529e2bd7
/dev/mapper/fae--vg-home  53213544     44324916  6155780    88%       /home <------- displayed by "limits"
/dev/mapper/fae--vg-root  14089680     10879152  2475092    82%       /
/dev/mapper/fae--vg-var   4969984      1593260   3104548    34%       /var
/dev/sda1                 240972       40657     187874     18%       /boot
/dev/mapper/fae--vg-tmp   906840       2736      840824     1%        /tmp
# the Library space is how full we make the harddisk trying to bring onto it
# the Library which is too large to all fit, and is distributed on removables
# and Library space when exceeded causes auto-migration from hd to removables.
# the Backup  space is for backups.dar which auto-run 5AM Sunday Morning...
# once we have run syncusb and syncsd to migrate those .dar's onto removables 
# then that space is available for the week (THESE ARE OUR ESSENTIAL BACKUPS!)
# the Download space is the maximum fullness of hd created by youtubedownload
# or more accurately the maximum fullness during which it will initiate youtube-dl
# which when fed a URL for a playist, user, or group may try downloading many GigaBytes.
# the Snapshot space is where our rsnapshots live, the last thing we sacrifice
# when downloads in-progress demand more harddisk (we referring to /home partition).
# the FULL space limit should not be exceeded without expectation of data loss
# due to temporary bursts hitting 100% full on the partition

EOF
	exit	0
	;;
-l )
	for f in /etc/*limit
	do
		echo -n "$f:  	"
		cat $f
	done	|	sort -rk2
	exit	0
	;;
-e )	vi	$(limits -l | sed 's/:.*$//')
	exit	0
	;;
*)
	echo "${0} does not cognize ${1}" >&2
	echo "try --help or -h" >&2
	exit	1
	;;
   esac
   shift # consume the option
done

# /etc/rsnapshotlimit:    95
let rsnapshotlimit=$(	head -n 1 /etc/rsnapshotlimit)	# fullest	limit
# /etc/downloadlimit:     90
let downloadlimit=$(	head -n 1 /etc/downloadlimit)	# download	limit
# /etc/darlimit:  85
let darlimit=$(		head -n 1 /etc/darlimit)	# backups	limit
# /etc/restorelimit:      80
let restorelimit=$(	head -n 1 /etc/restorelimit)	# library	limit	
let current=$(df --full | grep /dev/mapper/fae--vg-home | sed 's/%.*$//;s/^.* //') # /home fullness
echo -n	"$(now|sed 's/+00:00/ \/home /')"
for iterate in {27..100};do
	if [[ ${iterate} -le ${rsnapshotlimit} ]]
	then
		character="s"	#SNAPSHOTS
	else
		character="f"	#FULL!
	fi
	if [[ ${iterate} -le ${downloadlimit} ]]
	then
		character="d"	#DOWNLOADS
	fi
	if [[ ${iterate} -le ${darlimit} ]]
	then
		character="b"	#BACKUPS
	fi
	if [[ ${iterate} -le ${restorelimit} ]]
	then
		character="l"	#LIBRARY
	fi
	if	[[ ${iterate} -le ${current} ]]
	then
		echo -n "$(echo $character | tr '[:lower:]' '[:upper:]')"
	else
		echo -n "$character"
	fi
done; echo ""	# finally send a newline character

exit $?

