$:.push File.expand_path("../lib", __FILE__)

# Maintain your gem's version:
require "archive/version"
require 'json'
env_pwd = ENV['PWD']
app_path = File.expand_path(__dir__)
template_path = env_pwd + '/app/templates'
all_template = Dir.glob(template_path+'/*/')
all_template.each do |folder|
  if !folder.include?('mobile')
    begin
      next unless File.exist?("#{folder}modules/archive/info.json")
      info_json_file = "#{folder}modules/archive/info.json"
      info_json = JSON.parse(File.read(info_json_file))
      check_files = ["archive_index7.html.erb","archive_index8.html.erb","archive_index9.html.erb","archive_index10.html.erb","archive_index11.html.erb"]
      old_file_infos = [
        {
            "filename" => "archive_index7",
            "name" => {
                "zh_tw" => "7. 表格列表 ( 模組標題, 類別標題, 檔案名稱, 檔案簡介, 下載連結 )",
                "en" => "7. Table list (widget-title, category, filename, download link)"
                },
            "thumbnail" => "ar5.png"
        },
        {
            "filename" => "archive_index8",
            "name" => {
                "zh_tw" => "8. 表格列表 ( 模組標題, 檔案名稱, 檔案簡介, 下載連結 )",
                "en" => "8. Table list (widget-title, filename, download link)"
                },
            "thumbnail" => "ar5.png"
        }
      ]
      new_file_infos = [
        {
            "filename" => "archive_index7",
            "name" => {
                "zh_tw" => "7. 表格列表 ( 模組標題, 類別標題, 上傳日期, 檔案名稱, 檔案簡介, 下載連結 )",
                "en" => "7. Table list (widget-title, category, Updated At, filename, download link)"
                },
            "thumbnail" => "ar5.png"
        },
        {
            "filename" => "archive_index8",
            "name" => {
                "zh_tw" => "8. 表格列表 ( 模組標題, 上傳日期, 檔案名稱, 檔案簡介, 下載連結 )",
                "en" => "8. Table list (widget-title, Updated At, filename, download link)"
                },
            "thumbnail" => "ar5.png"
        },
        {
            "filename" => "archive_index9",
            "name" => {
                "zh_tw" => "9. 表格列表 - 簡約 ( 模組標題, 檔案名稱, 下載連結 )",
                "en" => "9. Table list - Simple (widget-title, download link)"
                },
            "thumbnail" => "ar9.png"
        },
        {
            "filename" => "archive_index10",
            "name" => {
                "zh_tw" => "10. 表格列表 ( 模組標題, 類別標題, 編號,  檔案名稱, 下載連結 )",
                "en" => "10. Table list (widget-title, category, Serial Number, filename, download link)"
                },
            "thumbnail" => "ar5.png"
        },
        {
            "filename" => "archive_index11",
            "name" => {
                "zh_tw" => "11. 表格列表 - 簡約 - 展開檔案 ( 模組標題, 類別標題, 編號,  檔案名稱, 下載連結 )",
                "en" => "11. Table list - Simple - Unfold Files (widget-title, category, Serial Number, filename, download link)"
                },
            "thumbnail" => "ar9.png"
        }
      ]
      flags = []
      all_frontend_names = info_json["frontend"].map{|h| h["name"]}
      check_files.each_with_index do |check_file,i|
        flag = (info_json["frontend"].select{|h| h["filename"] == check_file.split(".").first}.count == 0 rescue false)
        if flag
          info_json["frontend"].push(new_file_infos[i])
        end
        if !flag
          old_file_info = old_file_infos[i]
          if old_file_info
            idx = all_frontend_names.index(old_file_info["name"])
            if idx && idx != -1
              info_json["frontend"][idx] = new_file_infos[i]
              flag = true
            end
          end
        end
        flags << flag
      end
      force_update_files = ["archive_index1.html.erb","archive_index3.html.erb"]
      puts "updating archive index page"
      if flags.select{|flag| flag }.count != 0
        flags.each_with_index do |flag,i|
          if flag
            Bundler.with_clean_env{%x[cp -f #{app_path}/modules/archive/#{check_files[i]} #{folder}modules/archive/#{check_files[i]}]}
          end
        end
        begin
          info_json = JSON.pretty_generate(info_json).gsub(":[",":[\n").gsub(":{",":{\n")
        rescue
          info_json = info_json.to_s.gsub("=>",": \n")
        end
        File.open(info_json_file,"w+"){|f| f.write(info_json)}
      end
      force_update_files.each do |update_file|
        Bundler.with_clean_env{%x[cp -f #{app_path}/modules/archive/#{update_file} #{folder}modules/archive/#{update_file}]}
      end
      Bundler.with_clean_env{%x[cp -rn #{app_path}/modules/archive/thumbs/* #{folder}modules/archive/thumbs/.]}
    rescue
      puts "There has some error when updating archive index page."
    end
  end
end
# Describe your gem and declare its dependencies:
Gem::Specification.new do |s|
  s.name        = "archive"
  s.version     = Archive::VERSION
  s.authors     = ["Ruling Digital"]
  s.email       = ["orbit@rulingcom.com"]
  s.homepage    = "http://www.rulingcom.com"
  s.summary     = "Archive module for Orbit."
  s.description = "Archive module for Orbit"
  s.license     = "MIT"

  s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
  s.test_files = Dir["test/**/*"]
end
