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

# Stellarium バージョン

$stellarium_version = "24.4"

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

# バージョン入りファイル名

$milkyway_orig_filename = "milkyway.orig-v25.4.png"

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

Set-StrictMode -version Latest

Add-Type -AssemblyName System.Windows.Forms

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

Write-Host "//"
Write-Host "// INTERSTELLAR CONSOLE  Installer (ps1)"
Write-Host "// Copyright (C) 2025- Misato Observatory, Wakayama, Japan"
Write-Host "//"
Write-Host ""

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

$lang = $args[0]

Write-Host "LANG = $lang"

#Start-Sleep -Seconds 3
#exit 1

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

try {
  cd "interstellar-console" -ErrorAction Stop
} catch {
  if ( $lang -eq "ja" ) {
    $text = "ネットワークドライブからのインストールはサポートされていません。パッケージすべてをローカルドライブやUSBドライブへコピーし、そこでsetup.exeを実行してください"
  } else {
    $text = "Installation from a network drive is not supported. Please copy all packages to a local drive or USB drive and run setup.exe from there."
  }
  $caption = "ERROR"
  $buttonsType = "OK"
  $iconType = "Error"
  $result = [System.Windows.Forms.MessageBox]::Show($text, $caption, $buttonsType, $iconType)
  exit 1
}

$currentDirectory = Get-Location
Write-Host "PACKAGE: $currentDirectory"

$STELDIR     = "C:\Program Files\Stellarium"
$TEXTURESDIR = "$STELDIR\textures"
$SKYCULDIR   = "$STELDIR\skycultures"
$QMSKYCULDIR = "$STELDIR\translations\stellarium-skycultures"
$SSCDIR      = "$STELDIR\scripts"
$LANDSCDIR   = "$STELDIR\landscapes"

$flagNotFound = 0
if ( !(Test-Path -Path "$STELDIR" -PathType Container) ) {
  $flagNotFound = 1
}
if ( !(Test-Path -Path "$TEXTURESDIR" -PathType Container) ) {
  $flagNotFound = 1
}
if ( !(Test-Path -Path "$SKYCULDIR" -PathType Container) ) {
  $flagNotFound = 1
}
if ( !(Test-Path -Path "$QMSKYCULDIR" -PathType Container) ) {
  $flagNotFound = 1
}
if ( !(Test-Path -Path "$SSCDIR" -PathType Container) ) {
  $flagNotFound = 1
}
if ( !(Test-Path -Path "$LANDSCDIR" -PathType Container) ) {
  $flagNotFound = 1
}

if ( $flagNotFound -ne 0 ) {
  if ( $lang -eq "ja" ) {
    $text = "Stellariumが正常にインストールされていません。セットアップを中止します"
  } else {
    $text = "Stellarium was not installed correctly. Setup will be aborted."
  }
  $caption = "ERROR"
  $buttonsType = "OK"
  $iconType = "Error"
  $result = [System.Windows.Forms.MessageBox]::Show($text, $caption, $buttonsType, $iconType)
  exit 1
}

$DESTDIR = "$SSCDIR\interstellar-console"
Write-Host "DESTDIR: $DESTDIR"

if ( Test-Path -Path "$DESTDIR" ) {
  if ( !(Test-Path -Path "$DESTDIR" -PathType Container) ) {
    Write-Host "/////////////////////////////////////////////////////////////////////"
    Write-Host "// ERROR: An unexpected error has occurred. Setup will be aborted. //"
    Write-Host "/////////////////////////////////////////////////////////////////////"
    Start-Sleep -Seconds 5
    exit 1
  }
} else {
  try {
    New-Item -Path "$DESTDIR" -ItemType Directory -ErrorAction Stop > $null
  } catch {
    if ( $lang -eq "ja" ) {
      $text = "ディレクトリの作成に失敗しました。セットアップを中断します"
    } else {
      $text = "Failed to create directory. Setup will be aborted."
    }
    $caption = "ERROR"
    $buttonsType = "OK"
    $iconType = "Error"
    $result = [System.Windows.Forms.MessageBox]::Show($text, $caption, $buttonsType, $iconType)
    exit 1
  }
}

