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


# Default sky culture of ISC
set sky_culture_value "japanese_misato-art"

# Default LANG of ISC
set app_language_value "system"
set flag_save_app_language_value "false"


####################################################################

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


#
# Main
#

set stellarium_version "unknown"
set stellarium_major_version "unknown"

set fullpath_of_config_ini [get_stellarium_io_directory]
append fullpath_of_config_ini "/config.ini"
set fullpath_of_isc_initial_settings [get_stellarium_io_directory]
append fullpath_of_isc_initial_settings "/initialSettings.isc.txt"
set fullpath_of_stellarium_version [get_stellarium_io_directory]
append fullpath_of_stellarium_version "/stellarium_version.isc.txt"

puts "Info: fullpath_of_config_ini = $fullpath_of_config_ini"
puts "Info: fullpath_of_isc_initial_settings = $fullpath_of_isc_initial_settings"

if { $fullpath_of_config_ini ne "" } {

  set lst [split $fullpath_of_config_ini "/"]

  set flg_found_config_ini 0

  # Confirm config.ini.  Create directories if not found them.
  #
  set tmp_path ""
  set len_lst [llength $lst]
  for {set i 0} {$i < $len_lst} {incr i} {
    set v [lindex $lst $i]
    append tmp_path $v
    #puts "tmp_path: $tmp_path"
    set flg_drive 0
    if { $i == 0 && [string length $v] == 2 && [string index $v 1] eq ":" } {
      # This is a DRIVE
      set flg_drive 1
    } else {
      # This is a directory or a file
      if { [expr $i + 1] == $len_lst } {
        if { [file isfile $tmp_path] == 1 } {
          set flg_found_config_ini 1
        } else {
          set flg_found_config_ini 0
        }
        break
      } else {
        if { [file isdirectory $tmp_path] == 0 } {
          puts "Info: mkdir $tmp_path"
          file mkdir $tmp_path
        }
      }
    }
    append tmp_path "/"
  }

  #puts "tmp_path: $tmp_path"

  if { $fullpath_of_isc_initial_settings ne "" } {
    if { [catch {open $fullpath_of_isc_initial_settings r} fid] } {
      # do nothing
    } else {
      set i 0
      while { [gets $fid line] >= 0 } {
        set lst [split $line ","]
        if { 1 < [llength $lst] } {
          set s0 [string trim [lindex $lst 0]]
          if { $s0 eq "AppLanguage" } {
            set s1 [string trim [lindex $lst 1]]
            if { $s1 ne "" } {
              set app_language_value $s1
            }
          } elseif { $s0 eq "FlagSaveAppLanguage" } {
            set s1 [string trim [lindex $lst 1]]
            if { $s1 ne "" } {
              set flag_save_app_language_value $s1
            }
          } elseif { $s0 eq "SkyCulture" } {
            set s1 [string trim [lindex $lst 1]]
            if { $s1 ne "" } {
              set sky_culture_value $s1
            }
          }
        }
      }
      close $fid
    }
  }

  array set lines_config_ini {}

  # Read all lines of config.ini
  #
  if { 1 } {

    set len_lines 0
    if { $flg_found_config_ini != 0 } {
      if { [catch {open $fullpath_of_config_ini r} fid] } {
        set len_lines 0
      } else {
        set i 0
        while { [gets $fid line] >= 0 } {
          set lines_config_ini($i) $line
          incr i
        }
        close $fid
        set len_lines $i
      }
    }

    # test existence of section
    #
    set flg_section_plugins_load_at_startup 0
    set flg_section_RemoteControl 0
    set flg_section_color 0
    set flg_section_localization 0
    set flg_section_main 0
    set flg_0 0
    set flg_1 0
    set flg_2 0
    set flg_3 0
    set flg_4 0
    set flg_5 0

    for {set i 0} {$i < $len_lines} {incr i} {

      set line [string trim $lines_config_ini($i)]
      #puts $lines_config_ini($i)
      set status [regexp -- {^[\[]([^\[\]]+)[\]]} $line match s0]
      if { $status == 1 } {
        set flg_section_plugins_load_at_startup 0
        set flg_section_RemoteControl 0
        set flg_section_color 0
        if { $s0 eq "plugins_load_at_startup" } {
          set flg_section_plugins_load_at_startup 1
        } elseif { $s0 eq "RemoteControl" } {
          set flg_section_RemoteControl 1
        } elseif { $s0 eq "color" } {
          set flg_section_color 1
        } elseif { $s0 eq "localization" } {
          set flg_section_localization 1
        } elseif { $s0 eq "main" } {
          set flg_section_main 1
        }
      }

      if { $flg_section_plugins_load_at_startup != 0 } {
        set status [regexp -- {^([^= ]+)[ ]*[=][ ]*([^= ]+)} $line match s0 s1]
        if { $status == 1 && $s0 eq "RemoteControl" && $s1 eq "true" } {
          set flg_0 1
        }
      }
      if { $flg_section_RemoteControl != 0 } {
        set status [regexp -- {^([^= ]+)[ ]+[=][ ]+([^= ]+)} $line match s0 s1]
        if { $status == 1 && $s0 eq "autostart" && $s1 eq "true" } {
          set flg_1 1
        } elseif { $s0 eq "enable_cors" && $s1 eq "true" } {
          set flg_2 1
        }
      }
      if { $flg_section_localization != 0 } {
        set status [regexp -- {^([^= ]+)[ ]*[=][ ]*([^= ]+)} $line match s0 s1]
        if { $flag_save_app_language_value == "true" } {
          if { $status == 1 && $s0 eq "app_locale" && $s1 eq $app_language_value } {
            set flg_3 1
          }
        }
        set status [regexp -- {^([^= ]+)[ ]*[=][ ]*([^= ]+)} $line match s0 s1]
        if { $status == 1 && $s0 eq "sky_culture" && $s1 eq $sky_culture_value } {
          set flg_4 1
        }
        if { $flag_save_app_language_value == "true" } {
          set status [regexp -- {^([^= ]+)[ ]*[=][ ]*([^= ]+)} $line match s0 s1]
          if { $status == 1 && $s0 eq "sky_locale" && $s1 eq $app_language_value } {
            set flg_5 1
          }
        }
      }
      if { $flg_section_main != 0 } {
        set status [regexp -- {^([^= ]+)[ ]*[=][ ]*([^= ]+)} $line match s0 s1]
        if { $status == 1 && $s0 eq "version" } {
          set stellarium_version $s1
          set status [regexp -- {^([0-9]+)} $stellarium_version match s2]
          if { $status == 1 } {
            set stellarium_major_version $s2
          }
        }
      }

    }

    # Append lines to be changed

    if { $flg_0 == 0 || $flg_1 == 0 || $flg_2 == 0 || $flg_3 == 0 || $flg_4 == 0 || $flg_5 == 0 } {
      #
      set lines_config_ini($len_lines) ""
      incr len_lines
      set lines_config_ini($len_lines) "\[plugins_load_at_startup\]"
      incr len_lines
      set lines_config_ini($len_lines) "RemoteControl = true"
      incr len_lines
      set lines_config_ini($len_lines) ""
      incr len_lines
      set lines_config_ini($len_lines) "\[RemoteControl\]"
      incr len_lines
      set lines_config_ini($len_lines) "autostart = true"
      incr len_lines
      set lines_config_ini($len_lines) "enable_cors = true"
      incr len_lines

      # Not required in v25-
      if { $stellarium_major_version < 25 } {
        puts "Info: Updated orbit colors."
        set lines_config_ini($len_lines) ""
        incr len_lines
        set lines_config_ini($len_lines) "\[color\]"
        incr len_lines
        set lines_config_ini($len_lines) "earth_orbit_color   = 0.0,0.5,1.0"
        incr len_lines
        set lines_config_ini($len_lines) "jupiter_orbit_color = 0.3,0.2,0.0"
        incr len_lines
        set lines_config_ini($len_lines) "mars_orbit_color    = 0.4,0.1,0.1"
        incr len_lines
        set lines_config_ini($len_lines) "mercury_orbit_color = 0.3,0.7,1.0"
        incr len_lines
        set lines_config_ini($len_lines) "moon_orbits_color   = 0.5,0.5,0.5"
        incr len_lines
        set lines_config_ini($len_lines) "neptune_orbit_color = 0.0,0.0,0.8"
        incr len_lines
        set lines_config_ini($len_lines) "saturn_orbit_color  = 0.3,0.3,0.2"
        incr len_lines
        set lines_config_ini($len_lines) "uranus_orbit_color  = 0.0,0.1,0.7"
        incr len_lines
        set lines_config_ini($len_lines) "venus_orbit_color   = 1.0,0.7,0.3"
        incr len_lines
      }

      set lines_config_ini($len_lines) ""
      incr len_lines
      set lines_config_ini($len_lines) "\[localization\]"
      incr len_lines

      if { $flag_save_app_language_value == "true" } {
        set lines_config_ini($len_lines) "app_locale = $app_language_value"
        incr len_lines
      }

      set lines_config_ini($len_lines) "sky_culture = $sky_culture_value"
      incr len_lines

      if { $flag_save_app_language_value == "true" } {
        set lines_config_ini($len_lines) "sky_locale = $app_language_value"
        incr len_lines
      }

      if { [catch {open $fullpath_of_config_ini w} fid_w] } {
        puts stderr "//"
        puts stderr "// ERROR: Unable to open $fullpath_of_config_ini for writing"
        puts stderr "//"
        exit -1
      } else {
        for {set i 0} {$i < $len_lines} {incr i} {
          puts $fid_w $lines_config_ini($i)
        }
        close $fid_w
      }

      puts "Info: updated $fullpath_of_config_ini"

    } else {

      puts "Info: $fullpath_of_config_ini is OK."

    }

  }

  puts "Info: stellarium_version = $stellarium_version"
  puts "Info: stellarium_major_version = $stellarium_major_version"

  if { [catch {open $fullpath_of_stellarium_version w} fid_w] } {
    puts stderr "//"
    puts stderr "// ERROR: Unable to open $fullpath_of_stellarium_version for writing"
    puts stderr "//"
    exit -1
  } else {
    puts $fid_w $stellarium_version
    close $fid_w
  }

  puts "Info: updated $fullpath_of_stellarium_version"

}

