desc "Build the hook
    DEBUG - build plugins in debug mode - default: false"
task :build do
    flags = []
    if ENV["DEBUG"] == "true"
        flags.append "-gcflags", "all=-N -l"
    end

    # Prepend the common prefix for all server hooks. The motivations for this
    # were discussed in https://gitlab.isc.org/isc-projects/stork-hook-ldap/-/merge_requests/1#note_403065 .
    hook_name = "stork-server-ldap.so"
    
    build_dir = "build"
    sh "mkdir", "-p", build_dir
    sh "rm", "-f", *FileList[File.join(build_dir, "*.so")]

    output_path = File.expand_path File.join(build_dir, hook_name)

    Dir.chdir "src" do
        sh "go", "mod", "tidy"
        sh "go", "build", *flags, "-buildmode=plugin", "-o", output_path
    end

    size = File.size output_path
    size /= 1024.0 * 1024.0
    puts "Hook: '#{output_path}' size: #{'%.2f' % size} MiB"
end

desc "Lint the hook"
task :lint do
    Dir.chdir "src" do
        sh "go", "vet", "./..."
    end
end

desc "Run hook unit tests"
task :unittest do
    Dir.chdir "src" do
        sh "go", "test", "-race", "-v", "./..." 
    end
end

desc "Format code"
task :fmt do
    Dir.chdir "src" do
        sh "go", "fmt", "./..."
    end
end
