commit a9f959b716fc5d61320f39b78c6de4c9c074fe26
Author: David Sorber <david.sorber@gmail.com>
Date:   Fri Jan 11 11:20:04 2019 -0500

    Fixed bug in hbkcd_add.sh script that prevented adding paths with
    spaces. Added item queued time to log. Created simple install script.
    Version 0.1.2.

diff --git a/software/hbkcd/conf/systemd/hbkcd.service b/software/hbkcd/conf/systemd/hbkcd.service
new file mode 100644
index 0000000..bd81988
--- /dev/null
+++ b/software/hbkcd/conf/systemd/hbkcd.service
@@ -0,0 +1,15 @@
+[Unit]
+Description=HandBrake Control Daemon service
+After=syslog.target
+
+[Service]
+Type=simple
+User=dsorber
+Group=dsorber
+#WorkingDirectory=/home/node/Node/
+ExecStart=/usr/local/bin/hbkcd
+StandardOutput=syslog
+StandardError=syslog
+
+[Install]
+WantedBy=multi-user.target
diff --git a/software/hbkcd/hbkcd.service b/software/hbkcd/hbkcd.service
deleted file mode 100644
index bd81988..0000000
--- a/software/hbkcd/hbkcd.service
+++ /dev/null
@@ -1,15 +0,0 @@
-[Unit]
-Description=HandBrake Control Daemon service
-After=syslog.target
-
-[Service]
-Type=simple
-User=dsorber
-Group=dsorber
-#WorkingDirectory=/home/node/Node/
-ExecStart=/usr/local/bin/hbkcd
-StandardOutput=syslog
-StandardError=syslog
-
-[Install]
-WantedBy=multi-user.target
diff --git a/software/hbkcd/hbkcd/command_server.py b/software/hbkcd/hbkcd/command_server.py
index 7d7b5a7..f0e694b 100755
--- a/software/hbkcd/hbkcd/command_server.py
+++ b/software/hbkcd/hbkcd/command_server.py
@@ -84,7 +84,7 @@ class CommandHandler(BaseHTTPRequestHandler):
             try:
                 content_length = int(self.headers['Content-Length'])
                 raw_content = self.rfile.read(content_length)
-                # ~ print(type(raw_content))
+                # ~ print(raw_content)
                 content = json.loads(raw_content)
                 
                 # Default to invalid command
diff --git a/software/hbkcd/hbkcd/daemon.py b/software/hbkcd/hbkcd/daemon.py
index 95ec347..cce9359 100755
--- a/software/hbkcd/hbkcd/daemon.py
+++ b/software/hbkcd/hbkcd/daemon.py
@@ -45,6 +45,18 @@ def check_configuration():
     
     return True
     
+def datetime_diff(dt_start, dt_end):
+    """ Helper function to calculate the elapsed time between to datetime 
+    objects and return in a readable "HH::MM:SS" string format.
+    """
+    elapsed = dt_end - dt_sart
+    hours, remainder = divmod(elapsed.total_seconds(), 3600)
+    minutes, seconds = divmod(remainder, 60)
+    elapsed_str = '{:02}:{:02}:{:02}'
+    elapsed_str = elapsed_str.format(int(hours), int(minutes), 
+                                     int(seconds))  
+    return elapsed_str
+    
 
 def main():
 
@@ -118,21 +130,20 @@ def main():
                 log_file.write(hbk_proc.stderr.read())
             logging.info('Write encode log file: {:s}'.format(log_path))
             
-            # Calculate elapsed time
-            elapsed = item.encode_finished - item.encode_started
-            hours, remainder = divmod(elapsed.total_seconds(), 3600)
-            minutes, seconds = divmod(remainder, 60)
-            elapsed_str = '{:02}:{:02}:{:02}'
-            elapsed_str = elapsed_str.format(int(hours), int(minutes), 
-                                             int(seconds))            
+            # Calculate elapsed time of encode
+            elapsed_str = datetime_diff(item.encode_finished, item.encode_started)
+            
+            # Calculated queued time
+            queued_str = datetime_diff(item.encode_finished, item.time_added)            
             
             # Print success or failure message to the log depending on the
             # HandBrake process's return code
             if hbk_proc.returncode == 0:
