{{/*
vim:ft=gotmpl
*/}}
{{/* https://github.com/twpayne/dotfiles/blob/master/home/.chezmoi.toml.tmpl  */}}

{{/* Variable Definitions */}}
{{- $ephemeral := false -}}
{{- $headless := false  -}}
{{- $personal := false -}}
{{- $nix := false -}}
{{- $work := false -}}
{{- $wheel := false -}}
{{- $username := "" -}}
{{- $email := "" -}}
{{- $hostname := .chezmoi.hostname -}}
{{- $wheel := false -}}
{{- $diffCmd := "diff" -}}
{{- $encryptionProvider := "age" -}}
{{- $defaultIdentityPath := joinPath .chezmoi.homeDir ".config" "age" "key.txt" -}}
{{- $manageSecret := false -}}
{{- if stat $defaultIdentityPath -}}
{{-     $manageSecret = true -}}
{{ else -}}
{{-     $manageSecret = promptBoolOnce . "manageSecret" "Do you want to manage secrets" -}}
{{-     if $manageSecret -}}
{{-         $defaultIdentityPath = promptStringOnce . "defaultIdentityPath" "What is your default identity path, relative to your home directory" -}}
{{-         $defaultIdentityPath = joinPath .chezmoi.homeDir $defaultIdentityPath -}}
{{-     end -}}
{{- end -}}
{{- if stat $defaultIdentityPath -}}
{{- $dataDir := joinPath .chezmoi.homeDir ".dotfiles" "home" -}}
{{- $dataPath := joinPath $dataDir "secrets" "data.json.age" -}}
{{- $data := output "age" "--decrypt" "--identity" $defaultIdentityPath $dataPath | fromJson -}}
{{- $username = $data.username -}}
{{- $email = $data.email -}}
{{- end -}}
{{ if eq $username "" -}}
{{-     $username = promptStringOnce . "username" "What is your username" -}}
{{ end -}}
{{ if eq $email "" -}}
{{-     $email = promptStringOnce . "email" "What is your email address" -}}
{{ end -}}

{{/* darwin hostname hack */}}
{{- if eq .chezmoi.os "darwin" -}}
{{-   $hostname := output "scutil" "--get" "LocalHostName" | trim -}}
{{- end -}}

{{/* Wheel group detection */}}
{{- $wheel = or
  (and
    (eq .chezmoi.os "linux")
    (output "groups" | regexMatch "\\b(sudo|wheel|admin)\\b")
  )
  (and
    (eq .chezmoi.os "darwin")
    (output "groups" | regexMatch "\\b(admin|wheel)\\b")
  )
  (and
    (eq .chezmoi.os "windows")
    (output "powershell.exe" "-NoProfile" "-Command" "net localgroup administrators | Select-String $env:USERNAME")
  )
-}}

{{/*
Determining nix package manager
If under nix, some dotfiles should be managed via home-manager
If under nixOS, most scripts that require root access should not be run, as they are managed by nixOS
*/}}
{{/* nixOS */}}
{{- if eq .chezmoi.os "linux" -}}
{{- if eq .chezmoi.osRelease.id "nixos" }}
{{- $nix = true -}}
{{- end -}}
{{/* nix on other linux distros */}}
{{- if isExecutable "/nix/var/nix/profiles/default/bin/nix-env" -}}
{{- $nix = true -}}
{{- end -}}
{{- end -}}
{{- if eq .chezmoi.os "darwin" -}}
{{/* nix-darwin */}}
{{ if isExecutable "/run/current-system/sw/bin/darwin-rebuild" -}}
{{ $nix = true -}}
{{ end -}}
{{ end -}}

{{/*
Determining headless
*/}}
{{/* detect GitHub codespaces, VSCode remote containers, Docker containers, Multipass VMs, and Vagrant boxes */}}
{{- if or (env "CODESPACES") (env "REMOTE_CONTAINERS_IPC") (eq .chezmoi.username "root" "vagrant" "vscode") -}}
{{-   $ephemeral = true -}}
{{-   $headless = true -}}
{{- end -}}
{{- if not $headless -}}
{{- if eq .chezmoi.os  "linux" -}}
{{/* Fedora Server
{{- if eq .chezmoi.osRelease.variantID "server" -}}
{{- $headless = true
{{- end -}}
*/}}
{{/* Treat all Debian hosts as headless */}}
{{- if eq .chezmoi.osRelease.id "debian" -}}
{{- $headless = true -}}
{{- end -}}
{{- end -}}
{{- end -}}

{{/*
Determining diff command, fallback to diff
*/}}
{{- if lookPath "delta" -}}
{{-   $diffCmd = "delta" -}}
{{- else if lookPath "git-delta" -}}
{{-   $diffCmd = "git-delta" -}}
{{- end -}}

{{/*
Determining encryption provider, fallback to gpg
*/}}
{{- if lookPath "age" -}}
{{-   $encryptionProvider = "age" -}}
{{- else if lookPath "rage" -}}
{{-   $encryptionProvider = "rage" -}}
{{- else -}}
{{-   $encryptionProvider = "gpg" -}}
{{- end -}}

sourceDir = "~/.dotfiles"
encryption = {{ $encryptionProvider | quote }}

[data]
    email = {{ $email | quote }}
    username = {{ $username | quote }}
    headless = {{ $headless }}
    nix = {{ $nix }}
    wheel = {{ $wheel }}
    manageSecret = {{ $manageSecret }}

[diff]
    command = {{ $diffCmd | quote }}

[age]
    identities = [
        {{ $defaultIdentityPath | quote }}
        ]
    recipients = [
        "age1mcvqpg39t32ll684r4m2l8j0l9zag6endg0h6zjw8svkgdwc4pjqkk5fvj",
        "age1z2km2vu3mw6usw8p2rhjzkugddmsvk0ypwjw76cscyy3ar3tnufqtrv3nu",
    ]
