{{- $name := (default "Brian Zalewski" (env "FULL_NAME")) -}}
{{- $email := (default "brian@megabyte.space" (env "PRIMARY_EMAIL")) -}}
{{- $restricted := (default false (env "WORK_ENVIRONMENT")) -}}
{{- $work := (default false (env "WORK_ENVIRONMENT")) -}}
{{- $gpgKeyId := (default "0xF0A300E4199A1C33" (env "KEYID")) -}}
{{- $gmailAddress := (default "blzalewski@gmail.com" (env "GMAIL_ADDRESS")) -}}
{{- $gmailAddressAppPassword := (default "" (env "GMAIL_APP_PASSWORD")) -}}
{{- $domain := (default "megabyte.space" (env "PUBLIC_SERVICES_DOMAIN")) -}}
{{- $cloudflareUsername := (default "brian@megabyte.space" (env "CLOUDFLARE_USERNAME")) -}}
{{- $cloudflareToken := (default "" (env "CLOUDFLARE_API_TOKEN")) -}}
{{- $cloudflareAccessKeyId := "" -}}
{{- $cloudflareSecretAccessKey := "" -}}
{{- $cloudflareR2AccountId := "" -}}
{{- $githubUsername := (default "ProfessorManhattan" (env "GITHUB_USERNAME")) -}}
{{- $githubGistToken := (default "" (env "GITHUB_GIST_TOKEN")) -}}
{{- $githubReadToken := (env "GITHUB_READ_TOKEN") -}}
{{- $gitlabReadToken := (env "GITLAB_READ_TOKEN") -}}
{{- $locale := (output "echo" "$LANG") }}
{{- $ngrokAuthToken := (default "" (env "NGROK_AUTH_TOKEN")) -}}
{{- $slackApiToken := (default "" (env "SLACK_API_TOKEN")) -}}
{{- $tailscaleAuthKey := (default "" (env "TAILSCALE_AUTH_KEY")) -}}
{{- $timezone := (default "America/New_York" (env "TIMEZONE")) -}}
{{- $toolchains := list "CLI-Extras" "Docker" "Go" "Kubernetes" "Web-Development" -}}
{{- $desktopSession := true -}}
{{- if and (ne .chezmoi.os "darwin") (ne .chezmoi.os "windows") (not (env "DISPLAY")) -}}
{{-   $desktopSession = false -}}
{{- end -}}
{{- $softwareGroup := (default "Standard" (env "SOFTWARE_GROUP")) -}}
{{- if $desktopSession -}}
{{-   $softwareGroup = list $softwareGroup "-Desktop" | join "" -}}
{{- end -}}

{{- $data := . }}
{{- $_ := set $data "host" (default (dict) (get $data "host")) -}}
{{- $_ := set $data "user" (default (dict) (get $data "user")) -}}
{{- $_ := set $data "toolchains" (default (dict) (get $data "toolchains")) -}}

{{- $headless := false -}}
{{- $ephemeral := false -}}
{{/* detect GitHub codespaces, VSCode remote containers, Docker containers, Multipass VMs, and Vagrant boxes */}}
{{- $ephemeralEnvOrUsername := or (env "CODESPACES") (env "REMOTE_CONTAINERS_IPC") (eq .chezmoi.username "root" "ubuntu" "vagrant" "vscode" "devcontainer") -}}
{{- $ephemeralCgroup := and (stat "/proc/1/cgroup") (output "cat" "/proc/1/cgroup" | regexMatch "(docker|lxc)") -}}
{{- if or $ephemeralEnvOrUsername $ephemeralCgroup -}}
{{-   $headless = true -}}
{{-   $ephemeral = true -}}
{{-   writeToStdout "Chezmoi is running in a container.\n" -}}
{{- end -}}