if ( !(Test-Path -Path "$DESTDIR\images") ) {
  try {
    New-Item -Path "$DESTDIR\images" -ItemType Directory -ErrorAction Stop > $null
  } catch {
    Write-Host "///////////////////////////////////////////////////////////////"
    Write-Host "// ERROR: Failed to create directory. Setup will be aborted. //"
    Write-Host "///////////////////////////////////////////////////////////////"
    Start-Sleep -Seconds 5
    exit 1
  }
}

Write-Host "Permission 1 is currently set..."

#cacls "$DESTDIR" /E /G Users:F
icacls "$DESTDIR" /grant "Users:(OI)(CI)F" /T /Q

Write-Host "Permission 2 is currently set..."

#cacls "$TEXTURESDIR" /E /G Users:F
icacls "$TEXTURESDIR" /grant "Users:(OI)(CI)F" /T /Q

Write-Host "Permission 3 is currently set..."

#cacls "$LANDSCDIR" /E /G Users:F
icacls "$LANDSCDIR" /grant "Users:(OI)(CI)F" /T /Q

Start-Sleep -Seconds 1


if ( $lang -eq "ja" ) {
  Write-Host "ソフトウェア本体をコピー中..."
} else {
  Write-Host "Copying the software..."
}

if ( Test-Path -Path "..\VERSION.txt" ) {
  Copy-Item "..\VERSION.txt" "$DESTDIR\VERSION.txt" -Force
}
xcopy "*.exe" "$DESTDIR\." /Q /Y /R
Copy-Item "nircmd-x64\nircmd.exe" "$DESTDIR\nircmd.exe" -Force
Copy-Item "nkfwin\nkf.exe" "$DESTDIR\nkf.exe" -Force
xcopy "*.tcl" "$DESTDIR\." /Q /Y /R
xcopy "splash*.gif" "$DESTDIR\." /Q /Y /R
xcopy "*.inc" "$DESTDIR\." /Q /Y /R
xcopy "*.ssc" "$DESTDIR\." /Q /Y /R
xcopy "*.ps1" "$DESTDIR\." /Q /Y /R
xcopy "*.sh" "$DESTDIR\." /Q /Y /R
xcopy "*.txt" "$DESTDIR\." /Q /Y /R
xcopy "images\*.*" "$DESTDIR\images\." /Q /Y /R
xcopy "media\*" "$DESTDIR\media\." /Q /Y /R /I /E
xcopy "examples\*" "$DESTDIR\examples\." /Q /Y /R /I /E
xcopy "milkyway\*" "$DESTDIR\milkyway\." /Q /Y /R /I /E
xcopy "landscapes\*" "$LANDSCDIR\." /Q /Y /R /I /E

if ( $lang -eq "ja" ) {
  Copy-Item "$DESTDIR\localInitLocation.ja_JP.ssc" "$DESTDIR\localInitLocation.ssc"
  Set-Content -Path "$DESTDIR\isc-lang.txt" -Value "ja" -Encoding ascii
} else {
  Copy-Item "$DESTDIR\localInitLocation.en_US.ssc" "$DESTDIR\localInitLocation.ssc"
  Set-Content -Path "$DESTDIR\isc-lang.txt" -Value "en" -Encoding ascii
}


if ( $lang -eq "ja" ) {
  Write-Host "日本語訳データをコピー中..."
} else {
  Write-Host "Copying Japanese translation data..."
}

if ( Test-Path -Path "$QMSKYCULDIR\ja.qm" ) {
  if ( !(Test-Path -Path "$QMSKYCULDIR\ja.qm.old") ) {
    Rename-Item "$QMSKYCULDIR\ja.qm" "ja.qm.old"
  }
}
Copy-Item "24.4\translations\ja.qm" "$QMSKYCULDIR\ja.qm" -Force


