#!/bin/sh
#
# # @(#)@@SIMPLEVERSIONTAG@@
#
# Heavily inspired (aka "copied")  from /etc/periodic/daily/800.scrub-zfs
#

# If there is a global system configuration file, suck it in.
#

newline="
" # A single newline

if [ -r /etc/defaults/periodic.conf ]
then
    . /etc/defaults/periodic.conf
    source_periodic_confs
fi

: ${daily_local_trim_zfs_enable:=NO}
: ${daily_local_trim_zfs_pools=}
: ${daliy_local_trim_zfs_default_threshold:=35}
# \${daily_local_trim_zfs_$(echo "${pool}"|tr  ".:-" "_")_threshold}

case "$daily_local_trim_zfs_enable" in
    [Yy][Ee][Ss])
	echo
	echo 'TRIM of ZFS pools:'

	if [ -z "${daily_local_trim_zfs_pools}" ]; then
		daily_local_trim_zfs_pools="$(zpool list -H -o name)"
	fi

	rc=0
	for pool in ${daily_local_trim_zfs_pools}; do
		# sanity check
		_status=$(zpool list "${pool}" 2> /dev/null)
		if [ $? -ne 0 ]; then
			rc=2
			echo "   WARNING: pool '${pool}' specified in"
			echo "            '/etc/periodic.conf:daily_local_trim_zfs_pools'"
			echo "            does not exist"
			continue
		fi
		_status=${_status##*$newline}
		case ${_status} in
		*FAULTED*)
			rc=3
			echo "Skipping faulted pool: ${pool}"
			continue ;;
		*UNAVAIL*)
			rc=4
			echo "Skipping unavailable pool: ${pool}"
			continue ;;
		esac

		# determine how many days shall be between trums
		eval _pool_threshold=\${daily_local_trim_zfs_$(echo "${pool}"|tr  ".:-" "_")_threshold}
		if [ -z "${_pool_threshold}" ]; then
			_pool_threshold=${daliy_local_trim_zfs_default_threshold}
		fi

		_last_local_trim=$(zpool history ${pool} | \
		    egrep "^[0-9\.\:\-]{19} zpool trim( -w)? ${pool}\$" | tail -1 |\
		    cut -d ' ' -f 1)
		if [ -z "${_last_local_trim}" ]; then
			# creation time of the pool if no trim was done
			_last_local_trim=$(zpool history ${pool} | \
			    sed -ne '2s/ .*$//p')
		fi
		if [ -z "${_last_local_trim}" ]; then
			echo "   skipping TRIM of pool '${pool}':"
			echo "      can't get last TRIM date"
			continue
		fi

		# Now minus last trim (both in seconds) converted to days.
		_local_trim_diff=$(expr -e \( $(date +%s) - \
		    $(date -j -v -70M -f %F.%T ${_last_local_trim} +%s) \) / 60 / 60 / 24)
		if [ ${_local_trim_diff} -lt ${_pool_threshold} ]; then
			echo "   skipping TRIM of pool '${pool}':"
			echo "      last TRIM is ${_local_trim_diff} days ago, threshold is set to ${_pool_threshold} days"
			continue
		fi

		# Check general pool status (as in scrub-zfs)
		_status="$(zpool status ${pool} | grep scan:)"
		case "${_status}" in
			*"scrub in progress"*)
				echo "   scrubbing of pool '${pool}' in progress, skipping:"
				continue
				;;
			*"resilver in progress"*)
				echo "   resilvering of pool '${pool}' is in progress, skipping:"
				continue
				;;
			*)
				# VOID
				;;
		esac

		# Check whether a trim is already running
		_status="$(zpool status ${pool} | fgrep trimming)"
		case "${_status}" in
			*\(trimming\)*)
				echo "   TRIM of pool '${pool}' already in progress, skipping:"
				;;
			*)
				echo "   starting TRIM of pool '${pool}':"
				zpool trim -w ${pool}
				[ $rc -eq 0 ] && rc=1
				;;
		esac
	done
	;;

    *)
	rc=0
	;;
esac

exit $rc
