id: com.floatpane.matcha
runtime: org.freedesktop.Sdk
runtime-version: "23.08"
sdk: org.freedesktop.Sdk
sdk-extensions:
  - org.freedesktop.Sdk.Extension.golang

command: matcha
finish-args:
  - --share=network
  - --share=ipc
  - --device=dri
  - --filesystem=home

modules:
  - name: libpcsclite
    buildsystem: simple
    build-commands:
      - install -Dm644 winscard.h /app/include/winscard.h
      - install -Dm644 wintypes.h /app/include/wintypes.h
      - sed 's/@VERSION@/2.4.1/' pcsclite.h.in > pcsclite.h
      - install -Dm644 pcsclite.h /app/include/pcsclite.h
      - gcc -c -o pcsclite_stub.o pcsclite_stub.c
      - mkdir -p /app/lib
      - ar rcs /app/lib/libpcsclite.a pcsclite_stub.o
      - install -Dm644 libpcsclite.pc /app/lib/pkgconfig/libpcsclite.pc
    sources:
      - type: file
        url: https://raw.githubusercontent.com/LudovicRousseau/PCSC/2.4.1/src/PCSC/winscard.h
        sha256: be88c31d0c7d5823fa2eb13d81e892301817f3597f891d476a970652aacb7b1c
      - type: file
        url: https://raw.githubusercontent.com/LudovicRousseau/PCSC/2.4.1/src/PCSC/wintypes.h
        sha256: 5eed57487b860295feafdc0d37d62f2ec63b5f790e0a80ed67500337e2b42cb4
      - type: file
        url: https://raw.githubusercontent.com/LudovicRousseau/PCSC/2.4.1/src/PCSC/pcsclite.h.in
        sha256: 4dedcdfd21e54161b7120c328091ab37270611d32dc36527baeeb5aea70085d3
      - type: inline
        dest-filename: pcsclite_stub.c
        contents: |
          #include <stddef.h>
          typedef long LONG;
          typedef unsigned long DWORD;
          typedef void *LPCVOID;
          typedef char *LPSTR;
          typedef const char *LPCSTR;
          typedef unsigned char *LPBYTE;
          typedef unsigned char BYTE;
          typedef LONG SCARDCONTEXT;
          typedef LONG SCARDHANDLE;
          typedef struct { DWORD dwProtocol; DWORD cbPciLength; } SCARD_IO_REQUEST;
          typedef struct { LPCSTR szReader; DWORD dwCurrentState; DWORD dwEventState; DWORD cbAtr; unsigned char rgbAtr[36]; void *pvUserData; } SCARD_READERSTATE;
          LONG SCardEstablishContext(DWORD s, LPCVOID r1, LPCVOID r2, SCARDCONTEXT *c) { return 0; }
          LONG SCardReleaseContext(SCARDCONTEXT c) { return 0; }
          LONG SCardIsValidContext(SCARDCONTEXT c) { return 0; }
          LONG SCardCancel(SCARDCONTEXT c) { return 0; }
          LONG SCardConnect(SCARDCONTEXT c, LPCSTR r, DWORD s, DWORD p, SCARDHANDLE *h, DWORD *ap) { return 0; }
          LONG SCardReconnect(SCARDHANDLE h, DWORD s, DWORD p, DWORD d, DWORD *ap) { return 0; }
          LONG SCardDisconnect(SCARDHANDLE h, DWORD d) { return 0; }
          LONG SCardBeginTransaction(SCARDHANDLE h) { return 0; }
          LONG SCardEndTransaction(SCARDHANDLE h, DWORD d) { return 0; }
          LONG SCardStatus(SCARDHANDLE h, LPSTR r, DWORD *rl, DWORD *s, DWORD *p, LPBYTE a, DWORD *al) { return 0; }
          LONG SCardTransmit(SCARDHANDLE h, const SCARD_IO_REQUEST *si, const BYTE *s, DWORD sl, SCARD_IO_REQUEST *ri, BYTE *r, DWORD *rl) { return 0; }
          LONG SCardControl(SCARDHANDLE h, DWORD c, LPCVOID i, DWORD il, void *o, DWORD ol, DWORD *br) { return 0; }
          LONG SCardGetAttrib(SCARDHANDLE h, DWORD a, LPBYTE b, DWORD *bl) { return 0; }
          LONG SCardSetAttrib(SCARDHANDLE h, DWORD a, const BYTE *b, DWORD bl) { return 0; }
          LONG SCardListReaders(SCARDCONTEXT c, LPCSTR g, LPSTR r, DWORD *rl) { return 0; }
          LONG SCardListReaderGroups(SCARDCONTEXT c, LPSTR g, DWORD *gl) { return 0; }
          LONG SCardGetStatusChange(SCARDCONTEXT c, DWORD t, SCARD_READERSTATE *s, DWORD n) { return 0; }
          LONG SCardFreeMemory(SCARDCONTEXT c, LPCVOID m) { return 0; }
          const char *pcsc_stringify_error(LONG e) { return "stub"; }
      - type: inline
        dest-filename: libpcsclite.pc
        contents: |
          prefix=/app
          libdir=${prefix}/lib
          includedir=${prefix}/include
          Name: libpcsclite
          Description: PC/SC Lite (stub for build)
          Version: 2.4.1
          Cflags: -I${includedir}
          Libs: -L${libdir} -lpcsclite

  - name: matcha
    buildsystem: simple
    build-options:
      append-path: /usr/lib/sdk/golang/bin
      env:
        GOCACHE: /app/.cache/go-build
        GOPATH: /app/.go
      build-args:
        - --share=network
    build-commands:
      - go build -o /app/bin/matcha .
      - chmod -R u+w /app/.go /app/.cache
      - rm -rf /app/.go /app/.cache

    sources:
      - type: dir
        path: .
