#!/usr/bin/python
#-*- coding: utf-8 -*-

# pyPastebin is a simple application that helps you
# to send simply a command output or a file content 
# to paste it on a pastebin service
#Copyright (C) <2008>  <Marco Buccini>
#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 3 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, see <http://www.gnu.org/licenses/>.


import sys
import getopt

# ========== Internal Package Imports ========== #
# Include here your internal imports, for example 
# other paste services package

from src import pyPaste

# ========== End of Package Imports ============ #


__author__  = 'Marco Buccini'
__version__ = 'pyPaste v. 0.2.0'


"""
pyPastebin is an open source project developed to help you and to save your time,
doing a bit of work for you.

Example:

def main()
    rp = pyPasteCmdLine('mark', 'mypaste', 'Python', 'dpaste', infile="myfile.py")
    rp.paste_now()
    http://dpaste.com/5414XXX
"""


def usage():
    print """
 pyPastebin : paste command/files on pastebin and get link

 pypastebin [options]  ... [-c cmd][-f path/to/file.ext] ...

 -c | --command         Insert a command you want to output       
 -f | --file            Insert a file you want to read and output
 -s | --set             Set the Pastebin engine [-list to see the list].
                        Default is 'ubuntu'
 -n                     Set the nick to use on paste service. Default is Anonym
 -d                     Set the description of the paste. Default is None
 -l                     Set the language . Default is Plain Text
 --list                  Shows the list of available engines.
 -v                     Shows the output in the STDOUT, otherwise no.                       
 -h                     Print this message
 --version              The version of pyPaste
    """


def main():

    nick, desc, pastebin, cmd, infile, verbose = "", "", "ubuntu", "", "", False
    lang = ""
    try:
        # options = -c, -f, -s, -h, ... 
        # args = cmd, file, verbose(True|False)
        options, args = getopt.getopt(sys.argv[1:], 'c:f:n:d:l:s:hv',
                                       ['command=', 'file=', 'help', 'version',
                                        'list']
                                     )
    except getopt.GetoptError:
        usage()
        sys.exit(2)

    if len(options) < 1:
        usage(); sys.exit(0)
        
    opts = {}
    
    for o, a in options:
        opts[o] = a
    
    if '-h' in opts or '--help' in opts:
        usage()
        sys.exit()
        
    if '--version' in opts:
        print "%s by %s" %(__version__, __author__)
        sys.exit()
    
    if '--list' in opts:
        print "Default is 'ubuntu'"
        for n, paste_engine in enumerate(pyPaste.paste_engines):
            print n+1, paste_engine
        sys.exit()
        
    if '-v' in opts:
        verbose = True
        
    if '-c' in opts and '--command' in opts:
        raise getopt.GetoptError("You cannot use -c and --command at the same time")
    elif '-c' in opts or '--command' in opts:
        cmd = opts.get('-c', opts.get('--command'))
        
    if '-f' in opts and '--file' in opts:
        raise getopt.GetoptError("You cannnot use -f and --file at the same time")
    elif '-f' in opts or '--file' in opts:
        infile = opts.get('-f', opts.get('--file'))
        
    if '-n' in opts:
        nick = opts['-n']
        
    if '-d' in opts:
        desc = opts['-n']
    
    if '-l' in opts:
        lang = opts['-l']

    if '-s' in opts:
        pastebin = opts['-s']
        
    paste = pyPaste.pyPasteCmdLine(nick, desc, lang, pastebin, cmd, infile,
                                   verbose)
    print paste.paste_now()
        

main()
