#!/usr/bin/python
##	jar2jad.py - main module
##
##	Copyright (C) 2005 Mohammad Hafiz bin Ismail (mypapit)
##
##	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., 675 Mass Ave, Cambridge, MA 02139, USA.
##
##
##	Report problems and direct all questions to:
##
##	<mypapit@gmail.com>
##
##	For latest version of this software, please visit :
##
##	http://mypapit.net/pproject
##	http://blog.mypapit.net/
##
##	authors email : mypapit@gmail.com, papit01@lycos.com
##

import zipfile
import string
import sys
import os

"jar2jar.py - Produce JAD (Java Application Descriptor) file, for Java Mobile application installation"
version = "0.1"
def extractjar (fh,infile):
     try :
         fin = fh.read("META-INF/MANIFEST.MF")
     except:
         print infile,": Manifest file not found"
         print infile,":", "Must be a Jarfile"
         sys.exit(1)

     filesize = os.stat(infile).st_size
##     print filesize
     
     fin+="MIDlet-Jar-Size: " + str(filesize) + "\n"
     fin+="MIDlet-Jar-URL: " + str(infile) + "\n"
     
     jadfile = infile[:string.find(infile,".")]+".jad"

     try :
        fout = open(jadfile,"w")
     except:
        print jadfile,":", sys.exc_info()[1]
        sys.exit(1)

     print "done."        

     fout.write(fin)
     fout.close
     fh.close
    
     
def mainprogram():
     print "jar2jad - produce JAD file from JAR, useful for J2ME applications\n"
     argc = len(sys.argv)
     if argc <2:
         usage()
         sys.exit(1)
## check file exist or not
     try:
        fh = zipfile.ZipFile(sys.argv[1],"r")
     except:
         print sys.argv[1],":",sys.exc_info()[1]
         usage()
         
## commence extraction
     extractjar(fh,sys.argv[1])     
    
     
 
def usage():
     print "usage : jar2jad filename.jar"
     sys.exit(1)


if __name__ == '__main__':
    mainprogram()
    
    
    
    
     
             

             
     


