scripts

shell scripts
git clone git://git.bitsmanent.org/scripts
Log | Files | Refs | README

wally (1207B)


      1 #!/bin/sh
      2 #
      3 # TODO
      4 # - handle network errors
      5 
      6 rand="shuf -zn 1 -i %d-%d"
      7 NL="
      8 "
      9 
     10 d() {
     11 	echo "$@"
     12 	log "$@"
     13 }
     14 
     15 die() {
     16 	echo "$@"
     17 	exit 1
     18 }
     19 
     20 log() {
     21 	echo "$@" >> "$LOGFILE"
     22 }
     23 
     24 obj_items() {
     25 	items="$1"
     26 	index="$2"
     27 	n=0
     28 
     29 	oifs="$IFS"
     30 	IFS=" $NL	"
     31 	for item in $items; do
     32 		[ -z "$item" ] && continue
     33 		n="$(echo "$n+1" |bc)"
     34 
     35 		if [ ! -z "$index" -a "$index" = "$n" ]; then
     36 			echo -n "$item"
     37 			return 0
     38 		fi
     39 	done
     40 	IFS="$oifs"
     41 	echo -n "$n"
     42 }
     43 
     44 isfunc() {
     45 	type $1 |grep -q function
     46 }
     47 
     48 usage() {
     49 	echo "Usage: $0 [-h] -f <file>"
     50 	exit 1
     51 }
     52 
     53 main() {
     54 	config=~/.wallyrc
     55         while getopts "f:h" o; do
     56                 case $o in
     57 		f) config="$OPTARG";;
     58 		h) usage;;
     59                 esac
     60         done
     61 	shift "$(echo "$OPTIND - 1"|bc)"
     62 
     63 	[ -z "$config" ] && die "$0: missing configuration"
     64 	. "$config"
     65 
     66 	totservices="$(obj_items "$ENABLED_SERVICES")"
     67 	log "Running on $(date)..."
     68 
     69 	while true;
     70 	do
     71 		index="$($(printf "$rand" 1 "$totservices"))"
     72 		service="$(obj_items "$ENABLED_SERVICES" "$index")"
     73 		use="${service}_use"
     74 		isfunc "$use" || use=useresult
     75 		res="$($service)"
     76 		err=$?
     77 
     78 		if [ $err -ne 0 ]; then
     79 			log "ERROR: $service: $res"
     80 		else
     81 			$use "$res"
     82 		fi
     83 
     84 		sleep "$DELAY"
     85 	done
     86 }
     87 
     88 main "$@"