# SPDX-FileCopyrightText: 2020 Luke Granger-Brown <depot@lukegb.com>
#
# SPDX-License-Identifier: Apache-2.0

{ depot, ... }: {
  archiver = depot.third_party.buildGo.program {
    name = "archiver";
    srcs = [ ./archiver/archiver.go ];
    deps = [
      depot.third_party.gopkgs."github.com".dghubble.oauth1
      depot.third_party.gopkgs."github.com".jackc.pgx.v4
    ];
  };

  viewer = (depot.third_party.buildGo.program {
    name = "viewer";
    srcs = [ ./viewer/viewer.go ];
    deps = [
      depot.third_party.gopkgs."github.com".google.safehtml
      depot.third_party.gopkgs."github.com".google.safehtml.template
      depot.third_party.gopkgs."github.com".google.safehtml.uncheckedconversions
      depot.third_party.gopkgs."github.com".gorilla.mux
      depot.third_party.gopkgs."github.com".jackc.pgtype
      depot.third_party.gopkgs."github.com".jackc.pgx.v4.pgxpool
      depot.third_party.gopkgs."github.com".pomerium.sdk-go
      depot.third_party.gopkgs."gocloud.dev".blob
      depot.third_party.gopkgs."gocloud.dev".blob.s3blob
      depot.third_party.gopkgs."gocloud.dev".gcerrors
    ];
    dockerData = [ (
      depot.pkgs.runCommand "source" {} ''
        cp -R ${builtins.filterSource (path: type: (type == "directory" && depot.lib.hasSuffix "/templates" path) || (depot.lib.hasInfix "/templates/" path)) ./viewer} $out
      ''
    ) ];
  }).overrideAttrs (oldAttrs: {
    buildCommand = ''
      ${oldAttrs.buildCommand}
      mkdir $out/share
      cp -R ${builtins.filterSource (path: type: (type == "directory" && depot.lib.hasSuffix "/templates" path) || (depot.lib.hasInfix "/templates/" path)) ./viewer}/* $out/share
    '';
  });

  relatedfetcher = depot.third_party.buildGo.program {
    name = "relatedfetcher";
    srcs = [ ./related_fetcher/fetcher.go ];
    deps = [
      depot.third_party.gopkgs."github.com".dghubble.oauth1
      depot.third_party.gopkgs."github.com".jackc.pgtype
      depot.third_party.gopkgs."github.com".jackc.pgx.v4.pgxpool
      depot.third_party.gopkgs."gocloud.dev".blob
      depot.third_party.gopkgs."gocloud.dev".blob.s3blob
      depot.third_party.gopkgs."gocloud.dev".gcerrors
    ];
  };
}