if ( $lang -eq "ja" ) {
  Write-Host "プラネタリウムライブ上映に最適化された skycultures 関連データをコピー中..."
} else {
  Write-Host "Copying skycultures-related data optimized for live planetarium presentations..."
}

if ( !(Test-Path -Path "$SKYCULDIR\japanese_default-art") ) {
  try {
    New-Item -Path "$SKYCULDIR\japanese_default-art" -ItemType Directory -ErrorAction Stop > $null
  } catch {
    Write-Host "///////////////////////////////////////////////////////////////"
    Write-Host "// ERROR: Failed to create directory. Setup will be aborted. //"
    Write-Host "///////////////////////////////////////////////////////////////"
    Start-Sleep -Seconds 5
    exit 1
  }
}

xcopy "24.4\japanese_default-art\*" "$SKYCULDIR\japanese_default-art\." /Q /Y /R /I /E
xcopy "$SKYCULDIR\modern\*.png" "$SKYCULDIR\japanese_default-art\." /Q /Y /R /I /E

if ( !(Test-Path -Path "$SKYCULDIR\english_default-art") ) {
  try {
    New-Item -Path "$SKYCULDIR\english_default-art" -ItemType Directory -ErrorAction Stop > $null
  } catch {
    Write-Host "///////////////////////////////////////////////////////////////"
    Write-Host "// ERROR: Failed to create directory. Setup will be aborted. //"
    Write-Host "///////////////////////////////////////////////////////////////"
    Start-Sleep -Seconds 5
    exit 1
  }
}

xcopy "24.4\english_default-art\*" "$SKYCULDIR\english_default-art\." /Q /Y /R /I /E
xcopy "$SKYCULDIR\modern\*.png" "$SKYCULDIR\english_default-art\." /Q /Y /R /I /E


if ( $lang -eq "ja" ) {
  Write-Host "オリジナル星座絵入り skycultures 関連ファイルをコピー中..."
} else {
  Write-Host "Copying skycultures-related files with original constellation illustrations..."
}

if ( !(Test-Path -Path "$SKYCULDIR\japanese_misato-art") ) {
  try {
    New-Item -Path "$SKYCULDIR\japanese_misato-art" -ItemType Directory -ErrorAction Stop > $null
  } catch {
    Write-Host "///////////////////////////////////////////////////////////////"
    Write-Host "// ERROR: Failed to create directory. Setup will be aborted. //"
    Write-Host "///////////////////////////////////////////////////////////////"
    Start-Sleep -Seconds 5
    exit 1
  }
}

xcopy "24.4\japanese_default-art\*" "$SKYCULDIR\japanese_misato-art\." /Q /Y /R /I /E
Copy-Item "..\art\info.ja.ini" "$SKYCULDIR\japanese_misato-art\info.ini" -Force
Copy-Item "..\art\description.ja.utf8" "$SKYCULDIR\japanese_misato-art\description.ja.utf8" -Force
xcopy "..\art\*.png" "$SKYCULDIR\japanese_misato-art\." /Q /Y /R

if ( !(Test-Path -Path "$SKYCULDIR\english_misato-art") ) {
  try {
    New-Item -Path "$SKYCULDIR\english_misato-art" -ItemType Directory -ErrorAction Stop > $null
  } catch {
    Write-Host "///////////////////////////////////////////////////////////////"
    Write-Host "// ERROR: Failed to create directory. Setup will be aborted. //"
    Write-Host "///////////////////////////////////////////////////////////////"
    Start-Sleep -Seconds 5
    exit 1
  }
}

