#!/bin/bash
#Program:	mp3Record
#Author:	Tony McCallie
#Notes:		See INSTALL

NOARGS=0
E_BADARGS=65
VERSION=1.4

#if [ $# = $NOARGS ]
#then
#	echo "Usage: `basename $0` filename (without .mp3)"
#	exit $E_BADARGS
#fi

#
#Remove previos tmp file
#
`rm -f mp3record.tmp`

echo
echo "mp3record v. $VERSION"
echo "Written by Tony McCallie"
echo "http://www.bluedogtek.com"
echo 
#echo "NOTE: This program requires sox and lame to be installed"
#echo "Make sure you run aumix to make sure the"
#echo "proper recording source is selected"
#echo "Default answers are marked in []"
#read -p "Continue?[Y/n]: " bolContinue
#
#if [ $bolContinue ] 
#then 
#	if [ $bolContinue = "n" ] || [ $bolContinue = "N" ]
#	then
#		echo "Exiting"
#		exit 0 
#	fi
#fi

#
#Create tmp file to let the system know that the program is running
#this is deleted to kill the loop
#
`touch mp3record.tmp`

#
#Get filename
#Default "recorded_yearmody_hrmi.mp3"
#
strFileName=`date +"%Y%m%d_%H%M"`
strFileName="`pwd`/recorded_$strFileName.mp3"
read -p "Filename? [$strFileName]: " strTempFileName
if [ $strTempFileName ] 
then 
	strFileName=$strTempFileName
fi

#
#Get TimeLimit
#Default = 0 (No Limit)
#
read -p "Time Limit? (Minutes) [0 = No Limit]: " intTimeLimit
let "intTimeLimit = intTimeLimit * 60"

#
#Get Frequnecy
#Default = 44100
#
strFreqRate=44100
read -p "Frequency? [44100]: " strTempFreqRate
if [ $strTempFreqRate ]
then
	strFreqRate=$strTempFreqRate
fi

#
#Get BitRate
#Default = 128
#
strBitRate=128
read -p "BitRate? (128 is CD quality) [128]: " strTempBitRate
if [ $strTempBitRate ]
then
	strBitRate=$strTempBitRate
fi

#
#Get command to run on finish
#Hang up ppp0 etc
#
read -p "Would you like to run a command when finished?[N/y] " bolHangup

if [ $bolHangup ]
then
	if [ $bolHangup = "Y" ] || [ $bolHangup = "y" ]
	then
		read -p "Command (e.g. ifdown ppp0 or poweroff): " command
	fi
fi

#
#Prepare to start recording
#
echo "Recording of $strFileName will begin when you hit enter."
echo
read -p "Hit ENTER to begin recording, hit ENTER again to stop recording" temp

#
#Subprocess record
#The heart of this script, records mp3 file with specified params
#
(sox -r $strFreqRate -t ossdsp -w -s /dev/dsp -t raw -c 2 - \
| lame -s 44.1 -x -b $strBitRate -m s - $strFileName) &

#
#Subprocess: loop
#Loops to display time/size status while mp3record.tmp exists
#
(
#
#Function: formattime
#Displays time differenct in standardized format HH:MM:SS
#
function formattime()
{
        intHour=0
        intMin=0
        intSec=0
        intCount=0
	let "intTime = $1"
        while [ "$intCount" -lt "$1" ]
        do
                let "intSec+=1"
                if [ $intSec = 60 ]
                then
                        let "intMin+=1"
                        let "intSec=0"
                fi
                if [ $intMin = 60 ]
                then
                        let "intHour+=1"
                        let "intMin=0"
                fi
                let "intCount+=1"
        done
	ten=10
	if [ "$intSec" -lt "$ten" ]
	then
		strSec="0$intSec"
	else
		strSec="$intSec"
	fi
	
	if [ "$intMin" -lt "$ten" ]
	then
		strMin="0$intMin"
	else
		strMin="$intMin"
	fi
	
	if [ "$intHour" -lt "$ten" ]
	then
		strHour="0$intHour"
	else
		strHour="$intHour"
	fi
	
        echo "Time passed: $strHour:$strMin:$strSec"
}

intStart=`date +%s`
bolLoop="true"

#
#Core loop
#
while [ $bolLoop = "true" ]
do
		clear
		intCurrent=`date +%s`
		echo "Hit ENTER to stop recording"
		echo `ls -sh $strFileName`
		let "intDiff = intCurrent - intStart"
		
		formattime $intDiff
	sleep 1
	if [ ! -e mp3record.tmp ]
	then
		let "bolLoop=false"
	fi
done

#
#Execute finish command
#
if [ $bolHangup ]
then
	if [ $bolHangup = "Y" ] || [ $bolHangup = "y" ]
	then
		echo "Executing '$command'"
		$command
	fi
fi

#
#End recording
#
`killall sox`

#
#Kill subprocess
#
exit 0
) & 

echo "Hit ENTER to stop recording"

#
#Check Timelimit or wait for ENTER
#
if [ ! $intTimeLimit = 0 ]
then
	read -t $intTimeLimit temp
else
	read temp
fi

#
#Delete tmp file to kill subprocess record
#
`rm -f mp3record.tmp`

exit 0
