class SymControlController < BaseController
  skip_before_action :verify_authenticity_token
  http_basic_authenticate_with name: "vip", password: "!abcABC01"

  def run
  	sym_path =  File.expand_path("~/symmetric/")
  	check_sym_proc_str = `#{"sudo service SymmetricDS status"}`
      # Check Sym Installed
      # if sym_install_status[1] == "false"
      #   response = { "status": false, "message": "Activation is success but Symmetric service not Installed. \n Please contact code2lab call center!"}
      # end

      if check_sym_proc_str.include? "Active: active (running)" || "Active: active (exited)"   #"Server is already running"
        # Create Sym Table
        check_sym_table = system("sudo " + sym_path + "/bin/symadmin --engine sx create-sym-tables")
        if check_sym_table
          sym_sql = Rails.root + "db/sym_master.sql"
          
          if File.exist? (sym_sql)
            # Import Sym Sql to db and start sym
            run_sym_sql = system("sudo " + sym_path + "/bin/dbimport --engine sx " + sym_sql.to_s)
            stop_sym = system("sudo service SymmetricDS stop")
            run_sym = system("sudo service SymmetricDS start")
            if run_sym
              flash[:notice] = 'Success!'
            end
          else
            flash[:notice] = 'Sym Sql file not found!'
          end
        end
      else      
        flash[:notice] = "Sym not running!"
      end
  end

  def get_key
    license = License.new(ENV["SX_PROVISION_URL"])
    status = license.get_key
    if status
      flash[:notice] = "Success!"
    else
      flash[:notice] = "Not Get!"
    end 
  end
end