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

"""vol

volume control in the shell for your mac

Usage:
    vol (in|out|alert) <volume>
    vol mute
    vol unmute
    vol load <profile>
    vol info

    vol (-h | --help)
    vol --version

Options:
    -h --help     Show this screen.
    --version     Show version.

"""

import ConfigParser
import os
import subprocess
import sys

from docopt import docopt

from . import __version__ as version


def asrun(ascript):
    "Run the given AppleScript and return the standard output and error."

    ## shamelessly stolen from  
    ## http://www.leancrew.com/all-this/2013/03/combining-python-and-applescript/

    osa = subprocess.Popen(['osascript', '-'],
        stdin=subprocess.PIPE,
        stdout=subprocess.PIPE)

    return osa.communicate(ascript)[0]

def setvolume(device, amount):
    "Set the volume to 'amount' on 'device' "

    if device == 'out':
        device = 'output'
    if device == 'in':
        device = 'input'

    return asrun('set volume {0} volume {1}'.format(device, amount) )

def main():
    "Run the main programm."
    args = docopt(__doc__, version='vol version '+ version)
    
    #print args ## todo delete!
    
    if args['load'] and args['<profile>']:
        print args['<profile>']

        home = os.path.expanduser("~")
        cfpath = os.path.join(home, '.vol')

        if os.path.exists(cfpath):
            pass
        else:
            print "{0} does not exist".format(cfpath)

        print cfpath

        ## todo implement!

    elif args['info']:
        print asrun('get volume settings')

    elif args['mute']:
        asrun('set volume output muted true')

    elif args['unmute']:
        asrun('set volume output muted false')

    elif args['out']:
        setvolume('out', args['<volume>']) 

    elif args['in']:
        setvolume('in', args['<volume>'])

    elif args['alert']:
        setvolume('alert', args['<volume>'])


    sys.exit(0)

@@ -1322,20 +1322,19 @@
       cf
-path
+ile
  = os.pa
@@ -1368,51 +1368,89 @@
     
-if os.path.exists(cfpath):%0A            pass
+try:%0A            cfg = ConfigParser.RawConfigParser()%0A            cfg.read(cfile)
 %0A   
@@ -1455,18 +1455,22 @@
        e
-ls
+xcept 
 e:%0A     
@@ -1483,18 +1483,32 @@
  print %22
-%7B0
+Error: %7B0%7D in %7B1
 %7D does n
@@ -1537,29 +1537,8 @@
 h)%0A%0A
-        print cfpath%0A
 %0A   

read config