#
# Cookbook:: kosmos-bitcoin
# Recipe:: source
#

# TODO move to custom kosmos cookbook before publshing bitcoin cookbook
systemd_unit "mnt-data-bitcoin.mount" do
  content({
    Unit: {
      Description: 'Bitcoin Core data directory',
    },
    Mount: {
      What: '/var/lib/vmshare-bitcoin',
      Where: '/mnt/data/bitcoin',
      Type: '9p',
      Options: 'trans=virtio,version=9p2000.L'
    },
    Install: {
      WantedBy: 'multi-user.target'
    }
  })
  verify false
  triggers_reload true
  action [:create, :enable, :start]
end

build_essential
include_recipe 'ark'


%w{ libtool autotools-dev make automake cmake curl g++-multilib libtool
    binutils-gold bsdmainutils pkg-config python3 patch }.each do |pkg|
  apt_package pkg
end

ark 'bitcoind' do
  url "https://bitcoincore.org/bin/bitcoin-core-#{node['bitcoin']['version']}/bitcoin-#{node['bitcoin']['version']}.tar.gz"
  checksum node['bitcoin']['checksum']
  action :put
  notifies :run, 'execute[compile_bitcoin-core_dependencies]', :immediately
end

execute "compile_bitcoin-core_dependencies" do
  cwd "/usr/local/bitcoind/depends"
  command "make NO_QT=1"
  action :nothing
  notifies :run, 'bash[compile_bitcoin-core]', :immediately
end

bash "compile_bitcoin-core" do
  cwd "/usr/local/bitcoind"
  code <<-EOH
    ./autogen.sh
    ./configure --prefix=$PWD/depends/x86_64-pc-linux-gnu
    make
  EOH
  action :nothing
  notifies :restart, "systemd_unit[bitcoind.service]", :delayed
end

link "/usr/local/bin/bitcoind" do
  to "/usr/local/bitcoind/src/bitcoind"
end

link "/usr/local/bin/bitcoin-cli" do
  to "/usr/local/bitcoind/src/bitcoin-cli"
end

bitcoin_user      = node['bitcoin']['username']
bitcoin_group     = node['bitcoin']['usergroup']
bitcoin_datadir   = node['bitcoin']['datadir']
bitcoin_walletdir = node['bitcoin']['walletdir']
bitcoin_conf_path = node['bitcoin']['conf_path']
credentials       = Chef::EncryptedDataBagItem.load('credentials', 'bitcoin')

group bitcoin_group

user bitcoin_user do
  manage_home true
  uid 1006
  gid bitcoin_group
  shell "/bin/bash"
end

[bitcoin_datadir, bitcoin_walletdir].each do |path|
  directory path do
    owner bitcoin_user
    group bitcoin_group
    mode '0750'
    recursive true
    action :create
  end
end

bitcoin_config = node['bitcoin']['conf'].merge({
  rpcpassword: credentials["rpcpassword"]
})

template bitcoin_conf_path do
  owner bitcoin_user
  group bitcoin_group
  mode '0640'
  variables conf: bitcoin_config,
            mainnet_conf: node['bitcoin']['mainnet_conf'],
            testnet_conf: node['bitcoin']['testnet_conf'],
            regtest_conf: node['bitcoin']['regtest_conf']
  action :create
  notifies :restart, "systemd_unit[bitcoind.service]", :delayed
end

systemd_unit 'bitcoind.service' do
  content({
    Unit: {
      Description: 'Bitcoin Core daemon',
      Documentation: ['https://bitcoincore.org'],
      After: 'network.target'
    },
    Service: {
      User: bitcoin_user,
      Type: 'simple',
      ExecStart: "bitcoind -conf=#{bitcoin_conf_path} -datadir=#{bitcoin_datadir} -walletdir=#{bitcoin_walletdir} -pid=#{bitcoin_datadir}/bitcoind.pid",
      PIDFile: "#{bitcoin_datadir}/bitcoind.pid",
      Restart: 'always',
      PrivateTmp: true,
      LimitNOFILE: 'infinity',
      TimeoutStopSec: '60s',
      TimeoutStartSec: '20s',
      StartLimitInterval: '60s',
      StartLimitBurst: '2'
    },
    Install: {
      WantedBy: 'multi-user.target'
    }
  })
  verify false
  triggers_reload true
  action [:create, :enable, :start]
end

include_recipe "kosmos-bitcoin::firewall"
