version: '3'

includes:
  common: ../Taskfile.yml

vars:
  # Signing configuration - edit these values for your project
  # PGP_KEY: "path/to/signing-key.asc"
  # SIGN_ROLE: "builder"  # Options: origin, maint, archive, builder
  #
  # Password is stored securely in system keychain. Run: wails3 setup signing

  # Docker image for cross-compilation (used when building on non-Linux or no CC available)
  CROSS_IMAGE: wails-cross

tasks:
  build:
    summary: Builds the application for Linux
    cmds:
      # Linux requires CGO - use Docker when:
      # 1. Cross-compiling from non-Linux, OR
      # 2. No C compiler is available, OR
      # 3. Target architecture differs from host architecture (cross-arch compilation)
      - task: '{{if and (eq OS "linux") (eq .HAS_CC "true") (eq .TARGET_ARCH ARCH)}}build:native{{else}}build:docker{{end}}'
        vars:
          ARCH: '{{.ARCH}}'
          DEV: '{{.DEV}}'
          OUTPUT: '{{.OUTPUT}}'
          EXTRA_TAGS: '{{.EXTRA_TAGS}}'
          OBFUSCATED: '{{.OBFUSCATED}}'
          GARBLE_ARGS: '{{.GARBLE_ARGS}}'
    vars:
      DEFAULT_OUTPUT: '{{.BIN_DIR}}/{{.APP_NAME}}'
      OUTPUT: '{{ .OUTPUT | default .DEFAULT_OUTPUT }}'
      # Determine target architecture (defaults to host ARCH if not specified)
      TARGET_ARCH: '{{.ARCH | default ARCH}}'
      # Check if a C compiler is available (gcc or clang) — cross-platform via wails3 tool
      HAS_CC:
        sh: 'wails3 tool has "gcc|clang"'

  build:native:
    summary: Builds the application natively on Linux
    internal: true
    deps:
      - task: common:go:mod:tidy
      - task: common:build:frontend
        vars:
          BUILD_FLAGS:
            ref: .BUILD_FLAGS
          OBFUSCATED:
            ref: .OBFUSCATED
          DEV:
            ref: .DEV
      - task: common:generate:icons
      - task: generate:dotdesktop
    preconditions:
      - sh: '{{if eq .OBFUSCATED "true"}}command -v garble >/dev/null 2>&1{{else}}true{{end}}'
        msg: "garble is required for obfuscated builds. Install it with: go install mvdan.cc/garble@v0.16.0 (requires Go 1.24+). See https://github.com/burrowers/garble/releases for version/toolchain compatibility."
    cmds:
      - '{{if eq .OBFUSCATED "true"}}garble {{.GARBLE_ARGS}} build{{else}}go build{{end}} {{.BUILD_FLAGS}} -o {{.OUTPUT}}'
    vars:
      # LDFLAGS_EXTRA is this repo's one edit to the scaffold: `wails3
      # build` has no -ldflags flag of its own, and the packaging recipes
      # (packaging/arch/PKGBUILD, the Homebrew formula) need to stamp
      # main.version / main.commit.  Pass it as a task variable:
      #   wails3 task build LDFLAGS_EXTRA="-X 'main.version=v1.3.0'"
      # It is appended inside the production -ldflags string, so the
      # default -w -s still applies.  Empty by default, which is what
      # keeps `make build-dev` / `make build-prod` unchanged.
      BUILD_FLAGS: '{{if eq .DEV "true"}}{{if or .EXTRA_TAGS (eq .OBFUSCATED "true")}}-tags {{if eq .OBFUSCATED "true"}}wails_obfuscated{{if .EXTRA_TAGS}},{{end}}{{end}}{{.EXTRA_TAGS}} {{end}}-buildvcs=false -gcflags=all="-l"{{else}}-tags production{{if eq .OBFUSCATED "true"}},wails_obfuscated{{end}}{{if .EXTRA_TAGS}},{{.EXTRA_TAGS}}{{end}} -trimpath -buildvcs=false -ldflags="-w -s{{if .LDFLAGS_EXTRA}} {{.LDFLAGS_EXTRA}}{{end}}"{{end}}'
      DEFAULT_OUTPUT: '{{.BIN_DIR}}/{{.APP_NAME}}'
      OUTPUT: '{{ .OUTPUT | default .DEFAULT_OUTPUT }}'
    env:
      GOOS: linux
      CGO_ENABLED: 1
      GOARCH: '{{.ARCH | default ARCH}}'

  build:docker:
    summary: Builds for Linux using Docker (for non-Linux hosts or when no C compiler available)
    internal: true
    deps:
      - task: common:build:frontend
        vars:
          OBFUSCATED:
            ref: .OBFUSCATED
      - task: common:generate:icons
      - task: generate:dotdesktop
    preconditions:
      - sh: docker info > /dev/null 2>&1
        msg: "Docker is required for cross-compilation to Linux. Please install Docker."
      - sh: docker image inspect {{.CROSS_IMAGE}} > /dev/null 2>&1
        msg: |
          Docker image '{{.CROSS_IMAGE}}' not found.
          Build it first: wails3 task setup:docker
    cmds:
      - docker run --rm -v "{{.ROOT_DIR}}:/app" {{.DOCKER_MOUNTS}} -e APP_NAME="{{.APP_NAME}}" {{if .EXTRA_TAGS}}-e EXTRA_TAGS="{{.EXTRA_TAGS}}"{{end}} {{if eq .OBFUSCATED "true"}}-e OBFUSCATED=true{{end}} {{if .GARBLE_ARGS}}-e GARBLE_ARGS="{{.GARBLE_ARGS}}"{{end}} "{{.CROSS_IMAGE}}" linux {{.DOCKER_ARCH}}
      - cmd: docker run --rm -v "{{.ROOT_DIR}}:/app" alpine chown -R $(id -u):$(id -g) /app/bin
        platforms: [linux, darwin]
      - mkdir -p {{.BIN_DIR}}
      - mv "bin/{{.APP_NAME}}-linux-{{.DOCKER_ARCH}}" "{{.OUTPUT}}"
    vars:
      DOCKER_ARCH: '{{.ARCH | default "amd64"}}'
      DEFAULT_OUTPUT: '{{.BIN_DIR}}/{{.APP_NAME}}'
      OUTPUT: '{{ .OUTPUT | default .DEFAULT_OUTPUT }}'
      # Generate Docker volume mounts: Go module cache + go.mod replace directives
      # Uses wails3 tool docker-mounts for cross-platform compatibility (Windows/Linux/macOS)
      DOCKER_MOUNTS:
        sh: 'wails3 tool docker-mounts'

  package:
    summary: Packages the application for Linux
    deps:
      - task: build
    cmds:
      - task: create:appimage
      - task: create:deb
      - task: create:rpm
      - task: create:aur

  create:appimage:
    summary: Creates an AppImage
    dir: build/linux/appimage
    deps:
      - task: build
      - task: generate:dotdesktop
    cmds:
      - cp "{{.APP_BINARY}}" "{{.APP_NAME}}"
      - cp ../../appicon.png "{{.APP_NAME}}.png"
      - wails3 generate appimage -binary "{{.APP_NAME}}" -icon {{.ICON}} -desktopfile {{.DESKTOP_FILE}} -outputdir {{.OUTPUT_DIR}} -builddir {{.ROOT_DIR}}/build/linux/appimage/build
    vars:
      APP_NAME: '{{.APP_NAME}}'
      APP_BINARY: '../../../bin/{{.APP_NAME}}'
      ICON: '{{.APP_NAME}}.png'
      DESKTOP_FILE: '../{{.APP_NAME}}.desktop'
      OUTPUT_DIR: '../../../bin'

  create:deb:
    summary: Creates a deb package
    deps:
      - task: build
    cmds:
      - task: generate:dotdesktop
      - task: generate:deb

  create:rpm:
    summary: Creates a rpm package
    deps:
      - task: build
    cmds:
      - task: generate:dotdesktop
      - task: generate:rpm

  create:aur:
    summary: Creates a arch linux packager package
    deps:
      - task: build
    cmds:
      - task: generate:dotdesktop
      - task: generate:aur

  generate:deb:
    summary: Creates a deb package
    cmds:
      - wails3 tool package -name "{{.APP_NAME}}" -format deb -config ./build/linux/nfpm/nfpm.yaml -out {{.ROOT_DIR}}/bin

  generate:rpm:
    summary: Creates a rpm package
    cmds:
      - wails3 tool package -name "{{.APP_NAME}}" -format rpm -config ./build/linux/nfpm/nfpm.yaml -out {{.ROOT_DIR}}/bin

  generate:aur:
    summary: Creates a arch linux packager package
    cmds:
      - wails3 tool package -name "{{.APP_NAME}}" -format archlinux -config ./build/linux/nfpm/nfpm.yaml -out {{.ROOT_DIR}}/bin

  generate:dotdesktop:
    summary: Generates a `.desktop` file
    dir: build
    cmds:
      - mkdir -p {{.ROOT_DIR}}/build/linux/appimage
      - wails3 generate .desktop -name "{{.APP_NAME}}" -exec "{{.EXEC}}" -icon "{{.ICON}}" -outputfile "{{.ROOT_DIR}}/build/linux/{{.APP_NAME}}.desktop" -categories "{{.CATEGORIES}}"
    vars:
      APP_NAME: '{{.APP_NAME}}'
      EXEC: '{{.APP_NAME}}'
      ICON: '{{.APP_NAME}}'
      CATEGORIES: 'Development;'
      OUTPUTFILE: '{{.ROOT_DIR}}/build/linux/{{.APP_NAME}}.desktop'

  run:
    cmds:
      - '{{.BIN_DIR}}/{{.APP_NAME}}'

  sign:deb:
    summary: Signs the DEB package
    desc: |
      Signs the .deb package with a PGP key.
      Set PGP_KEY in the vars section to override the key configured globally via
      `wails3 setup` (~/.config/wails/defaults.yaml).
      Password is retrieved from system keychain (run: wails3 setup signing)
    deps:
      - task: create:deb
    cmds:
      # PGP_KEY is optional: if unset, `wails3 tool sign` falls back to the key
      # configured globally via `wails3 setup`.
      - wails3 tool sign --input "{{.BIN_DIR}}/{{.APP_NAME}}*.deb" {{if .PGP_KEY}}--pgp-key "{{.PGP_KEY}}"{{end}} {{if .SIGN_ROLE}}--role "{{.SIGN_ROLE}}"{{end}}

  sign:rpm:
    summary: Signs the RPM package
    desc: |
      Signs the .rpm package with a PGP key.
      Set PGP_KEY in the vars section to override the key configured globally via
      `wails3 setup` (~/.config/wails/defaults.yaml).
      Password is retrieved from system keychain (run: wails3 setup signing)
    deps:
      - task: create:rpm
    cmds:
      - wails3 tool sign --input "{{.BIN_DIR}}/{{.APP_NAME}}*.rpm" {{if .PGP_KEY}}--pgp-key "{{.PGP_KEY}}"{{end}}

  sign:packages:
    summary: Signs all Linux packages (DEB and RPM)
    desc: |
      Signs both .deb and .rpm packages with a PGP key.
      Set PGP_KEY in the vars section to override the key configured globally via
      `wails3 setup` (~/.config/wails/defaults.yaml).
      Password is retrieved from system keychain (run: wails3 setup signing)
    cmds:
      - task: sign:deb
      - task: sign:rpm
