#!/usr/bin/python

# This just creates an 'upstart' autostart script for the current path/location of edl_main - for full info see - http://www.debian-administration.org/articles/28

from edl_util import edl_call
import os

start_path = os.getcwd()
start_path += "/"

edl_main_fullpath = start_path+"edl_main"
edl_stop_fullpath = start_path+"edl_stop"

autostart_script = """
[Unit] 
Description=EcoDroidLink - edl_main script
StopWhenUnneeded=yes

[Service]
ExecStart={}
ExecStop={}
LimitNPROC=1

[Install] 
WantedBy=bluetooth.target
""".format(edl_main_fullpath, edl_stop_fullpath)

f = open('generated_autostart_file', 'w')
f.write(autostart_script)
f.close()

ret = edl_call("sudo cp generated_autostart_file /etc/systemd/system/ecodroidlink.service","autostart_gen")

if (ret!=0):
   print("error: can't write to /etc/systemd/system/ecodroidlink.service")
   exit(-1)

ret = edl_call("sudo systemctl enable ecodroidlink.service","autostart_gen")

if (ret!=0):
   print("error: failed to enable ecodroidlink.service","autostart_gen")
   exit(-1)

print("Successfully setup auto-start-on-boot for edl_main")
