| 
									
										
										
										
											2023-12-11 17:07:09 -07:00
										 |  |  | ##################################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DEPENDENCIES+=() | 
					
						
							|  |  |  | REQUIRED_ENV+=() | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ##################################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TALLY_USE_REDIS=false  # maybe someday | 
					
						
							|  |  |  | TALLY_PATH="$SCWRYPTS_DATA_PATH/tally" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | ##################################################################### | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | TALLY() { | 
					
						
							|  |  |  | 	local USAGE="
 | 
					
						
							|  |  |  | 		usage: [...options...] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		options: | 
					
						
							|  |  |  | 		  -c, --increment-count   increment the tally by this much (default 1) | 
					
						
							|  |  |  | 		  -n, --tally-name        name of tally system (default 'default') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		  -g, --get     only output the current value | 
					
						
							|  |  |  | 		  -s, --set     set the tally to a specific value | 
					
						
							|  |  |  | 		  -r, --reset   set the tally back to zero | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		  --raw   only output the tally value | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		  -h, --help   print this dialogue and exit | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		Simple tally mark system; keep track of a count. | 
					
						
							|  |  |  | 	"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	local INCREMENT_COUNT=1 | 
					
						
							|  |  |  | 	local TALLY_NAME=default | 
					
						
							|  |  |  | 	local RAW=false | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	local SET_VALUE= | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	while [[ $# -gt 0 ]] | 
					
						
							|  |  |  | 	do | 
					
						
							|  |  |  | 		case $1 in | 
					
						
							|  |  |  | 			-c | --increment-count ) INCREMENT_COUNT=$2; shift 1 ;; | 
					
						
							|  |  |  | 			-n | --tally-name      ) TALLY_NAME=$2; shift 1 ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			-g | --get   ) INCREMENT_COUNT=0 ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			-s | --set   ) SET_VALUE=$2; shift 1 ;; | 
					
						
							|  |  |  | 			-r | --reset ) SET_VALUE=0 ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			--raw ) RAW=true ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			-h | --help ) USAGE; return 0 ;; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			* ) ERROR "unknown argument '$1'" ;; | 
					
						
							|  |  |  | 		esac | 
					
						
							|  |  |  | 		shift 1 | 
					
						
							|  |  |  | 	done | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	[ $TALLY_NAME ] && echo "$TALLY_NAME" | grep -qv '/' \
 | 
					
						
							|  |  |  | 		|| ERROR "invalid tally name '$TALLY_NAME'" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	local TALLY_FILENAME="$TALLY_PATH/$TALLY_NAME.txt" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	CHECK_ERRORS --no-fail || return 1 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	########################################## | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	local NEW_VALUE CURRENT_VALUE=0 | 
					
						
							|  |  |  | 	[ $SET_VALUE ] && NEW_VALUE=$SET_VALUE || { | 
					
						
							|  |  |  | 		[ -f "$TALLY_FILENAME" ] && { | 
					
						
							|  |  |  | 			CURRENT_VALUE=$(cat "$TALLY_FILENAME" | tail -n1 | grep '^[0-9]\+') | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		[ $CURRENT_VALUE ] || { | 
					
						
							|  |  |  | 			ERROR "malformed tally file '$TALLY_FILENAME'; aborting" | 
					
						
							|  |  |  | 			return 1 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		NEW_VALUE=$(($CURRENT_VALUE + $INCREMENT_COUNT)) | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	########################################## | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	local TALLY_DIR="$(dirname "$TALLY_FILENAME")" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	[ -d "$TALLY_DIR" ] || mkdir -p "$TALLY_DIR" | 
					
						
							|  |  |  | 	[ -d "$TALLY_DIR" ] || { | 
					
						
							|  |  |  | 		ERROR "unable to write to '$TALLY_DIR'; aborting" | 
					
						
							|  |  |  | 		return 1 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	echo "# autogenerated tally file; avoid direct modification\n$NEW_VALUE" > "$TALLY_FILENAME" || { | 
					
						
							|  |  |  | 		ERROR "failed to write to '$TALLY_FILENAME': aborting" | 
					
						
							|  |  |  | 		return 1 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	########################################## | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	case $RAW in | 
					
						
							|  |  |  | 		true  ) printf "$NEW_VALUE" ;; | 
					
						
							|  |  |  | 		false ) | 
					
						
							|  |  |  | 			case $TALLY_NAME in | 
					
						
							| 
									
										
										
										
											2024-02-06 14:06:44 -07:00
										 |  |  | 				default ) SUCCESS "current tally : $NEW_VALUE" ;; | 
					
						
							|  |  |  | 				*       ) SUCCESS "$TALLY_NAME : $NEW_VALUE" ;; | 
					
						
							| 
									
										
										
										
											2023-12-11 17:07:09 -07:00
										 |  |  | 			esac | 
					
						
							|  |  |  | 	esac | 
					
						
							|  |  |  | } |