#!/bin/bash # $HeadURL: https://svn.pasta.freemyip.com/main/miniade/trunk/bin/nop-sh $ $LastChangedRevision: 10133 $ # Modules . $(miniade) || { echo "${0##*/}: ERROR: miniade failed (hint: run 'miniade' to see error)" >&2; exit 1; } # Configurable stuff # Other globals main() { local MY_ARGS PADDING EXPIRY PROGNAME MODE # Defaults for options PADDING=3 EXPIRY=5 MODE=display # Process options special_opts_handler() { case $1 in --padding=*) PADDING=${1#*=} ;; --expiry=*) EXPIRY=${1#*=} ;; --reset) MODE=reset ;; *) return 1 ;; esac } miniade_process_options --special-opts-handler=special_opts_handler --help-handler=help MY_ARGS "$@" && set -- "${MY_ARGS[@]}" # Process arguments [ $# = 0 ] || miniade_bad_usage # Sanity checks and derivations miniade_get_progname PROGNAME CACHE_DIR=${XDG_CACHE_HOME:-$HOME/.cache}/$PROGNAME mkdir -p $CACHE_DIR CACHE_FILE=$CACHE_DIR/last-used # Guts # If resetting, then remove cache and exit. if [ $MODE = reset ]; then rm -f $CACHE_FILE return fi # Remove expired cache. if [ -f $CACHE_FILE ] && (($EPOCHSECONDS > $(stat -c %Y $CACHE_FILE) + $EXPIRY)); then miniade_debug 10 "main: removing expired cache ..." rm -f $CACHE_FILE fi # Create cache if necessary. if [ ! -f $CACHE_FILE ]; then miniade_debug 10 "main: initialising cache ..." echo 0 > $CACHE_FILE fi NEXT_NUM=$(($(cat $CACHE_FILE) + 1)) # It's more important not to produce the same number twice than # to risk there being jumps, so save the used number *before* # displaying it. echo $NEXT_NUM > $CACHE_FILE printf "%0*d\\n" $PADDING $NEXT_NUM } help() { local PROGNAME miniade_get_progname PROGNAME echo "Usage: $PROGNAME [ ] { --reset | [ --padding= ] [ --expiry= ] }" echo echo "Defaults:" echo " padding: $PADDING" echo " expiry: ${EXPIRY}s" exit 0 } main "$@"