on: [push]
jobs:
  test_checkout:
    runs-on: docker
    container:
      image: ghcr.io/typst/typst:v0.13.1
    steps:
      - run: apk add --no-cache git curl
      - run: |
          mkdir /tmp/font
          cd /tmp/font 
          curl -L -O http://www.gust.org.pl/projects/e-foundry/tex-gyre/whole/tg2_501otf.zip
          unzip tg2_501otf.zip
          install -Dt /usr/share/fonts/tex-gyre -m644 tg2_501otf/*.otf
          mkdir -p ~/.local/share/typst/packages/local/template-thesis-matisse/
          git clone https://git.mineau.eu/histausse/template-thesis-matisse.git ~/.local/share/typst/packages/local/template-thesis-matisse/0.0.1
      - run: |
          TOKEN=$(echo -n "x-access-token:${GITHUB_TOKEN}" | base64)
          mkdir -p "${GITHUB_WORKSPACE}"
          cd "${GITHUB_WORKSPACE}"
          git init
          git remote add origin "${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}"
          git -c http.extraHeader="Authorization: Basic ${TOKEN}" fetch origin "${GITHUB_SHA}"
          git checkout "${GITHUB_SHA}"
          ls
      - run: |
          cd "${GITHUB_WORKSPACE}"
          /bin/typst compile --input commit=$(git rev-parse --short HEAD) --font-path /usr/share/fonts/ main.typ
          /bin/typst compile --input draft=false --input abstract-only=true main.typ abstract.pdf
      - run: |
          curl -s --show-headers --user "histausse:${{secrets.TOKEN_HISTAUSSE_PACKAGE}}" -X DELETE "${GITHUB_SERVER_URL}/api/packages/these-ammii/generic/thesis/latest/draft.pdf"
          curl -s --show-headers --user "histausse:${{secrets.TOKEN_HISTAUSSE_PACKAGE}}" --upload-file "${GITHUB_WORKSPACE}/main.pdf" "${GITHUB_SERVER_URL}/api/packages/these-ammii/generic/thesis/latest/draft.pdf"
          curl -s --show-headers --user "histausse:${{secrets.TOKEN_HISTAUSSE_PACKAGE}}" -X DELETE "${GITHUB_SERVER_URL}/api/packages/these-ammii/generic/thesis/latest/abstract.pdf"
          curl -s --show-headers --user "histausse:${{secrets.TOKEN_HISTAUSSE_PACKAGE}}" --upload-file "${GITHUB_WORKSPACE}/main.pdf" "${GITHUB_SERVER_URL}/api/packages/these-ammii/generic/thesis/latest/abstract.pdf"

#          curl -s --show-headers --user "histausse:${{secrets.TOKEN_HISTAUSSE_PACKAGE}}" --upload-file "${GITHUB_WORKSPACE}/main.pdf" "${GITHUB_SERVER_URL}/api/packages/these-ammii/generic/thesis/${GITHUB_SHA}/draft.pdf"
