#!/bin/bash
#                       /usr/local/bin/limits
#  http://crystalfaeries.net/posix/bin/limits
# celeste:crystalfaery LIMITS 2019-08-14 23:08:06+00:00
# limits displays the fullness of /dev/mapper/fae--vg-home
# as a percentage graph 100 characters long... with
# Capital letters = disk is this full
# Lower Case = disk space not yet occupied
# LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLdddbbbsssffff
# L = Library Space we auto-fill from backups/library removable media
# d = Download Space available for downloads.
# b = Backup Space for *.dar
# s = Snapshot space for rsnapshot.
# f = FULL! the filesystem is not efficient.

# The LIMIT measured in % fullness of partition (adjustable while system is running):
# /etc/restorelimit:   84 Restore audio from media to HD up to limit: /usr/local/bin/sync{sd,usb}
# /etc/downloadlimit:  90 Download from the internet	 up to limit: /usr/local/bin/youtubedownload
# /etc/darlimit:       93 Delete oldest .dar backups   down to limit: /usr/local/bin/darlimit.fey
# /etc/rsnapshotlimit: 96 Delete oldest rsnapshots     down to limit: /usr/local/bin/rsnapshotlimit.fey

let help=21 # (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
	;;
--help | -h )
	head -n ${help} ${0} >&2
	exit	0
	;;
*)
	echo "${0} does not cognize ${1}" >&2
	echo "try --help or -h" >&2
	exit	1
	;;
    esac
    shift # consume the option
done

let rsnapshotlimit=$(	head -n 1 /etc/rsnapshotlimit)	# fullest	limit
let darlimit=$(		head -n 1 /etc/darlimit)	# backups	limit
let downloadlimit=$(	head -n 1 /etc/downloadlimit)	# download	limit
let restorelimit=$(	head -n 1 /etc/restorelimit)	# library	limit	
let current=$(df --full | grep /dev/mapper/fae--vg-home | sed 's/%.*$//;s/^.* //') # /home fullness
for iterate in {1..100};do
	if [[ ${iterate} -le ${rsnapshotlimit} ]]
	then
		character="s"	#SNAPSHOTS
	else
		character="f"	#FULL!
	fi
	if [[ ${iterate} -le ${darlimit} ]]
	then
		character="b"	#BACKUPS
	fi
	if [[ ${iterate} -le ${downloadlimit} ]]
	then
		character="d"	#DOWNLOADS
	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 ""
exit $?

