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

####################################################################
##                                                                ##
##     S Y S T E M - D E P E N D E N T    P R O C E D U R E S     ##
##                                                                ##
####################################################################

set TCLSRC_ENCODING utf-8
#set TCLSRC_ENCODING cp932

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

#
# URL of Stellarium Service
#
set stellarium_url "http://localhost:8090"
set iscserv_url    "http://localhost:8091"

#
# Get OS name
#
proc get_os_name {} {

  set os_name ""
  if { [info exists ::android] } {
    set os_name "Android"
  } elseif { [info exists ::env(OS)] } {
    set os_name $::env(OS)
  }

  if { $os_name eq "" } {
    # Check Android
    #set str_uname_r [exec -ignorestderr uname -r]
    # split a string like "6.6.30-android15-6-xxxxxxxxxxxxx"
    #set elms [split $str_uname_r "-"]
    #set len_elms [llength $elms]
    #for {set i 0} {$i < $len_elms} {incr i} {
    #  set el [string tolower [lindex $elms $i]]
    #  if { [string range $el 0 6] eq "android" } {
    #    set os_name "Android"
    #    break
    #  }
    #}
    # Linux, Darwin, etc.
    if { $os_name eq "" } {
      set os_name [exec -ignorestderr uname]
    }
  }
  if { $os_name eq "" } {
    set os_name "Unknown OS"
  }

  return $os_name

}

#
# Get Stellarium executable file
#
proc get_stellarium_executable_file {} {

  set path_ret "C:/Program Files/Stellarium/stellarium.exe"

  return $path_ret

}

#
# Get Stellarium output directory name
#
proc get_stellarium_io_directory {} {

  set os_name [get_os_name]
  if { [string range $os_name 0 6] eq "Windows" } {
    set path_raw $::env(HOMEDRIVE)
    append path_raw $::env(HOMEPATH)
    append path_raw "/AppData/Roaming/Stellarium"
  } else {
    set path_raw ""
  }

  set path_ret [regsub -all {\\} $path_raw {/}]

  return $path_ret

}

#
# Get Stellarium ISC script directory name
#
proc get_stellarium_isc_script_directory_name {} {

  set ret_string "interstellar-console"

  if { [catch {open "localConfig.ssc" r} fid] } {
    # ERROR: returns ""
    set ret_string ""
  } else {
    while { [gets $fid line] >= 0 } {
      set status [regexp -- {^[ ]*const[ ]+([A-Za-z0-9_]+)[ ]*[=][ ]*["]([^"]+)["]} $line match s0 s1]
      if { $status == 1 && $s0 eq "stellarium_script_dir" } {
        set ret_string $s1
        break
      }
      set status [regexp -- {^[ ]*var[ ]+([A-Za-z0-9_]+)[ ]*[=][ ]*["]([^"]+)["]} $line match s0 s1]
      if { $status == 1 && $s0 eq "stellarium_script_dir" } {
        set ret_string $s1
        break
      }
    }
    close $fid
  }

  return $ret_string

}

