#!/usr/bin/python

############################################################################
#    Copyright (C) 2005 by David                                           #
#    david@base                                                            #
#                                                                          #
#    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.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
###########################################################################

"""converts an mp3 file (argument) into a CD ready WAVE file and cuts it to 74mins"""



import sys

def sox(file):
	import os
	import glob
	(path, name) = os.path.split(file)
	(shortname, extension) = os.path.splitext(name)
	print "================================================"
	print "Exporting to " + path + "/" + shortname + ".wav with mpg123..."
	print "================================================"
	print " "
	print " "
	os.system("mpg123 " + file + " -w " +path + "/" + shortname + ".wav")
	print "================================================"
	print "Outputting file to  " + path + "/" + shortname + "CD1.sox.wav..."
	print "This file will be CD ready and 74mins long"
	print "================================================"
	os.system("sox " + path + "/" + shortname + ".wav -r 44100 " + path + "/" + shortname + "CD1.sox.wav trim 0 74:00.0")
	print "================================================"
	print "cleaning up temp files....."
	print "================================================"
       # wave = glob.glob(path + '/*.wav')
	#for x in glob.glob(path + '/*.wav'):
	      #  os.remove(x)
	os.remove(path + "/" + shortname + ".wav")
	
	print "================================================"
	print "===============      DONE     =================="
	print "================================================"
	sys.exit()


def usage():
	print """
		ERROR: 
		Usage, CD http://www.tosome.media.avi
		
	CD written by DaveQB.
	"""
	sys.exit(2)

def usage2():
	print """
		ERROR: 
		The address given is a bit ambigious; not recognised as a URL or local file.
		Please note local files need to be full paths.
		
		If this is wrong, please report back to david@dward.us.
		
	CD written by DaveQB.
	"""
	sys.exit(1)

	
if len(sys.argv) == 2:
	file = sys.argv[1]
	start = file[:3]
	startf = file[:1]
	print start
	if start == "htt":
		os.system("wget " + `file`)
		sox(file)
	elif start == "ftp":
		os.system("wget " + `file`)
		sox(file)
	elif startf == "/":
		sox(file)
	else:
		usage2()
		sys.exit(1)
	# for Python 2.4
	#(path, name) = file.rsplit('/',1)
else:
	usage()
	sys.exit(1)
