# Copyright (C) Markus Franz Xaver Johannes Oberhumer
# BS BuildSystem: build "by hand" using a POSIX-compliant shell

name: 'Weekly CI BS - By Hand'
on:
  schedule: [cron: '00 1 * * 3'] # run weekly Wednesday 01:00 UTC
  workflow_dispatch:
env:
  CMAKE_REQUIRED_QUIET: OFF
  DEBIAN_FRONTEND: noninteractive
  VERBOSE: 1

jobs:
  job-alpine-by-hand: # uses a POSIX-compliant shell
    # ...and also uses a subdirectory "upx with space" in order to detect possible quoting issues
    # ...and also uses ccache as we are running the same build-script again and again
    if: github.repository_owner == 'upx'
    strategy: { matrix: { container: ['alpine:3.9','alpine:3.18','alpine:edge','i386/alpine:edge'] } }
    name: ${{ format('gcc by-hand {0}', matrix.container) }}
    runs-on: ubuntu-latest
    container: ${{ matrix.container }}
    steps:
      - name: ${{ format('Install packages {0}', matrix.container) }}
        run: |
          # install ccache, g++, git and various POSIX shells
          shells="bash dash loksh mksh zsh"
          case ${{ matrix.container }} in
            *:3.18 | *:edge) shells="$shells oksh yash" ;;
          esac
          echo "installing shells: $shells"
          apk update && apk upgrade && apk add ccache g++ git $shells
          # enable ccache and some warnings
          xflags="-static -Wall -Wextra -Werror"
          echo -e "CC=ccache gcc $xflags\nCXX=ccache g++ -std=gnu++17 $xflags" >> $GITHUB_ENV
          # this seems to be needed when running in a container (beause of UID mismatch??)
          git config --global --add safe.directory '*'
          # create user upx:upx 2000:2000 for file system tests below ("sudo")
          adduser upx -u 2000 -D && cd /home/upx && chmod 00700 . && chown -R upx:upx .
      - name: ${{ format('Check out UPX {0} source code', github.ref_name) }}
        run: |
          git clone --branch "$GITHUB_REF_NAME" --depth 1 https://github.com/upx/upx "upx with space"
          git -C "upx with space" submodule update --init
      - name: 'Build by-hand with bash'
        run: 'bash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with bash --posix'
        run: 'bash --posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with busybox ash'
        run: 'busybox ash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with busybox sh'
        run: 'busybox sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with dash'
        run: 'dash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with loksh'
        run: 'ksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with loksh -o posix'
        run: 'ksh -o posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with loksh -o sh'
        run: 'ksh -o sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with mksh'
        run: 'mksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with mksh -o posix'
        run: 'mksh -o posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with mksh -o sh'
        run: 'mksh -o sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with oksh'
        if: endsWith(matrix.container, ':3.18') || endsWith(matrix.container, ':edge')
        run: 'oksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with oksh -o posix'
        if: endsWith(matrix.container, ':3.18') || endsWith(matrix.container, ':edge')
        run: 'oksh -o posix "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with oksh -o sh'
        if: endsWith(matrix.container, ':3.18') || endsWith(matrix.container, ':edge')
        run: 'oksh -o sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with yash'
        if: endsWith(matrix.container, ':3.18') || endsWith(matrix.container, ':edge')
        run: 'yash "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with zsh'
        run: 'zsh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with zsh --emulate ksh'
        run: 'zsh --emulate ksh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Build by-hand with zsh --emulate sh'
        run: 'zsh --emulate sh "./upx with space/misc/scripts/build_upx_by_hand.sh"'
      - name: 'Show ccache stats'
        run: |
          ccache -s
          ccache -p
      - name: 'Run file system test suite (busybox)'
        run: |
          apk add bash sudo
          testsuite="$(readlink -fn "upx with space"/misc/testsuite/test_symlinks.sh)"
          cd "upx with space"/build/by-hand
          # IMPORTANT: do NOT run as user root!
          chmod a+w . && sudo -u upx bash "$testsuite"
      - name: 'Run file system test suite (coreutils)'
        run: |
          apk add bash coreutils sudo
          testsuite="$(readlink -fn "upx with space"/misc/testsuite/test_symlinks.sh)"
          cd "upx with space"/build/by-hand
          # IMPORTANT: do NOT run as user root!
          chmod a+w . && sudo -u upx bash "$testsuite"
