class SymControlController < BaseController
  skip_before_action :verify_authenticity_token

  def run
  	sym_path = "/home/yan/symmetric/"
  	check_sym_proc_str = `#{"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(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(sym_path + "bin/dbimport --engine sx " + sym_sql.to_s)
            stop_sym = system("service SymmetricDS stop")
            run_sym = system("service SymmetricDS start")
            if run_sym
              render :text => "<h1>Success!</h1>"
            end
          else
            render :text => "<h1>Sym Sql file not found!</h1>"
          end
        end
      else      
        render :text => "<h1>Sym not running!</h1>"
      end
  end

end