#!/usr/bin/python

import sys
import shlex, subprocess
import argparse

if __name__=="__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("execute", help="start|stop|restart transmission")
  parser.add_argument("-d", "--downloads", type=str, default="", help="path to downloads directory")
  parser.add_argument("-t", "--torrents", type=str, default="", help="path to torrents-to-watch directory")
  args = parser.parse_args()
  if args.execute== "start":
    if args.downloads == "" or args.torrents == "":
      sys.exit("Error: paths to downloads and torrents must be set")
    bashCommand = "docker run --net=host -v " \
                    + args.downloads + ":/var/lib/transmission-daemon/downloads -v " \
                    + args.torrents + ":/var/lib/transmission-daemon/torrents " \
                    "--name transmission -d catatnight/transmission"
    process = subprocess.Popen(shlex.split(bashCommand))
    output = process.communicate()[0]
  elif args.execute== "stop":
    bashCommand = "docker rm -f transmission"
    process = subprocess.Popen(shlex.split(bashCommand))
    output = process.communicate()[0]
  elif args.execute== "restart":
    bashCommand = "docker rm -f transmission"
    process = subprocess.Popen(shlex.split(bashCommand))
    output = process.communicate()[0]
    if args.downloads == "" or args.torrents == "":
      sys.exit("Error: paths to downloads and torrents must be set")
    bashCommand = "docker run --net=host -v " \
                    + args.downloads + ":/var/lib/transmission-daemon/downloads -v " \
                    + args.torrents + ":/var/lib/transmission-daemon/torrents " \
                    "--name transmission -d catatnight/transmission"
    process = subprocess.Popen(shlex.split(bashCommand))
    output = process.communicate()[0]

@@ -442,35 +442,27 @@
 s()%0A
-  if args.execute== %22start%22
+%0A  def _start(args)
 :%0A  
@@ -965,36 +965,22 @@
 %5B0%5D%0A
+%0A
   
-elif args.execute== %22
+def _
 stop
-%22
+()
 :%0A  
@@ -1122,12 +1122,11 @@
 %5B0%5D%0A
+%0A
   
-el
 if a
@@ -1140,18 +1140,16 @@
 cute== %22
-re
 start%22:%0A
@@ -1156,157 +1156,33 @@
     
-bashCommand = %22docker rm -f transmission%22%0A    process = subprocess.Popen(shlex.split(bashCommand))%0A    output = process.communicate()%5B0%5D%0A    
+_start(args)%0A  el
 if args.
 down
@@ -1181,494 +1181,96 @@
 rgs.
-downloads == %22%22 or args.torrents == %22
+execute== %22stop
 %22:%0A    
-  sys.exit(%22Error: paths to downloads and torrents must be set%22)%0A    bashCommand = %22docker run --net=host -v %22 %5C%0A                    + args.downloads + %22:/var/lib/transmission-daemon/downloads -v %22 %5C%0A                    + args.torrents + %22:/var/lib/transmission-daemon/torrents %22 %5C%0A                    %22--name transmission -d catatnight/transmission%22%0A    process = subprocess.Popen(shlex.split(bashCommand))%0A    output = process.communicate()%5B0%5D
+_stop()%0A  elif args.execute== %22restart%22:%0A    _stop()%0A    _start(args)
 %0A

define _start() and _stop()