xcopy "24.4\english_default-art\*" "$SKYCULDIR\english_misato-art\." /Q /Y /R /I /E
Copy-Item "..\art\info.en.ini" "$SKYCULDIR\english_misato-art\info.ini" -Force
Copy-Item "..\art\description.en.utf8" "$SKYCULDIR\english_misato-art\description.en.utf8" -Force
xcopy "..\art\*.png" "$SKYCULDIR\english_misato-art\." /Q /Y /R
Copy-Item "$SKYCULDIR\english_misato-art\aquila.rotated.png" "$SKYCULDIR\english_misato-art\aquila.png" -Force


# 天の川のテクスチャーをコピー

$source_yes = "$DESTDIR\milkyway\milkyway.0.90.png"
$source_no  = "$DESTDIR\milkyway\$milkyway_orig_filename"
$dest_milkyway = "$TEXTURESDIR\milkyway.png"

if ( $lang -eq "ja" ) {
  $text = "全天周スクリーンへの投影やプレゼンテーション用に調整された`r`n高品位な天の川のテクスチャーをインストールしますか？`r`n何らかの理由でStellariumオリジナルのテクスチャーを利用する`r`n場合以外は、Yesを選択してください。`r`n天の川のテクスチャーは後でも変更できます。"
} else {
  $text = "Do you want to install high-quality Milky Way texture optimized for full-dome projection or presentations?`r`nSelect 'Yes' unless you have a specific reason to use the original Stellarium texture.`r`nYou can change the Milky Way texture later."
}
$caption = "Setup: Interstellar Console"
$buttonsType = "YesNo"
$iconType = "Information"
$result = [System.Windows.Forms.MessageBox]::Show($text, $caption, $buttonsType, $iconType)

if ( $result -eq "No" ) {
  Copy-Item "$source_no" "$dest_milkyway" -Force
} else {
  Copy-Item "$source_yes" "$dest_milkyway" -Force
}


#if ( $lang -eq "ja" ) {
#
#  Write-Host "Tcl/Tk-9用ソースファイルを作成中..."
#
#  if ( !(Test-Path -Path "$DESTDIR\tcl_utf8") ) {
#    try {
#      New-Item -Path "$DESTDIR\tcl_utf8" -ItemType Directory -ErrorAction Stop > $null
#    } catch {
#      Write-Host "///////////////////////////////////////////////////////////////"
#      Write-Host "// ERROR: Failed to create directory. Setup will be aborted. //"
#      Write-Host "///////////////////////////////////////////////////////////////"
#      Start-Sleep -Seconds 5
#      exit 1
#    }
#  }
#
#  foreach ($i in Get-ChildItem *.tcl) {
#    $f = $i.Name
#    .\nkfwin\nkf.exe -O -S -w $f "$DESTDIR\tcl_utf8\$f"
#  }
#
#} else {
#
#  Write-Host "Currently creating source files for English environment..."
#
#  foreach ($i in Get-ChildItem *.tcl) {
#    $f = $i.Name
#    .\nkfwin\nkf.exe -O -S -w $f "$DESTDIR\$f"
#  }
#
#}


# デスクトップにショートカットを作成

. ".\create_isc_shortcuts.ps1"


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

if ( $lang -eq "ja" ) {
  Write-Host "セットアップが完了しました！"
  $text = "セットアップが完了しました！`r`n`r`n【お願い】`r`nわからない事や不具合があれば、遠慮せずに みさと天文台へ質問をお願いします。質問は、ソフトウェアの改良に大いに役立ちます。"
} else {
  Write-Host "Setup complete!"
  $text = "Setup complete! `r`n`r`n [ To Our Users ] `r`nIf you have any questions or encounter any problems, please do not hesitate to contact Misato Observatory. Your questions will greatly help us improve the software."
}

$caption = "Finished Setup"
$buttonsType = "OK"
$iconType = "Information"

$result = [System.Windows.Forms.MessageBox]::Show($text, $caption, $buttonsType, $iconType)

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

if ( $lang -eq "ja" ) {
. ".\$stellarium_version\setup\last.ja.ps1"
} else {
. ".\$stellarium_version\setup\last.en.ps1"
}

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

Start-Sleep -Seconds 1

