#!/usr/bin/env python
# -*- coding: utf-8 -*- #
'''A script for create formated .py or .sh files

by Wubin Qu <quwubin@gmail.com>
Copyright @ 2010, All Rights Reserved.
'''

Author = 'Wubin Qu <quwubin@gmail.com>, BIRM, China'
Date = '2010-11-2'
License = 'GPL v3'
Version = '1.2'

import sys
import os
import datetime

py_template = '''#!/usr/bin/python
# -*- coding:utf-8 -*-
\'''Short description here for this script


by Wubin Qu <quwubin@gmail.com>,
Copyright @ 2010, All Rights Reserved.
\'''

Author  = 'Wubin Qu  <quwubin@gmail.com> CZlab, BIRM, China'
Date    = '%s'
License = 'GPL v3'
Version = '1.0'

import sys
import os
from optparse import OptionParser


def get_opt():
    \'''Handle options\'''
    usage = 'Usage: %s [options]'

    version = '%s Version: ' + '%s [%s]' %s (Version, Date)
    parser = OptionParser(usage=usage, version=version)
    parser.add_option('-i', '--infile', dest='infile', help='Input file name. [String]')
    parser.add_option('-o', '--outfile', dest='outfile', help='Output file name. [String]')
    [options, args] = parser.parse_args()

    if len(args) > 1:
        parser.error('Incorrect argument, add" "-h" for help.')

    if not options.infile:
        pass

    return options

def main ():
    \'''Main\'''
    options = get_opt()

if __name__ == '__main__':
    main()

'''

sh_template = '''#!/bin/sh
# Author  = 'Wubin Qu  <quwubin@gmail.com> CZlab, BIRM, China?# Date    = '%s'
# License = 'GPL v3?# Version = '1.0'

'''

others_template = ''''''

def get_opt():
    '''Handle options'''
    usage = '''%s filename

Notes: only .py and .sh file types suported now.''' % (sys.argv[0])

    if len(sys.argv) != 2:
	print >> sys.stderr, usage
	exit()
    else:
	filename = sys.argv[1]
	if os.path.isfile(filename):
	    print >> sys.stderr, 'Error: %s already exist!' % filename
	    exit()
        suffix = os.path.splitext(filename)[1]
	if suffix not in ('.py', '.sh'):
            print >> sys.stderr, 'Warnning: unsported file type, create empty file instead.'

    return filename, suffix


def create_file(filename, suffix):
    '''Create file'''
    fh = open(filename, 'w')
    date = datetime.datetime.now().strftime('%b-%d-%Y %H:%M:%S');

    if suffix == '.py':
	file_content = py_template % (date, '%prog', '%prog', '%s', '%s', '%')
    elif suffix == '.sh':
	file_content = sh_template % date
    else:
	file_content = others_template

    fh.write(file_content)
    fh.close()

def main ():
    '''Main'''
    filename, suffix = get_opt()
    create_file(filename, suffix)


if __name__ == '__main__':
    main()


