#!/bin/csh ############################################################### # This selects a file to play from the list of files found # in the file whose name is passed in the first argument. # It bases its choice on the current play mode: # inorder = alphabetically (which will be track order) # random = select one at random # oldest = one least-recently played (based on access time) # # It prints out the name of the file and then quits. If there # are no files, then it doesn't print anything. # It returns 0 on success, nonzero on failure. ############################################################## ############################################################## # Argument checking # Make sure they passed in the name of a file set choice_file = $1 if (0 == `echo -n "$choice_file" | wc -c`) then exit 1 endif # Make sure that the file exists if (-e "$choice_file") then else exit 1 endif # Make sure the file is not empty if ( 0 == `wc "$choice_file" -l` ) then exit 1 endif ############################################################## # Find out what play mode we are in. set play_mode = `musicbox_report_play_mode.txt` ############################################################## # Now pick a file based on the mode. if ( inorder == "$play_mode" ) then # Pick the first one alphabetically. Because they are # sorted alphabetically already, this is the first! cat "$choice_file" | head -1 else if ( random == "$play_mode" ) then # Find out which one to play based on a random number # between 1 and the number of elements we have. set count = `wc -l "$choice_file"` set which = `musicbox_random_number.txt $count` cat "$choice_file" | head -$which | tail -1 else if ( oldest == "$play_mode" ) then # Find the file with the oldest access time and play # that one. We do this by using the 'ls' command to # sort by access time, and sorting in reverse order. set files = `cat "$choice_file"` ls --sort=time --time=access -r $files | head -1 else # Unknown play mode! exit 1 endif exit 0