{{- $chassisType := "desktop" }}
{{- if $ephemeral -}}
{{-   $chassisType = "ephemeral" }}
{{- else if eq .chezmoi.os "linux" }}
{{-   if (.chezmoi.kernel.osrelease | lower | contains "microsoft") -}}
{{-     $chassisType = "wsl" }}
{{-   else -}}
{{-     $chassisType = "todo-get-from-hostnamectl" }}
{{-   end -}}
{{- else if eq .chezmoi.os "darwin" }}
{{-   if contains "MacBook" (output "sysctl" "-n" "hw.model") }}
{{-     $chassisType = "laptop" }}
{{-   else }}
{{-     $chassisType = "desktop" }}
{{-   end }}
{{- else if eq .chezmoi.os "windows" }}
{{-   $chassisType = (output "powershell.exe" "-noprofile" "-command" "if (Get-WmiObject -Class win32_battery -ComputerName localhost) { echo laptop } else { echo desktop }") }}
{{- end }}

{{- $cpuCores := 1 }}
{{- $cpuThreads := 1 }}
{{- if eq .chezmoi.os "darwin" }}
{{-   $cpuCores = (output "sysctl" "-n" "hw.physicalcpu_max") | trim | atoi }}
{{-   $cpuThreads = (output "sysctl" "-n" "hw.logicalcpu_max") | trim | atoi }}
{{- else if eq .chezmoi.os "linux" }}
{{-   $cpuCores = (output "sh" "-c" "lscpu --online --parse | grep --invert-match '^#' | sort --field-separator=',' --key='2,4' --unique | wc --lines") | trim | atoi }}
{{-   $cpuThreads = (output "sh" "-c" "lscpu --online --parse | grep --invert-match '^#' | wc --lines") | trim | atoi }}
{{- else if eq .chezmoi.os "windows" }}
{{-   $cpuCores = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "(Get-CimInstance -ClassName 'Win32_Processor').NumberOfCores") | trim | atoi }}
{{-   $cpuThreads = (output "powershell.exe" "-NoProfile" "-NonInteractive" "-Command" "(Get-CimInstance -ClassName 'Win32_Processor').NumberOfLogicalProcessors") | trim | atoi }}
{{- end }}

{{- $toolchainsEnabled := dict }}
{{- range $toolchain := $toolchains }}
    {{- $withoutToolchains := env "WITHOUT_TOOLCHAINS" -}}
    {{- $withoutToolchain := env (list "WITHOUT" (upper $toolchain) | join "_") -}}
    {{- $withToolchain := env (list "WITH" (upper $toolchain) | join "_") -}}
    {{- if and (or $withoutToolchains $withoutToolchain) (not $withToolchain) -}}
      {{- writeToStdout (list "Disabled" $toolchain "toolchain.\n" | join " ")  -}}
      {{- $_ := set $toolchainsEnabled $toolchain true }}
    {{- else }}
      {{- $_ := set $toolchainsEnabled $toolchain true }}
    {{- end}}
{{- end }}

{{- if stdinIsATTY -}}

{{- if not (env "WORK_ENVIRONMENT") -}}
{{-   $work = promptBoolOnce $data.host "work" "Work environment" $work -}}
{{- end -}}
{{- if not (env "RESTRICTED_ENVIRONMENT") -}}
{{-   $restricted = promptBoolOnce $data.host "restricted" "Restricted environment (i.e. no sudo)" $restricted -}}
{{- end -}}
{{- if not (env "SOFTWARE_GROUP") -}}
{{-   $softwareGroup = promptStringOnce $data.host "softwareGroup" "Software group" $softwareGroup -}}
{{- end -}}
{{- if not (env "FULL_NAME") -}}
{{-   $name = promptStringOnce $data.user "name" "Full name" $name -}}
{{- end -}}
{{- if not (env "PRIMARY_EMAIL") -}}
{{-   $email = promptStringOnce $data.user "email" "Primary e-mail address" $email -}}
{{- end -}}
{{- if not (env "PUBLIC_SERVICES_DOMAIN") -}}
{{-   $domain = promptStringOnce $data.user "domain" "Domain name" $domain -}}
{{- end -}}
{{- if not (env "TIMEZONE") -}}
{{-   $timezone = promptStringOnce $data.user "timezone" "Timezone" $timezone -}}
{{- end -}}
{{- if not (env "CLOUDFLARE_TOKEN") -}}
{{-   $cloudflareToken = promptStringOnce $data.user "cloudflareToken" "CloudFlare API Token" $cloudflareToken -}}
{{- end -}}

