#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import icatest
import icatest.daemon
import icatest.errors
#
# Command line format:
#     ica-tcstatus get
#
args = len(sys.argv)
if args != 2 and args != 3:
    print ("Invalid parameter")
    print ("Usage: %s get" % sys.argv[0])
    sys.exit(icatest.errors.ERROR_INVALID_PARAMETER)
if sys.argv[1].lower() != "get":
    print ("Verb not supported: %s" % sys.argv[1])
    sys.exit(icatest.errors.ERROR_INVALID_PARAMETER)

# As per required by ICA, we always check state.txt
state_txt_path = "/root/state.txt"
if not os.path.exists(state_txt_path):
    print ("No state.txt file")
    sys.exit(icatest.errors.ERROR_FILE_NOT_FOUND)
if not os.path.isfile(state_txt_path):
    print ("state.txt is not a file")
    sys.exit(icatest.errors.ERROR_BAD_ENVIRONMENT)
if not os.access(state_txt_path, os.R_OK):
    print ("state.txt is not readable")
    sys.exit(icatest.errors.ERROR_BAD_ENVIRONMENT)
try:
    fd = open(state_txt_path, "r")
    status = fd.readline()
    fd.close()
    if status[-1] == '\n':
        status = status[:-1]
    print (status)
except Exception:
    print ("Failed to open %s" % state_txt_path)
    sys.exit(icatest.errors.ERROR_BAD_ENVIRONMENT)
sys.exit(icatest.errors.ERROR_SUCCESS)
# vim: ff=unix expandtab shiftwidth=4
