on:
  push:
    branches:
     - master
  pull_request:
name: Build Irssi Termux package
jobs:
  termux-package:
    runs-on: ubuntu-latest
    steps:
      - name: checkout termux-packages
        uses: actions/checkout@main
        with:
          repository: termux/termux-packages
      - name: checkout irssi
        uses: actions/checkout@main
        with:
          path: src.irssi.git
      - name: download termux docker container
        uses: docker://termux/package-builder:latest
      - name: build perl package
        run: |
          ./scripts/run-docker.sh ./build-package.sh -i perl
      - name: create irssi build receipe
        run: |
          mkdir packages/irssi-an
          cat << 'BUILD_SH' > packages/irssi-an/build.sh
          TERMUX_PKG_HOMEPAGE=https://ailin-nemui.github.io/irssi/
          TERMUX_PKG_DESCRIPTION="Terminal based IRC client"
          TERMUX_PKG_LICENSE="GPL-2.0"
          TERMUX_PKG_MAINTAINER="@ailin-nemui"
          TERMUX_PKG_VERSION=@VERSION@
          TERMUX_PKG_REVISION=@REVISION@
          TERMUX_PKG_SRCURL=file:///home/builder/termux-packages/src.irssi.git
          TERMUX_PKG_AUTO_UPDATE=true
          TERMUX_PKG_DEPENDS="glib, libandroid-glob, libiconv, libotr, ncurses, openssl, perl, utf8proc"
          TERMUX_PKG_BREAKS="irssi"
          TERMUX_PKG_REPLACES="irssi"
          TERMUX_PKG_EXTRA_CONFIGURE_ARGS="
          -Dfhs-prefix=$TERMUX_PREFIX
          "

          termux_step_configure_meson() {
              termux_setup_meson

              local perl_version=$(. $TERMUX_SCRIPTDIR/packages/perl/build.sh; echo $TERMUX_PKG_VERSION)
              local perl_srcdir=$TERMUX_TOPDIR/perl/src

              sed -i "/\\[binaries\\]/a\\
          perl = ['$perl_srcdir/miniperl', '-I$TERMUX_PREFIX/lib/perl5/$perl_version/${TERMUX_ARCH}-android', '-I$TERMUX_PREFIX/lib/perl5/$perl_version']" \
                  $TERMUX_MESON_CROSSFILE

              CC=gcc CXX=g++ CFLAGS= CXXFLAGS= CPPFLAGS= LDFLAGS= $TERMUX_MESON \
                $TERMUX_PKG_SRCDIR \
                $TERMUX_PKG_BUILDDIR \
                --cross-file $TERMUX_MESON_CROSSFILE \
                --prefix $TERMUX_PREFIX \
                --libdir lib \
                --buildtype minsize \
                --strip \
                $TERMUX_PKG_EXTRA_CONFIGURE_ARGS
          }

          termux_step_post_configure() {
              sed -i "s:-I$TERMUX_PREFIX/:-isystem$TERMUX_PREFIX/:g" $TERMUX_PKG_BUILDDIR/build.ninja
          }


          termux_step_pre_configure() {
              LDFLAGS+=" -landroid-glob"

              # Make build log less noisy.
              CFLAGS+=" -Wno-compound-token-split-by-macro"

              # Make sure that perl stuff is reinstalled.
              rm -rf $TERMUX_PREFIX/lib/irssi/perl
          }

          BUILD_SH
          version=$(awk '/^v/ { $0=$1; gsub(/^v/,""); gsub(/-head/,"dev"); gsub(/-/,""); print; exit }' src.irssi.git/NEWS)
          version=$version+g$(git -C src.irssi.git rev-parse --short HEAD)
          sed -i \
              -e "s:@VERSION@:$version:" \
              -e "s:@REVISION@:$GITHUB_RUN_NUMBER:" \
              packages/irssi-an/build.sh
          git -C src.irssi.git tag v$version
      - name: build irssi package
        run: |
          ./scripts/run-docker.sh ./build-package.sh -I irssi-an
      - uses: actions/upload-artifact@v2
        with:
          name: irssi-termux-pkg
          path: output/irssi-an*.deb
