#!/bin/bash
###########################################################################
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the #
# Free Software Foundation; either version 2 of the License, #
# or (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. #
# See the GNU General Public License for more details. #
# #
# Written and copyright 2001 #
# by Mark Styles of Lambic Computer Consultants Inc. #
# #
# Please send Bug reports to wake@lambic.co.uk #
# #
###########################################################################
help="
Welcome to wake, an MP3 Alarm Clock.
This script is designed to be run from crontab for the days you
wish to be woken on, and at the time you wish to be woken.
Each day it will take the next n songs from your playlist, and play
them with the required snooze time between each song.
At the moment, it only works with mpg123, but it's easy enough to
change it for other media players.
The script takes several options:
-c [count] The number of files to play (default 1)
-z [snooze] The number of seconds to snooze between songs (default 300)
-l [list] The path of your playlist (defaults to ~/allsongs)
-s skip files not found instead of exiting with error
-d debug, outputs what it will do without actually doing anything
-h Show this help info
Try something like this in your crontab:
40 7 * * 1-5 /home/lambic/wake -c 5 -p 300 -s
Which will wake you up at 07:40 Mon-Fri
with 5 songs separated by 5 minutes snooze"
MP3=mpg123
OGG=ogg123
function error_exit
{
echo "$*"
exit 1
}
# Initialise
[ -f ~/.songno ] && num=`cat ~/.songno` || num=1
snooze=300
count=1
list=~/allsongs
# Get command line options
while getopts hdsc:z:l: option ; do
case $option in
c) count=$OPTARG ;;
z) snooze=$OPTARG ;;
l) list=$OPTARG ;;
s) skip=true ;;
h) echo "$help" ; exit 0 ;;
d) debug=true ;;
*) error_exit "USAGE: wake [-c
[-z
[-s] (skip files not found)"
;;
esac
done
if [ ! -z "$debug" ] ; then
echo "Playing $count songs from playlist $list"
echo "Pausing $snooze seconds between each song"
[ -z "$skip" ] && echo not skipping || echo "Skipping"
echo "Setting snooze to 0 for debugging"
snooze=0
fi
# Check playlist exists
[ ! -f $list ] && error_exit "Playlist $list does not exist"
# Check length of playlist
listlength=`wc -l < $list`
[ $listlength -eq 0 ] && error_exit "Playlist $list is empty"
# Loop until number of songs to play count is reached
songcount=0
while [ $songcount -lt $count ] ; do
# restart at beginning of playlist if end is reached
song=
[ $num -gt $listlength ] && num=1
# if skip is specified, loop through playlist until
# a file is find, otherwise exit on first file not found
if [ ! -z "$skip" ] ; then
while [ ! -f "$song" ] ; do
song=`head -$num $list | tail -1`
let num=$num+1
done
else
song=`head -$num $list | tail -1`
[ ! -f "$song" ] && error_exit "File $song not found"
let num=$num+1
fi
suffix=`echo $song | awk -F. '{print $NF}'`
case $suffix in
mp3) player=$MP3 ;;
ogg) player=$OGG ;;
esac
# Play the song
echo Playing $song with $player
[ -z "$debug" ] && $player -q "$song" || echo "$player -q $song"
# Sleep the specified snooze time
sleep $snooze
# Increment song count
let songcount=$songcount+1
done
# Update the song number file ready for next day
[ -z "$debug" ] && echo $num > ~/.songno || echo $num