#!/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.             #
###########################################################################

import sys
import commands
import os
import urllib
import shutil

def lame(file):
	(path, name) = os.path.split(file)
	os.system("lame" + " -f --scale 3 --resample 44100 -b 80 "+name)
	(shortname, extension) = os.path.splitext(name)
	shutil.move(name + ".mp3", shortname)
	os.system("mpgsplit "+shortname+" [1/8] -o "+shortname+"a.mp3")
	os.system("mpgsplit "+shortname+" [2/8] -o "+shortname+"b.mp3")
	os.system("mpgsplit "+shortname+" [3/8] -o "+shortname+"c.mp3")
	os.system("mpgsplit "+shortname+" [4/8] -o "+shortname+"d.mp3")
	os.system("mpgsplit "+shortname+" [5/8] -o "+shortname+"e.mp3")
	os.system("mpgsplit "+shortname+" [6/8] -o "+shortname+"f.mp3")
	os.system("mpgsplit "+shortname+" [7/8] -o "+shortname+"g.mp3")
	os.system("mpgsplit "+shortname+" [8/8] -o "+shortname+"h.mp3")



def usage():
	print """
		ERROR: 
		Usage, podcast-split http://www.tosome.media.avi
		
	podcast-split written by DaveQB.
	"""
	return
def usage2():
	print """
		ERROR: 
		The address given is a bit ambigious; not recognised as a URL or local file.
		If this is wrong, please report back to david@dward.us
		
	podcast-split written by DaveQB.
	"""


def reporthook(*a):
        blk, sz, tot = a
        size = str(int(tot / 1024))
        tot2 = tot * 1.024
	current = (blk * sz) + .00000
	per = str(int((current / tot2) * 100))
	print '\r' + per + "%" + " of " + str(size) + "kb's",
	sys.stdout.flush()

# dependacy check
if commands.getoutput("which lame")[-4:] != "lame":
	print """You need lame. Either it's not installed or not in your path.
	Download from http://lame.sourceforge.net/"""
	sys.exit(2)
	
if commands.getoutput("which mpgtx")[-5:] != "mpgtx":
	print """You need mpgtx package.  Either it's not installed, not in your path or mpgsplit symlink doesnt exist.
	Download from http://mpgtx.sourceforge.net/"""
	sys.exit(2)

if len(sys.argv) == 2:
	file = sys.argv[1]
	start = file[:3]
	startf = file[:1]
	if start == "htt" or start == "ftp":
		#os.system("wget " + `file`)
		for url in sys.argv[1:]:
		        i = url.rfind('/')
		        file2 = url[i+1:]
		        print url, "->", file2
	       		urllib.urlretrieve(url, file2, reporthook)
		lame(file)
	elif os.path.isfile(file):
                lame(file)
	else:
		usage2()
		sys.exit(1)
	# for Python 2.4
	#(path, name) = file.rsplit('/',1)
else:
	usage()
	sys.exit(1)
