#!/bin/sh
# autorun-parts - A reverse task scheduler
# Author: Simon Volpert <simon@simonvolpert.com>
# Project page: https://simonvolpert.com/autorun-parts/
# This program is free software, released under the Apache License, Version 2.0. See the LICENSE file for more information
# Consult the README file for usage instructions and other helpful hints
min=`date +%M`
hour=`date +%H`
day=`date +%d`
mon=`date +%m`
dow=`date +%w`
if [ "$1" == "--help" ]; then
echo "Usage: autorun-parts [RESOLUTION] PATH..."
echo "Go through a directory of scripts and execute the ones that \"want\" to be executed."
echo
echo "Scheduler resolution:"
echo " -h The scheduler is executed hourly; Ignore the minute field"
echo " -d The scheduler is executed daily; Ignore the hour and minute fields"
echo " -m The scheduler is executed monthly; Ignore the day, hour and minute fields"
echo
echo "See the README for more information."
exit
fi
res=1
case "$1" in
-h)
res=2
shift
;;
-d)
res=3
shift
;;
-m)
res=4
shift
;;
esac
for d in "$@"; do
for f in "$d"/*; do
[ -x "$f" ] || continue # skip non-executable files
tag=`grep "#@" "$f"`
[ -z "$tag" ] && continue # skip files without the autorun-parts tag
tag=`echo "$tag" | tr -s [[:space:]] " "`
smin=`echo "$tag" | cut -f2 -d\ `
shour=`echo "$tag" | cut -f3 -d\ `
sday=`echo "$tag" | cut -f4 -d\ `
smon=`echo "$tag" | cut -f5 -d\ `
sdow=`echo "$tag" | cut -f6 -d\ `
# match date and time
wants=0
if [ "$smin" == "*" ] || [ $res -gt 1 ]; then
wants=$((wants + 1))
else
for i in `echo $smin | tr "," " "`; do
if [ $i -eq $min ]; then
wants=$((wants + 1))
fi
done
fi
#echo "smin $smin min $min wants $wants"
if [ "$shour" == "*" ] || [ $res -gt 2 ]; then
wants=$((wants + 1))
else
for i in `echo $shour | tr "," " "`; do
if [ $i -eq $hour ]; then
wants=$((wants + 1))
fi
done
fi
#echo "shour $shour hour $hour wants $wants"
if [ "$sday" == "*" ] || [ $res -gt 3 ]; then
wants=$((wants + 1))
else
for i in `echo $sday | tr "," " "`; do
if [ $i -eq $day ]; then
wants=$((wants + 1))
fi
done
fi
#echo "sday $sday day $day wants $wants"
if [ "$smon" == "*" ]; then
wants=$((wants + 1))
else
for i in `echo $smon | tr "," " "`; do
if [ $i -eq $mon ]; then
wants=$((wants + 1))
fi
done
fi
#echo "smon $smon mon $mon wants $wants"
if [ "$sdow" == "*" ]; then
wants=$((wants + 1))
else
for i in `echo $sdow | tr "," " "`; do
if [ $i -eq $dow ]; then
wants=$((wants + 1))
fi
done
fi
#echo "sdow $sdow dow $dow wants $wants"
# execute the file if it "wants" to be executed
if [ $wants -eq 5 ]; then
echo "executing $f"
"$f"
fi
done
done