#!/usr/bin/hy

(import subprocess)
(import sys)
(import os)

(defn hytouch [package-dict]
  package-dict)

(defn hytouch-tasks []
  (.get package.package "tasks" ))

(defn hytouch-dependencies []
  (.get package.package "dependencies" ))

(defn hytouch-run [task]
  (subprocess.run (task-exec-command task) :env (gen-exec-env "python3.7")))

(defn install-deps []
  (subprocess.run ["mkdir" ".hytouch"])
  (for [repo (hytouch-dependencies)]
    (subprocess.run ["pip" "install" (install-option) repo] :env (gen-exec-env "python3.7"))))

(defn gen-exec-env [pyver]
  {"PYTHONPATH" (.format "{0}/.hytouch/lib/{1}/site-packages/" (os.getcwd) pyver)
  "PATH" (.format "{0}/.hytouch/bin:{1}" (os.getcwd) (.get os.environ "PATH"))
  })

(defn task-exec-command [task]
  (.get (hytouch-tasks) task))

(defn install-option []
  (.format "--install-option=--prefix={0}/.hytouch" (os.getcwd)))

(import package)

(defn job-type [] (get sys.argv 1))
(defn task-name [] (get sys.argv 2))
(defn show-usage [] (print "usage:\nhytouch run [task-name] - run specified task\nhytouch install - install dependencies"))

(cond [(< (len sys.argv) 2) (show-usage)]
[(= (job-type) "run") (hytouch-run (task-name))]
[(= (job-type) "install") (install-deps)]
[True (show-usage)])
