name: run units target on a big endian architecture

on:
  push:
    branches: [ master ]
  pull_request:
    branches: [ master ]

jobs:
  s390x:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v4

      - uses: actions/cache@v4
        id:   restore-cache
        with:
          path: /home/runner/linux-s390x-glibc
          key:          dependent-packages-linux-s390x-glibc
          restore-keys: dependent-packages-linux-s390x-glibc

      - if: ${{ steps.restore-cache.outputs.cache-hit != 'true' }}
        run: |
          set -x

          mkdir -p /home/runner/linux-s390x-glibc

          curl -L -o lastest.json https://api.github.com/repos/leleliu008/uppm-package-repository-linux-s390x-glibc/releases/latest

          unset RELEASE_TAG_NAME_
          unset RELEASE_FILENAMES

          RELEASE_TAG_NAME_=$(jq -r '.tag_name'      lastest.json)
          RELEASE_FILENAMES=$(jq -r '.assets[].name' lastest.json)

          for filename in $RELEASE_FILENAMES
          do
              case $filename in
                   jansson-*-linux-s390x-glibc.tar.xz) ;;
                  libiconv-*-linux-s390x-glibc.tar.xz) ;;
                   libxml2-*-linux-s390x-glibc.tar.xz) ;;
                   libyaml-*-linux-s390x-glibc.tar.xz) ;;
                     pcre2-*-linux-s390x-glibc.tar.xz) ;;
                      zlib-*-linux-s390x-glibc.tar.xz) ;;
                  *) continue ;;
              esac
              curl -LO https://github.com/leleliu008/uppm-package-repository-linux-s390x-glibc/releases/download/${RELEASE_TAG_NAME_}/${filename}
              tar xf ${filename} -C /home/runner/linux-s390x-glibc --strip-components=1
          done

          rm -rf /home/runner/linux-s390x-glibc/share/log
          rm -rf /home/runner/linux-s390x-glibc/share/man
          rm -rf /home/runner/linux-s390x-glibc/share/doc
          rm -rf /home/runner/linux-s390x-glibc/share/gtk-doc
          rm -rf /home/runner/linux-s390x-glibc/lib/libz.so*
          rm -rf /home/runner/linux-s390x-glibc/lib/lib*.la
          rm -rf /home/runner/linux-s390x-glibc/installed-files
          rm -rf /home/runner/linux-s390x-glibc/installed-metadata-ppkg

      - run: sudo apt-get -y update
      - run: sudo apt-get -y install automake autoconf pkg-config make gcc-s390x-linux-gnu

      - run: ./autogen.sh
      - run: |
            export CC=/usr/bin/s390x-linux-gnu-gcc
            export CPPFLAGS=-I/home/runner/linux-s390x-glibc/include
            export LDFLAGS=-L/home/runner/linux-s390x-glibc/lib
            export PKG_CONFIG_PATH=/home/runner/linux-s390x-glibc/lib/pkgconfig
            ./configure --host=s390x-ibm-linux-gnu --enable-debugging || (cat config.log; exit 1)
      - run: make -j$(nproc)

      - run: /usr/bin/s390x-linux-gnu-readelf -h ctags
      - run: /usr/bin/s390x-linux-gnu-readelf -d ctags

      - run: |
          cat > run.sh <<EOF
          set -e

          COLOR_GREEN='\033[0;32m'        # Green
          COLOR_PURPLE='\033[0;35m'       # Purple
          COLOR_OFF='\033[0m'             # Reset

          echo() {
              printf '%b\n' "\$*"
          }

          run() {
              echo "\${COLOR_PURPLE}==>\${COLOR_OFF} \${COLOR_GREEN}\$@\${COLOR_OFF}"
              eval "\$*"
          }

          run uname -a

          run apt-get -y update
          run apt-get -y install make python3

          run make tmain CI=1 CTAGS_DEP= READTAGS_DEP= OPTSCRIPT_DEP=
          run make units CI=1 CTAGS_DEP=
          EOF

          chmod +x run.sh

      # https://github.com/multiarch/qemu-user-static
      - run: docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
      - run: docker run -v $PWD:$PWD -w $PWD s390x/ubuntu bash run.sh