-                logging.info('Encode of {:s} completed in {:s}'.format(item.path, 
-                                                                       elapsed_str))
+                fmt_str = 'Encode of {:s} completed in {:s} (queued for: {:s})'
+                logging.info(fmt_str.format(item.path, elapsed_str, queued_str))
             else:
-                logging.error('Error while encoding {:s}'.format(item.path))
+                fmt_str = 'Error while encoding {:s} (queued for: {:s})'
+                logging.error(fmt_str.format(item.path, queued_str))
             
             # Remove the item from the queue
             ENCODE_QUEUE.remove(0)
diff --git a/software/hbkcd/install.sh b/software/hbkcd/install.sh
new file mode 100755
index 0000000..85f9d88
--- /dev/null
+++ b/software/hbkcd/install.sh
@@ -0,0 +1,35 @@
+#!/bin/bash
+
+echo -e "hbkcd Install Script\n"
+
+if [[ $EUID -ne 0 ]]; then
+    echo "ERROR: This script must be run as root" 
+    exit 1
+fi
+
+# TODO: check and install prereqs if needed
+
+# Install systemd config
+cp conf/systemd/hbkcd.service /etc/systemd/system/
+systemctl daemon-reload
+
+# Install hbkcd Python module
+./setup.py install
+
+# Install scripts
+install -m 755 scripts/hbkcd_add.sh /usr/local/bin/hbkcd_add
+install -m 755 scripts/hbkcd_queue.sh /usr/local/bin/hbkcd_queue
+install -m 755 scripts/hbkcd_remove.sh /usr/local/bin/hbkcd_remove
+
+# Start or restart hbckd 
+systemctl status hbkcd
+result="$?"
+if [ "$result" -ne 0 ]; then
+    echo "Starting hbkcd"
+    systemctl start hbkcd
+else
+    echo "Restarting hbkcd"
+    systemctl restart hbkcd
+fi
+
+echo -e "\n[complete]\n"
diff --git a/software/hbkcd/scripts/hbkcd_add.sh b/software/hbkcd/scripts/hbkcd_add.sh
index d109490..456ac73 100755
--- a/software/hbkcd/scripts/hbkcd_add.sh
+++ b/software/hbkcd/scripts/hbkcd_add.sh
@@ -11,5 +11,7 @@ fi
 command -v curl >/dev/null 2>&1 || { echo >&2 "Please install curl to use this script. Aborting."; exit 1; }
 command -v jq >/dev/null 2>&1 || { echo >&2 "Please install jq to use this script. Aborting."; exit 1; }
 
-INPUT_PATH=$(readlink -f $1)
-curl -s -X POST -d '{"type": "add", "path": "'$INPUT_PATH'", "output": "'$2'"}' http://$ADDRESS:$PORT/command | jq
+INPUT_PATH=$(readlink -f "$1")
+#~ echo '{"type": "add", "path": "'"$INPUT_PATH"'", "output": "'"$2"'"}'
+
+curl -s -d '{"type": "add", "path": "'"$INPUT_PATH"'", "output": "'"$2"'"}' -X POST http://$ADDRESS:$PORT/command | jq
diff --git a/software/hbkcd/setup.py b/software/hbkcd/setup.py
index e71c53f..e937342 100755
--- a/software/hbkcd/setup.py
+++ b/software/hbkcd/setup.py
@@ -3,7 +3,7 @@ from setuptools import setup, find_packages
 
 setup(
     name = 'hbkcd',
-    version = '0.1.1',
+    version = '0.1.2',
     description = 'HandBrake Control Daemon',
     packages = find_packages(),
     python_requires = '>=3.6.0',
