#!/usr/bin/python3

import sys
import subprocess

# @TODO remove this when prepared for delivery
sys.stdout = open('/tmp/fiber-can-launch.log', 'w')

# Count the arguments
arguments = len(sys.argv) - 1
print ("The script %s is called with %i arguments" % (sys.argv[0], arguments))

# Output arguments
position = 1
jsonStr = None
while (arguments >= position):
    print ("Parameter %i: %s" % (position, sys.argv[position]))
    if position == 2:
        jsonStr = sys.argv[position]
    position = position + 1

# Check that the OTDR module is available
rc = subprocess.run("/usr/bin/otdr-detection.sh")

if rc.returncode != 0:
    print ("ERROR_MODULE^OTDR")
    print ("Test requires OTDR module. Ensure the module is properly attached.")
    sys.exit(1)
else:
    print ("OTDR module detected, continuing...")

sys.stdout.close()
sys.exit(0)
