#!/usr/bin/env ruby
# frozen_string_literal: true

# Supported languages:
# ar,cy,zh-Hans,zh-Hant,nl,fa,fr,de,el,he,id,ko,pt,ru,es,sv,tr,ja,it
# * Arabic
# * Welsh
# * Chinese (China) [zh-Hans]
# * Chinese (Taiwan) [zh-Hant]
# * Dutch
# * French
# * Greek
# * German
# * Hebrew
# * Indonesian
# * Korean
# * Portuguese (Brazil)
# * Russian
# * Spanish
# * Swedish
# * Turkish
# * Japanese
# * Italian
# * Farsi
require 'json'

if Dir.pwd =~ /Scripts/
  puts 'Must run script from root folder'
  exit
end

ALL_LANGS = {
  'ar' => 'ar',         # Arabic
  'cy' => 'cy',         # Welsh
  'de' => 'de',         # German
  'el' => 'el',         # Greek
  'es' => 'es',         # Spanish
  'fa' => 'fa',         # Farsi
  'fr' => 'fr',         # French
  'he' => 'he',         # Hebrew
  'id' => 'id',         # Indonesian
  'it' => 'it',         # Italian
  'ja' => 'ja',         # Japanese
  'ko' => 'ko',         # Korean
  'nl' => 'nl',         # Dutch
  'pt-br' => 'pt-BR',   # Portuguese (Brazil)
  'ru' => 'ru',         # Russian
  'sv' => 'sv',         # Swedish
  'tr' => 'tr',         # Turkish
  'zh-cn' => 'zh-Hans-CN', # Chinese (China)
  'zh-tw' => 'zh-Hant-TW' # Chinese (Taiwan)
}.freeze

# Whenever a translation is missing in any given language, we'll fall back to the English String
FALLBACK_LANG = 'en'

def copy_header(target_file, trans_strings)
  trans_strings.each_line do |line|
    unless line.start_with?('/*')
      target_file.write("\n")
      break
    end

    target_file.write(line)
  end
end

def copy_comment(file, trans_strings, value)
  prev_line = ''
  trans_strings.each_line do |line|
    if line.include?(value)
      file.write(prev_line)
      break
    end
    prev_line = line
  end
end

langs = {}
if ARGV.count.positive?
  ARGV.each do |key|
    unless (locale = ALL_LANGS[key])
      puts "Unknown language #{key}"
      exit 1
    end
    langs[key] = locale
  end
else
  langs = ALL_LANGS
end

fallback_lang_dir = File.join('Simplenote', "#{FALLBACK_LANG}.lproj")

langs.each do |code, locale|
  lang_dir = File.join('Simplenote', "#{locale}.lproj")
  puts "Updating #{code}"
  system "mkdir -p #{lang_dir}"

  # Backup the current file(s)
  system "if [ -e #{lang_dir}/Localizable.strings ]; then cp #{lang_dir}/Localizable.strings #{lang_dir}/Localizable.strings.bak; fi"
  system "if [ -e #{lang_dir}/MainMenu.strings ]; then cp #{lang_dir}/MainMenu.strings #{lang_dir}/MainMenu.strings.bak; fi"

  # Download: Localizable.strings
  system "curl -fLso #{lang_dir}/Localizable.strings https://translate.wordpress.com/projects/simplenote%2Fmacos/#{code}/default/export-translations?format=strings" or begin
    puts "Error downloading Localizable.strings - Language: [#{code}]"
  end

  # Download: MainMenu.strings
  system "curl -fLso #{lang_dir}/MainMenu.strings https://translate.wordpress.com/projects/simplenote%2Fmacos/main-menu/#{code}/default/export-translations?format=strings" or begin
    puts "Error downloading MainMenu.strings - Language: [#{code}]"
  end

  # Failsafe: Handle empty values
  system "./Scripts/fix-translation.swift #{lang_dir}/Localizable.strings #{fallback_lang_dir}/Localizable.strings"
  system "./Scripts/fix-translation.swift #{lang_dir}/MainMenu.strings #{fallback_lang_dir}/MainMenu.strings"

  # Make sure the new Strings are not corrupt!
  system "plutil -lint #{lang_dir}/Localizable.strings" and system "rm #{lang_dir}/Localizable.strings.bak"
  system "plutil -lint #{lang_dir}/MainMenu.strings" and system "rm #{lang_dir}/MainMenu.strings.bak"

  system "grep -a '\\x00\\x20\\x00\\x22\\x00\\x22\\x00\\x3b$' #{lang_dir}/Localizable.strings"
  system "grep -a '\\x00\\x20\\x00\\x22\\x00\\x22\\x00\\x3b$' #{lang_dir}/MainMenu.strings"
end
