#!/s/std/bin/python
 
# pyscript by Jake Rosin
# Last revision 3-24-07
# Version 1.0.2
##################################################################
# Licensing Info:                                                #
# Feel free to use this script generation script for             #
# all your scripted script generation needs.  You can alter      #
# the generated file as needed and rebrand it however you wish.  #
# If you make any changes to pyscript itself don't distribute    #
# them without contacting me (rosin at cs dot wisc dot edu).     # 
##################################################################

# Vital imports
import sys

from optparse import OptionParser
from subprocess import call

import os

# Let main know that 'file_text' exists. It is redefined below.
file_text = 'empty'

# Description of this script
desc = """Creates an executable Python script template at the specified location.  \
Edit the file produced to get the functionality you both want and deserve.  \
Carpe Diem!"""

# Here there be main
def main():
    """Creates a template Python script at the specified location."""
    parser = OptionParser(usage='%prog [options] file', description=desc)
    parser.add_option('-v', '--verbose', default=False, dest='verbose', action='store_true',
                      help='enables verbose output')
    parser.add_option('-g', '--group', default=False, dest='group', action='store_true',
    		      help='make the script executable by group')
    parser.add_option('-o', '--other', default=False, dest='other', action='store_true',
    		      help='make the script executable by other users')
    parser.add_option('-a', '--all', default=False, dest='all', action='store_true',
    		      help='make the script executable by all users')
		      
    options, args = parser.parse_args()

    # Is a destination specified?
    if len( args ) != 1:
    	parser.error( "Wrong number of arguments" )
    if os.access( args[0], os.R_OK ):
    	parser.error( "Destination file exists" )

    # Create the permissions string
    perm = "u"
    if options.group:
    	perm = perm + "g"
    if options.other:
    	perm = perm + "o"
    if options.all:
    	perm = perm + "a"
    perm = perm + "+x"
	
    # Copy the file
    if options.verbose:
    	print "Writing file"
    outfile = file( args[0], 'w' )
    outfile.write( file_text )
    outfile.close()
    if not os.access( args[0], os.R_OK ):
    	parser.error( "Couldn't copy the file!" )
    if options.verbose:
    	print "Adding permissions with " + perm
    retcode = call([ "chmod", perm, args[0] ])
    if options.verbose:
    	print "Done!"
    
    
########################################################################

# Defines file_text for reals

file_text = """#!/s/std/bin/python 
# A Python script

import sys

from optparse import OptionParser
from subprocess import call

import os


# Created automagically by pyscript.  Change whatever you want.
# Description of this script
desc = "This program was created by pyscript."

def main():
    parser = OptionParser(usage='%prog [options] file', description=desc)
    parser.add_option('-v', '--verbose', default=False, dest='verbose',
    		      action='store_true', help='enables verbose output')

    options, args = parser.parse_args()

    # Check that the file is there
    if len( args ) != 1:
    	parser.error( "Wrong number of arguments" )
    if args[0] == "-":
	f = sys.stdin
    elif os.access( args[0], os.R_OK ):
        f = file( args[0] )
    else:
        parser.error('File does not exist, or is unreadable')

    # Read the file
    text = f.read()
    f.close()    
    
########################################################################


if __name__ == '__main__':
    sys.exit(main())
"""


if __name__ == '__main__':
    sys.exit(main())
