{{- if eq .host.distro.family "linux" -}}
#!/usr/bin/env bash

{{- if (stat (joinPath (.chezmoi.sourceDir ".chezmoitemplates" "secrets" "key-vmware-workstation"))) -}}
{{-   $secretKey := (default "4C21U-2KK9Q-M8130-4V2QH-CF810" (includeTemplate "secrets/key-vmware-workstation" | decrypt)) -}}
{{- else -}}
{{-   $secretKey := (default "4C21U-2KK9Q-M8130-4V2QH-CF810" (env "VMWARE_WORKSTATION_LICENSE_KEY")) -}}
{{- end }}

# Source: https://gist.github.com/PurpleVibe32/30a802c3c8ec902e1487024cdea26251
# key: {{- $secretKey }}

{{ includeTemplate "universal/profile-before" }}
{{ includeTemplate "universal/logg-before" }}

### Install VMware Workstation
if ! command -v vmware > /dev/null; then
  ### Download VMWare Workstation
  logg info 'VMware Workstation is not installed'
  VMWARE_WORKSTATION_URL=https://www.vmware.com/go/getworkstation-linux
  VMWARE_WORKSTATION_DIR=/tmp/workstation-downloads
  mkdir -p $VMWARE_WORKSTATION_DIR
  logg info 'Downloading VMware Workstation Installer'
  curl -sSLA "Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20220101 Firefox/102.0" "$VMWARE_WORKSTATION_URL" -o "$VMWARE_WORKSTATION_DIR/tryworkstation-linux-64.sh"

  ### Register product key / license
  VMWARE_WORKSTATION_LICENSE_KEY='{{- $secretKey -}}'
  if [ -n "$VMWARE_WORKSTATION_LICENSE_KEY" ]; then
    logg info 'Registering VMware Workstation Pro license with serial number'
    sudo "$VMWARE_WORKSTATION_DIR/tryworkstation-linux-64.sh" --eulas-agreed --console --required --set-setting vmware-workstation serialNumber "$VMWARE_WORKSTATION_LICENSE_KEY"
  else
    logg info 'Agreeing to VMWare Workstation Pro license (without serial number)'
    sudo "$VMWARE_WORKSTATION_DIR/tryworkstation-linux-64.sh" --eulas-agreed --console --required
  fi
  logg success 'VMware Workstation installed successfully'
else
  logg info 'VMware Workstation is already installed'
fi
{{ end -}}