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}}'
    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)
      HAS_CC:
        sh: '(command -v gcc >/dev/null 2>&1 || command -v clang >/dev/null 2>&1) && echo "true" || echo "false"'

  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
          DEV:
            ref: .DEV
      - task: common:generate:icons
      - task: generate:dotdesktop
    cmds:
      - go build {{.BUILD_FLAGS}} -o {{.OUTPUT}}
    vars:
      BUILD_FLAGS: '{{if eq .DEV "true"}}{{if .EXTRA_TAGS}}-tags {{.EXTRA_TAGS}} {{end}}-buildvcs=false -gcflags=all="-l"{{else}}-tags production{{if .EXTRA_TAGS}},{{.EXTRA_TAGS}}{{end}} -trimpath -buildvcs=false -ldflags="-w -s"{{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
      - 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" {{.GO_CACHE_MOUNT}} {{.REPLACE_MOUNTS}} -e APP_NAME="{{.APP_NAME}}" {{if .EXTRA_TAGS}}-e EXTRA_TAGS="{{.EXTRA_TAGS}}"{{end}} "{{.CROSS_IMAGE}}" linux {{.DOCKER_ARCH}}
      - docker run --rm -v "{{.ROOT_DIR}}:/app" alpine chown -R $(id -u):$(id -g) /app/bin
      - 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 }}'
      # Mount Go module cache for faster builds
      GO_CACHE_MOUNT:
        sh: 'echo "-v ${GOPATH:-$HOME/go}/pkg/mod:/go/pkg/mod"'
      # Extract replace directives from go.mod and create -v mounts for each
      REPLACE_MOUNTS:
        sh: |
          grep -E '^replace .* => ' go.mod 2>/dev/null | while read -r line; do
            path=$(echo "$line" | sed -E 's/^replace .* => //' | tr -d '\r')
            # Convert relative paths to absolute
            if [ "${path#/}" = "$path" ]; then
              path="$(cd "$(dirname "$path")" 2>/dev/null && pwd)/$(basename "$path")"
            fi
            # Only mount if directory exists
            if [ -d "$path" ]; then
              echo "-v $path:$path:ro"
            fi
          done | tr '\n' ' '

  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.
      Configure PGP_KEY in the vars section at the top of this file.
      Password is retrieved from system keychain (run: wails3 setup signing)
    deps:
      - task: create:deb
    cmds:
      - wails3 tool sign --input "{{.BIN_DIR}}/{{.APP_NAME}}*.deb" --pgp-key {{.PGP_KEY}} {{if .SIGN_ROLE}}--role {{.SIGN_ROLE}}{{end}}
    preconditions:
      - sh: '[ -n "{{.PGP_KEY}}" ]'
        msg: "PGP_KEY is required. Set it in the vars section at the top of build/linux/Taskfile.yml"

  sign:rpm:
    summary: Signs the RPM package
    desc: |
      Signs the .rpm package with a PGP key.
      Configure PGP_KEY in the vars section at the top of this file.
      Password is retrieved from system keychain (run: wails3 setup signing)
    deps:
      - task: create:rpm
    cmds:
      - wails3 tool sign --input "{{.BIN_DIR}}/{{.APP_NAME}}*.rpm" --pgp-key {{.PGP_KEY}}
    preconditions:
      - sh: '[ -n "{{.PGP_KEY}}" ]'
        msg: "PGP_KEY is required. Set it in the vars section at the top of build/linux/Taskfile.yml"

  sign:packages:
    summary: Signs all Linux packages (DEB and RPM)
    desc: |
      Signs both .deb and .rpm packages with a PGP key.
      Configure PGP_KEY in the vars section at the top of this file.
      Password is retrieved from system keychain (run: wails3 setup signing)
    cmds:
      - task: sign:deb
      - task: sign:rpm
    preconditions:
      - sh: '[ -n "{{.PGP_KEY}}" ]'
        msg: "PGP_KEY is required. Set it in the vars section at the top of build/linux/Taskfile.yml"