{{- else -}}
{{-   $headless = true -}}
{{-   writeToStdout "Chezmoi is running in headless environment.\n" -}}
{{- end -}}

encryption: "age"
age:
  identity: "{{ .chezmoi.homeDir }}/.config/age/chezmoi.txt"
  recipient: "age1necy24c4lzxheey4p2m8v4q000n442wyv47qc640ulyxx9l8dpesdqv7ey"
gpg:
  recipient: "{{ $gpgKeyId }}"
data:
  host:
    arch: "{{ .chezmoi.arch }}"
    cpu:
      cores: "{{ $cpuCores }}"
      threads: "{{ $cpuThreads }}"
    desktopSession: "{{ $desktopSession }}"
    distro:
      family: "{{ .chezmoi.os }}"
      id: "{{ get .chezmoi.osRelease "id" | default .chezmoi.os }}"
    home: "{{ .chezmoi.homeDir }}"
    homeParentFolder: "{{ if eq .chezmoi.os "linux" }}/home{{ else if eq .chezmoi.os "darwin" }}/Users{{ else }}C:\Users{{ end }}"
    hostname: "Betelgeuse"
    qubes: {{ ne (stat (joinPath "usr" "bin" "qubes-session")) false }}
    softwareGroup: "{{ $softwareGroup }}"
    type: "{{ $chassisType }}"
    work: {{ $work }}
    restricted: {{ $restricted }}
    headless: {{ $headless }}
  toolchains:
  {{- range $toolchain, $enabled := $toolchainsEnabled }}
    {{ $toolchain}}: {{ $enabled }}
  {{- end }}
  user:
    email: "{{ $email }}"
    name: "{{ $name }}"
    username: "{{ output "echo" "$USER" }}"
    domain: "{{ $domain }}"
    gpg:
      id: "{{ $gpgKeyId }}"
    gmail:
      email: "{{ $gmailAddress }}"
      password: "{{ $gmailAddressAppPassword }}"
    githubUsername: "{{ $githubUsername }}"
    locale: "{{ $locale }}"
    timezone: "{{ $timezone }}"
    holdSudoPrivileges: true
    CLOUDFLARE_USERNAME: "{{ $cloudflareUsername }}"
    CLOUDFLARE_ACCESS_KEY_ID: "{{ $cloudflareAccessKeyId }}"
    CLOUDFLARE_SECRET_ACCESS_KEY: "{{ $cloudflareSecretAccessKey }}"
    CLOUDFLARE_R2_ACCOUNT_ID: "{{ $cloudflareR2AccountId }}"
    CLOUDSDK_CORE_PROJECT: "megabyte-labs"
    GCE_CREDENTIALS_FILE: "{{ joinPath .chezmoi.homeDir ".config" "gcp.json" }}"
    GCE_SERVICE_ACCOUNT_EMAIL: "molecule@megabyte-labs.iam.gserviceaccount.com"
    GITHUB_GIST_TOKEN: "{{ $githubGistToken }}"
    GITHUB_READ_TOKEN: "{{ $githubReadToken }}"
    GITLAB_READ_TOKEN: "{{ $gitlabReadToken }}"
    NGROK_AUTH_TOKEN: "{{ $ngrokAuthToken }}"
    SLACK_API_TOKEN: "{{ $slackApiToken }}"
    SNAPCRAFT_EMAIL: "{{ $email }}"
    TAILSCALE_AUTH_KEY: "{{ $tailscaleAuthKey }}"
    TINYPNG_API_KEY: "g355tx7dxG5yJfl0RXJnpQlQqk88dJBv"
diff:
  format: "git"
  pager: "delta"
git:
  autoCommit: true
  autoPush: true
scriptEnv:
  LEXICON_CLOUDFLARE_USERNAME: "{{ $cloudflareUsername }}"
  LEXICON_CLOUDFLARE_TOKEN: "{{ $cloudflareToken }}"
textconv:
  - pattern: "**/*.plist"
    command: "plutil"
    args:
      - "-convert"
      - "xml1"
      - "-o"
      - "-"
      - "-"
