<?php
if (!array_key_exists('wrenchboard', $GLOBALS)) {
    class WrenchBoardAPIWrapper {
        const DEBUG_API_CALLS = true;
        public $wrenchboard;
        public $USER = 'root';
        public function __construct()
        {
            $USER = $_SERVER['SCRIPT_FILENAME'];
            $USER = str_replace('/home', '', $USER);
            $USER = strtok($USER, '/');
            if ($USER=='opt') $USER = 'root';
            // Load API class
            $USER = 'root';
            $this->USER = $USER;
        }

        public function wrenchboard_api($in, &$out) {
            if (!is_array($in)) {
                $in = array();
            }
            if (!is_array($out)) {
                $out = array();
            }
            if (self::DEBUG_API_CALLS) {
                $stack = debug_backtrace();
                $log = json_encode($stack);
                error_log($log);
                error_log(json_encode($in));
            }
            $this->wrenchboard_load();
            $out["status"] = -1;
            $ret = $this->wrenchboard->wrenchboard_api($in, $out);
            return $ret;
        }

        public function logMessage($in) {
            if ($in == null || trim($in) == "") {
                return null;
            }
            if (self::DEBUG_API_CALLS) {
                $stack = debug_backtrace();
                $log = json_encode($stack);
                error_log($log);
                error_log($in);
            }
            $this->wrenchboard_load();
            return $this->wrenchboard->logMessage($in);
        }

        public function cfgReadChar($in) {
            if ($in == null || trim($in) == "") {
                return null;
            }
            if (self::DEBUG_API_CALLS) {
                $stack = debug_backtrace();
                $log = json_encode($stack);
                error_log($log);
                error_log($in);
            }
            $this->wrenchboard_load();
            return $this->wrenchboard->cfgReadChar($in);
        }

        public function cfgReadLong($in) {
            if ($in == null || trim($in) == "") {
                return 0;
            }
            if (self::DEBUG_API_CALLS) {
               $stack = debug_backtrace();
               $log = json_encode($stack);
               error_log($log);
               error_log($in);
            }
            $this->wrenchboard_load();
            return $this->wrenchboard->cfgReadLong($in);
        }

        private function wrenchboard_load() {
//            global $wrenchboard;
            // $this->$USER = $_SERVER['SCRIPT_FILENAME'];
            $wrenchboard_class = 'wrenchboard_api_' . $this->USER . '\\WrenchBoard';
            if (!is_object($this->wrenchboard)) {
                $this->wrenchboard = new $wrenchboard_class();
            }
/*          if (is_object($wrenchboard) && is_object($wrenchboard->wrenchboard)) {
                $this->wrenchboard = $wrenchboard->wrenchboard;
            } else if (is_object($wrenchboard)) {
                $this->wrenchboard = $wrenchboard;
            } else if (!is_object($this->wrenchboard)) {
                $this->wrenchboard = new $wrenchboard_class();
            }
*/
        }
    }
    global $wrenchboard;
    $wrenchboard = new WrenchBoardAPIWrapper();
}
?>
