locals {
  persistent_folder = var.persistent_folder
  m3uproxy_version  = var.tag
  m3uproxy_fqdn     = var.fqdn
  wireguard_version = "1.0.20210914-r4-ls54"
  squid_version     = "6.1-23.10_edge"

  geoip_image         = "maxmindinc/geoipupdate"
  geoip_version       = "latest"
  geoip_cron_schedule = var.geoip_cron_schedule
  geoip_editions      = var.geoip_editions
  geoip_path          = "${local.m3uproxy_path}/geoip"

  m3uproxy_path       = "${local.persistent_folder}/m3uproxy"
  m3uproxy_cache_path = "${local.m3uproxy_path}/cache"
  squid_app_path      = "${local.m3uproxy_path}/squid"
  wireguard_address   = "192.168.5.3/32"
  wireguard_dns       = "192.168.5.1"
  wireguard_allowed_ips = [
    "192.168.5.1/32",
    "192.168.5.3/32",
    "0.0.0.0/0"
  ]
  wireguard_endpoint      = "188.82.170.183"
  wireguard_endpoint_port = 1195

  m3uproxy_config = templatefile("${path.module}/resources/m3uproxy/m3uproxy.json", {
    m3uproxy_secret = var.secret
  })
  m3uproxy_config_checksum = nonsensitive(sha256(local.m3uproxy_config))

  m3uproxy_playlist          = file("${path.module}/resources/m3uproxy/playlist.json")
  m3uproxy_playlist_checksum = sha256(local.m3uproxy_playlist)

  wireguard_config = templatefile("${path.module}/resources/wireguard/wireguard.conf.tpl", {
    wireguard_private_key = var.wireguard_private_key
    wireguard_public_key  = var.wireguard_public_key
    wireguard_address     = local.wireguard_address
    wireguard_dns         = local.wireguard_dns
    wireguard_allowed_ips = join(", ", local.wireguard_allowed_ips)
    wireguard_endpoint    = "${local.wireguard_endpoint}:${local.wireguard_endpoint_port}"
  })
  wireguard_config_checksum = nonsensitive(sha256(local.wireguard_config))

  squid_config          = file("${path.module}/resources/squid/squid.conf")
  squid_config_checksum = sha256(local.squid_config)
}

