#!/usr/bin/env python

import sys

if sys.argv[-1] == "systemd":

    import os
    os.system("systemctl stop espresso")
    os.system("rm /etc/systemd/system/espresso.service")
    os.system("""
echo '''[Unit]
Description=espresso

[Service]
Type=simple
ExecStart=/usr/bin/espresso
Restart=always

[Install]
WantedBy=multi-user.target
''' >> /etc/systemd/system/espresso.service
    """)

    os.system("systemctl stop espresso")
    os.system("rm /etc/systemd/system/espresso.service")
    os.system("""
echo '''[Unit]
Description="espresso restart"

[Service]
Type=simple
ExecStart='/usr/bin/espresso restart'
Restart=always

[Install]
WantedBy=multi-user.target
''' >> /etc/systemd/system/espresso.service
    """)

    os.system("systemctl stop espresso")
    os.system("rm /etc/systemd/system/espresso.timer")
    os.system("""
echo '''[Unit]
Description="espresso check status"

[Timer]
OnBootSec=5s
OnUnitActiveSec=180m
Unit=espresso.service

[Install]
WantedBy=timers.target
''' >> /etc/systemd/system/espresso.timer
    """)

    os.system("systemctl daemon-reload")
    print("execute 'systemctl start espresso' for start the daemon")
    print("execute 'systemctl start espresso.timer' for start the timer daemon")
    sys.exit()


if sys.argv[-1] == 'restart':
    import os
    print('Restarting espresso...')
    os.system("systemctl restart espresso")
    sys.exit()


print("Run 'espresso systemd' as superuser for create the daemon.")
from espresso import main
