#!/usr/bin/env ruby
require "bundler/inline"
require "yaml"

gemfile do
  source "https://rubygems.org"
  gem "base64"
  gem "bcrypt_pbkdf"
  gem "ed25519"
  gem "sshkit"
end

include SSHKit::DSL

ARCH = "linux/amd64,linux/arm64"
BUILDER_NAME = "campfire-builder"
GIT_SHA = `git rev-parse HEAD`.strip
VERSION = ARGV[0]

DOCKER_IMAGE_NAME = "registry.once.com/campfire"
DOCKER_LATEST_IMAGE_NAME = [ DOCKER_IMAGE_NAME, :latest ].join(":")
DOCKER_TAGGED_IMAGE_NAME = [ DOCKER_IMAGE_NAME, GIT_SHA ].join(":")

EXPORT_IMAGE_NAME = "campfire-export"
EXPORT_DOCKERFILE = "Dockerfile-export"
EXPORT_TARGET_NAME = "campfire.zip"
EXPORT_REMOTE_USER = "app"
EXPORT_DEPLOY_HOSTS = %w[ once-store-app-101 once-store-app-102 ]
EXPORT_DEPLOY_LOCATION = "/local/app/product_releases/"

abort "You must specify a version, e.g. `bin/release 1.0.0`" unless VERSION

def announcing(message)
  puts "\n\e[1;36m#{message}\e[0m"
  yield
end

on(:local) do
  builder_found = test :docker, :buildx, :inspect, BUILDER_NAME

  unless builder_found
    announcing "Setting up builder..." do
      execute :docker, :buildx, :create, "--name", BUILDER_NAME
    end
  end
end

announcing "Tagging as #{VERSION}" do
  on(:local) do
    execute :git, :fetch, :origin, "--tags"

    unless system "git tag -a v#{VERSION} -m 'Version #{VERSION}\n\n- list important changes here' -e"
      abort "Failed to create tag; check that the version doesn't already exist"
    end

    unless test :git, :push, "--tags"
      abort "Failed to push tag; check that the version doesn't already exist"
    end
  end
end

announcing "Creating GitHub release..." do
  on(:local) do
    execute :gh, :release, :create, "v#{VERSION}", "--notes-from-tag"
  end
end

announcing "Building and pushing image..." do
  versioning = "--build-arg APP_VERSION=#{VERSION} --build-arg GIT_REVISION=#{GIT_SHA}"

  on(:local) do
    execute :docker, :buildx, :build, "--builder", BUILDER_NAME, "--push", versioning, "-t", DOCKER_LATEST_IMAGE_NAME, "-t", DOCKER_TAGGED_IMAGE_NAME, "--platform", ARCH, "."
  end
end

SSHKit::Backend::Netssh.configure do |ssh|
  ssh.connection_timeout = 30
  ssh.ssh_options = { user: EXPORT_REMOTE_USER }
end

announcing "Building source code ZIP image..." do
  on(:local) do
    execute :docker, :build, "-t", EXPORT_IMAGE_NAME, "-f", EXPORT_DOCKERFILE, "."
  end
end

announcing "Exporting source code to ZIP..." do
  on(:local) do
    execute :docker, :run, "--name", EXPORT_IMAGE_NAME, EXPORT_IMAGE_NAME
    execute :docker, :cp, "#{EXPORT_IMAGE_NAME}:/#{EXPORT_TARGET_NAME}", "tmp/#{EXPORT_TARGET_NAME}"
    execute :docker, :rm, EXPORT_IMAGE_NAME
  end
end

announcing "Uploading source code ZIP..." do
  on(EXPORT_DEPLOY_HOSTS) do
    within EXPORT_DEPLOY_LOCATION do
      upload! "tmp/#{EXPORT_TARGET_NAME}", EXPORT_TARGET_NAME
    end
  end
end
