require 'sinatra'
require 'sinatra/contrib'
require 'pry'
require 'yaml'
require 'haml'

# Note if you start via rackup this will be ignored
set :port, 9952

# Bind to 0.0.0.0 even in development mode for access from VM
set :bind, '0.0.0.0'

# Set :root explicitly, so that is is not generated dynamically.
# The problem with dynamic generation is that Forge.generate_certificates
# calls Dir.chdir, and then sinatra doesn't know where to look for
# views and stylesheets
set :root, settings.root

unless [:production, :development, :test].include? settings.environment
  # :development is default if nothing specified for RACK_ENV on command line
  raise ArgumentError, 'Unsupported environment'
end

require 'json'
require './helpers/view_helper'

class FreeCinc < Sinatra::Base

  CONFIG_FILE = File.expand_path('config/location.yml', File.dirname(__FILE__))
  RESTART_COMMAND = "pkill -f '/usr/bin/taskd server'"
  RESTART_SECRET = YAML.load_file(CONFIG_FILE)['restart_secret']

  configure :production, :development do
    enable :logging
  end
end

# include ViewHelper here so that it is both accessible in view
# and accessible in this file
include ViewHelper


# Require all your models manually here
 require './models/user'
 require './models/forge'


# /
get root_path do
  ensure_correct_environment
  haml :index
end

# /generated_keys
get generate_path do
  locals = {user: User.new}
  haml :generate, locals: locals
end

# /about
get about_path do
  haml :about
end

# /terms
get terms_path do
  haml :terms
end

post '/download/:filename' do |filename|
  content_type 'application/octet-stream'

  user_name, method_name, pem = *filename.split('.')

  raise ArgumentError, "Unknown file '#{filename}'" unless ['key', 'cert', 'ca', 'mirakel_config'].include? method_name
  uuid_for_mirakel = params[:uuid_for_mirakel] || fake_uuid
  copy_forge = CopyForge.new(user_name, params[:token], uuid_for_mirakel, server_with_port)
  copy_forge.read_user_certificates.send(method_name)
end

get '/restart/:secret' do |secret|
  if secret == FreeCinc::RESTART_SECRET
    `#{FreeCinc::RESTART_COMMAND}`
    return 'Process restarted. Will be online in 10 seconds'
  else
    return 'invalid secret'
  end
end


private
def ensure_correct_environment
  # Make sure RACK_ENV has been set to production if you are serving freecinc.com proper,
  # since that's the only way Sinatra knows to hide its stack traces
  if freecinc_proper? && !settings.production?
    raise ArgumentError, "Environment mismatch. If you are serving up freecinc.com proper, set RACK_ENV=production before starting server"
  end
end

