#!/bin/sh
""":"
for cmd in python python3 python2 ; do
   command -v > /dev/null $cmd && exec $cmd $0 "$@"
done

echo "error: one of the following symlinks is missing from the environment : python python3 python2"

exit 2
":"""

# Install script for Men & Mice Authserve Agent
# Copyright (c) 2003-2022 Men & Mice http://www.menandmice.com
# If you encounter any problems running this script
# please contact support@menandmice.com

import os
import time
from subprocess import call

if os.getuid() != 0:
   raise Exception("Please run this script as the root user")

def print_key():
   time.sleep(0.2)  # Give the agent time to generate they key
   retries = 0
   while retries < 3:
      try:
         agent_setup_key = open("ssl/agent-setup.key", "r")
         print("Please continue to add the agent through Micetro with the following agent setup key:\n\t%s" % agent_setup_key.read())
         return
      except EnvironmentError:
         retries += 1
         time.sleep(0.2)

binary_symlink_location =  "/usr/sbin/mm-authserve-agent"
agent_path = os.path.abspath("bin/mm-authserve-agent")
call("./svc.sh install && ./svc.sh start", shell=True)
print_key()
try:
   os.symlink(agent_path, binary_symlink_location)
   print("Created a symlink for mm-authserve-agent binary in: " + binary_symlink_location)
except EnvironmentError:
   # FileExistsError is Python 3
   pass
