#!/bin/bash function usage() { printf "Usage\n\t$(basename $0) \ -b -n -l [-c ]\n\t\ Makes an equiphasic polyphasic schedule.\n\t\t\ -b \tbase time of schedule\n\t\t\ -n \tnumber of naps in a day\n\t\t\ -l \tlength of each nap\n\t\t\ -c \tadd a core long\n\t\ Accepts minutes or hh:mm for all arguments.\n\t\ First nap or core starts at time.\n" >/dev/stderr exit 1 } function mt() { printf "%02d:%02d" $(($1/60)) $(($1%60)) } [ $# -lt 6 ] && usage while getopts b:n:l:c: OPT;do [ -n "${OPTARG//[0-9:]}" -o -z "${OPTARG//:}" ] && { echo \ "*** Invalid argument to $OPT: $OPTARG" >/dev/stderr;usage ; } if [ "$OPTARG" != "${OPTARG/:}" ];then hour=${OPTARG/:*} min=${OPTARG/*:} OPTARG=$((${hour:-0}*60+${min:-0})) fi case "$OPT" in b) base_time=$OPTARG ;; n) nap_count=$OPTARG ;; l) nap_length=$OPTARG ;; c) core_length=$OPTARG ;; ?) usage; exit -1 ;; esac done [ $base_time -a $nap_count -a $nap_length ] || usage [ $base_time -gt 1439 ] && { echo "*** Base time out of range: $(mt $base_time)"\ >/dev/stderr; usage ; } wake_length=$(((1440-(core_length+nap_count*nap_length))/\ (core_length>0?nap_count+1:nap_count))) nap_num=1 if [ $core_length ];then printf "Core: $(mt $base_time)-$(mt $((base_time+core_length)))\n" base_time=$((base_time+core_length+wake_length)) fi while [ $nap_num -le $nap_count ];do base_time=$(($base_time>=1440?$base_time-1440:$base_time)) printf "Nap $nap_num: $(mt $base_time)-" base_time=$((($base_time+nap_length)>=1440?$base_time-1440:$base_time)) printf "$(mt $((base_time+nap_length)))\n" base_time=$((base_time+wake_length+nap_length)) nap_num=$((nap_num+1)) done