#
# Cookbook Name:: kosmos-hubot
# Recipe:: hal8000
#
# Copyright:: 2019, Kosmos Developers
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

build_essential 'hal8000' do
  compile_time true
end

include_recipe "kosmos-nodejs"
include_recipe "kosmos-redis"
include_recipe "kosmos-hubot::_user"

unless node.chef_environment == "development"
  include_recipe 'firewall'
  firewall_rule 'hubot_express_hal8000_freenode' do
    port     node['hal8000']['http_port']
    protocol :tcp
    command  :allow
  end
end

hal8000_freenode_data_bag_item = Chef::EncryptedDataBagItem.load('credentials', 'hal8000_freenode')

hal8000_path = "/opt/hal8000"
application hal8000_path do
  owner "hubot"
  group "hubot"

  git do
    user       "hubot"
    group      "hubot"
    repository "https://github.com/67P/hal8000.git"
    revision   "master"
  end

  file "#{name}/external-scripts.json" do
    mode  "0640"
    owner "hubot"
    group "hubot"
    content [
      "hubot-help",
      "hubot-read-tweet",
      "hubot-redis-brain",
      "hubot-rules",
      "hubot-shipit",
      "hubot-plusplus",
      "hubot-tell",
      "hubot-seen",
      "hubot-incoming-webhook",
      "hubot-auth",
      "hubot-schedule"
    ].to_json
  end

  npm_install do
    user "hubot"
  end

  execute "systemctl daemon-reload" do
    command "systemctl daemon-reload"
    action :nothing
  end

  template "/lib/systemd/system/hal8000_nodejs.service" do
    source 'nodejs.systemd.service.erb'
    owner 'root'
    group 'root'
    mode '0644'
    variables(
      user: "hubot",
      group: "hubot",
      app_dir: hal8000_path,
      entry: "#{hal8000_path}/bin/hubot -a irc",
      environment: {
        # "HUBOT_LOG_LEVEL"             => "error",
        "HUBOT_IRC_SERVER"              => "irc.freenode.net",
        "HUBOT_IRC_ROOMS"               => "#5apps,#kosmos,#kosmos-dev,#kosmos-random,#remotestorage,#hackerbeach,#unhosted,#sockethub",
        "HUBOT_IRC_NICK"                => "hal8000",
        "HUBOT_IRC_NICKSERV_USERNAME"   => "hal8000",
        "HUBOT_IRC_NICKSERV_PASSWORD"   => hal8000_freenode_data_bag_item['nickserv_password'],
        "HUBOT_IRC_UNFLOOD"             => "100",
        "HUBOT_RSS_PRINTSUMMARY"        => "false",
        "HUBOT_RSS_PRINTERROR"          => "false",
        "HUBOT_RSS_IRCCOLORS"           => "true",
        "HUBOT_PLUSPLUS_POINTS_TERM"    => "karma,karma",
        "HUBOT_RSS_HEADER"              => "Update:",
        "HUBOT_AUTH_ADMIN"              => "bkero,derbumi,galfert,gregkare,slvrbckt,raucao",
        "HUBOT_HELP_REPLY_IN_PRIVATE"   => "true",
        "WEBHOOK_TOKEN"                 => hal8000_freenode_data_bag_item['webhook_token'],
        "EXPRESS_PORT"                  => node['hal8000']['http_port']
      }
    )
    notifies :run, "execute[systemctl daemon-reload]", :delayed
    notifies :restart, "service[hal8000_nodejs]", :delayed
  end

  service "hal8000_nodejs" do
    action [:enable, :start]
  end
end

