# Build Script for Nginx Ultimate Bad Bot Blocker using GHA
name: CI

defaults:
  run:
    shell: bash

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master
  schedule:
      - cron: '0 10 * * *'
      - cron: '0 22 * * *'

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Install Software
        run: |
          sudo apt -y install dos2unix


      - name: Configure Nginx - Stage 1
        shell: bash
        run: |
          echo "export TERM=xterm" >> ~/.bashrc
          source ~/.bashrc
          sudo systemctl enable nginx
          sudo systemctl start nginx
          sudo nginx -t
          sudo systemctl reload nginx
          ls -la /etc/nginx/sites-available
          ls -la /etc/nginx/sites-enabled
          cat /etc/nginx/sites-available/default
          ./dev-tools/generate-files.sh
          ./dev-tools/install-nginx-1.sh

      - name: Configure Nginx & Generate Blocker Files - Stage 2
        shell: bash
        run: |
          php ./dev-tools/generate-regex-format-referrers.php
          php ./dev-tools/generate-regex-format-referrers-whitelist-test.php
          ./dev-tools/update-bad-ips.sh
          ./dev-tools/generate-blacklist.sh
          ./dev-tools/generate-blacklist-testing-of-changes.sh
          ./dev-tools/modify-config-readme-files.sh
          ./dev-tools/prepare-robots-input.sh
          ./dev-tools/generate-robots.sh
          ./dev-tools/generate-google-disavow.sh
          php ./dev-tools/generate-google-exclude.php
          ./dev-tools/generate-files.sh
          ./dev-tools/install-nginx-1.sh

      - name: Test Blocker - Stage 1
        shell: bash
        run: |
          ./dev-tools/test-blocker-false-positives.sh
          ./dev-tools/test-blocker-quick.sh
          ./dev-tools/test-blocker-badwords.sh
          ./dev-tools/test-blocker-whitelist-domains.sh
          ./dev-tools/test-blocker-whitelist-ips.sh
          ./dev-tools/test-blocker-rate-limiting.sh
          ./dev-tools/test-blocker-whitelist.sh

      - name: Test Blocker - Stage 2
        shell: bash
        run: |
          ./dev-tools/install-nginx-2.sh
          ./dev-tools/test-nginx-2.sh

      - name: Test Blocker - Stage 3
        shell: bash
        run: |
          ./dev-tools/install-nginx-3.sh
          ./dev-tools/test-nginx-3.sh

      - name: Test setup-ngxblocker - Various Distro Nginx Conf Files Test
        shell: bash
        run: |
          ./dev-tools/test-setupngxblocker.sh

      #- name: Run Beta Tests
      #  shell: bash
      #  run: |
      #    ./dev-tools/install-nginx-1.sh
      #    ./dev-tools/beta-install-nginx-testing-of-changes.sh
      #    ./dev-tools/beta-test-blocker-false-positives.sh
      #    ./dev-tools/beta-test-blocker.sh
      #    ./dev-tools/beta-test-blocker-badwords.sh
      #    ./dev-tools/beta-test-blocker-whitelist-domains.sh
      #    ./dev-tools/beta-test-blocker-whitelist-ips.sh
      #    ./dev-tools/beta-test-blocker-rate-limiting.sh
      #    ./dev-tools/beta-test-blocker-whitelist.sh

      - name: Deploy Latest Package
        shell: bash
        run: |
          ./dev-tools/modify-files-and-commit.sh

      - name: Debug
        shell: bash
        run: | 
          cat /etc/nginx/nginx.conf
          sudo systemctl list-units --type=service --all
        if: always()