##########################################################################
##                                                                      ##
##  I N T E R S T E L L A R   C O N S O L E  BG process for scheduling  ##
##                                                                      ##
##            Copyright (C) 2025- Misato Observatory, Wakayama, Japan   ##
##                                                                      ##
##  This software is available under the GPL (General Public License)   ##
##                                                                      ##
##########################################################################


#
# Procedures for handling OS-dependent processes.
#
source system-proc.tcl

#
# Base procedures
#
source -encoding $TCLSRC_ENCODING base-proc.tcl

#
# Main
#

# used files as flags
set fullpath_of_flag_light_moon_pollution [get_stellarium_io_directory]
append fullpath_of_flag_light_moon_pollution "/flag_light_moon_pollution"
set fullpath_of_flag_astronomical_dawn [get_stellarium_io_directory]
append fullpath_of_flag_astronomical_dawn "/flag_astronomical_dawn"

set pid_file [get_stellarium_io_directory]
append pid_file "/_tmp_isc-scheduler_pid.isc.txt"

set stellarium_run_url "$stellarium_url/api/scripts/run"
set stellarium_script_dir [get_stellarium_isc_script_directory_name]
if { $stellarium_script_dir eq "" } {
  puts stderr "ERROR: Unable to open localConfig.ssc"
  exit 1
}

puts "// ISC Scheduler //"
puts "stellarium_run_url: $stellarium_run_url"
puts "stellarium_script_dir: $stellarium_script_dir"
puts "fullpath_of_flag_light_moon_pollution: $fullpath_of_flag_light_moon_pollution"
puts "fullpath_of_flag_astronomical_dawn: $fullpath_of_flag_astronomical_dawn"
puts ""

# get current PID and save it
set this_pid [pid]
puts "Info: PID=$this_pid"
if { [catch {open $pid_file w} fid] } {
  puts stderr "ERROR: Unable to save a file: $pid_file"
  exit 1
} else {
  puts "Info: PID_FILE=$pid_file"
  puts $fid $this_pid
  catch {close $fid}
}

file delete -force $fullpath_of_flag_light_moon_pollution
file delete -force $fullpath_of_flag_astronomical_dawn

puts "Counter of updateLightMoonPollution.ssc and setSolarSystem.ssc:"

set cnt0 0
set cnt1 0

while { 1 } {

  # check PID in file
  if { [catch {open $pid_file r} fid] } {
    break
  } else {
    gets $fid line
    catch {close $fid}
    set pid_in_file [string trim $line]
    if { $this_pid != $pid_in_file } {
      break
    }
  }

  if { [file isfile $fullpath_of_flag_light_moon_pollution] } {
    incr cnt0
    puts -nonewline "$cnt0 $cnt1\r"
    flush stdout
    set script_file updateLightMoonPollution.ssc
    do_http_post $stellarium_run_url "id=$stellarium_script_dir/$script_file" ret0 info_string0
    #set status [string trim $ret0]
  }

  after 250

  if { [file isfile $fullpath_of_flag_astronomical_dawn] } {
    incr cnt1
    puts -nonewline "$cnt0 $cnt1\r"
    flush stdout
    set script_file setSolarSystem.ssc
    do_http_post $stellarium_run_url "id=$stellarium_script_dir/$script_file" ret1 info_string1
    #set status [string trim $ret1]
  }

  if { [file isfile $fullpath_of_flag_light_moon_pollution] } {
    incr cnt0
    puts -nonewline "$cnt0 $cnt1\r"
    flush stdout
    set script_file updateLightMoonPollution.ssc
    do_http_post $stellarium_run_url "id=$stellarium_script_dir/$script_file" ret0 info_string0
    #set status [string trim $ret0]
  }

  after 250

}

