#!/usr/bin/php
<?php
#
# Usage: sm_ramp [port_number]
#
# This script will increment the sound masking gain by 1 as part of a scheduled cron-job. This script should
# not be executed on its own.
#
# (C) Copyright 2022, Bogen Communications, Inc. All rights reserved.
#
#
$debug = TRUE;
$port_num = $argv[1];





if($argc == 3) {
    $opt_command = $argv[2];
}
else {
    $opt_command = "start-ramp";
}
# Read the sm_ramp_x file and 
$ramp_file = "/etc/nyquist/sm_ramp_" . $port_num;
if ($debug) echo "Ramp file: $ramp_file\n";
if(strcmp($opt_command, "start-ramp") === 0) {
    if(!file_exists($ramp_file)) {
	    if ($debug) echo "Ramp file $ramp_file does not exist\n";
        exit(0);
    }
    $ramp_info=file_get_contents($ramp_file);
    $ramp_parts = explode(' ', $ramp_info);
    $days_rem = $ramp_parts[0];
    $days_total = $ramp_parts[1];
    $sm_zone = $ramp_parts[2];
    $sm_port = $ramp_parts[3];
}

if(strcmp($opt_command, "start-ramp") === 0) {
    exec("/usr/local/bin/find_tftp_server 2>&1", $server_ip);

    #Check if audio is paused, if so, we pause our ramping (don't execute anything when cron runs)
    $get_audio_status_cmd = "sudo /usr/local/bin/get_file_from_server $server_ip[0] audio_status";
    shell_exec("bash -c \"$get_audio_status_cmd\"");
    $audio_file = './audio_status';
    sleep(3);
    #$audio_file = './audio_status' . $port_num;
    if(file_exists($audio_file)) {
        $audio_stat = trim(file_get_contents($audio_file));
        if(strcmp($audio_stat, "disable") === 0) {
            //unlink($audio_file);
            exit(0);
        }
        else {
            //unlink($audio_file);
        }
    }
    if ($debug) echo "Days remaining:  $days_rem     Days total:  $days_total\n";
    if(strcmp($days_rem, "0") !== 0) {
        # This is a temporary stub until we figure out how to interface with the soundmasking DSP component
        if ($debug) echo "Increasing gain for sound masking by 1dB on port $port_num\n";

        # Here is the magic to signal the application
        $smPipe = fopen("/usr/local/bin/extSMChannel_$port_num", "w");
        fwrite($smPipe, "$opt_command $days_rem $days_total $sm_zone");
        fclose($smPipe);

        # At this point, we have to decrement the days remaining and write it back into the ramp file
        --$days_rem;
        $new_ramp_info = $days_rem . " " . $days_total . " " . $sm_zone . " " . $sm_port;
        file_put_contents($ramp_file, $new_ramp_info);
        if ($debug) echo "  New Days remaining:  $days_rem     \n";
        if ($debug) echo "  Zone:  $sm_zone     \n";
        if ($debug) echo "  Server IP:  $server_ip[0]     \n";

        # Send an MQTT message to the server to log this change of days remaining
        # <amp-MAC-address> ramp-left <zone> <number-of-days-left>
        $mqtt_cmd = "sudo /usr/local/bin/send_mqtt_message sm_server $server_ip[0] ramp-left $sm_zone $days_rem";
        shell_exec("bash -c \"$mqtt_cmd\"");
    }
    elseif(strcmp($days_rem, "0") === 0) {
        if ($debug) echo "Last day of ramping on port $port_num\n";

        # Here is the magic to signal the application
        $smPipe = fopen("/usr/local/bin/extSMChannel_$port_num", "w");
        fwrite($smPipe, "$opt_command $days_rem $days_total $sm_zone");
        fclose($smPipe);

        #Wipe out ramp files and clean crontab
        $sm_ramp_cmd = '/usr/local/bin/sm_ramp';
        $RAMP_CRON_CMD="/usr/bin/php $sm_ramp_cmd $port_num";
	    $RAMP_CRON_JOB="0 0 * * * $RAMP_CRON_CMD";
        $sm_port_ramp_file = '/etc/nyquist/sm_ramp';
        $port_ramp_file = $sm_port_ramp_file . "_" . $port_num;

        if(file_exists($port_ramp_file)) {
			exec("unlink $port_ramp_file");
			exec("sync");
		}

		# Remove the cron job
		$CRON_UPDATE_CMD = "crontab -l | grep -v \\\"$RAMP_CRON_CMD\\\" | crontab -";
		shell_exec("bash -c \"$CRON_UPDATE_CMD\"");

		#if ($debug) echo "  $RAMP_CRON_CMD $sm_amp_cmd\n";
		#shell_exec("bash -c \"$RAMP_CRON_CMD $sm_amp_cmd\"");
    }
}
elseif(strcmp($opt_command, "stop-ramp") === 0) {
    if ($debug) echo "  Stop command:  $opt_command     \n";
    $smPipe = fopen("/usr/local/bin/extSMChannel_$port_num", "w");
    fwrite($smPipe, "$opt_command");
    fclose($smPipe);    
}