name: Build ISO

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

jobs:
  build:
    runs-on: ubuntu-latest
    container:
      image: archlinux:latest
      options: --privileged

    steps:
    - uses: actions/checkout@v4

    - name: Install dependencies
      run: |
        pacman -Syu --noconfirm --needed \
          archiso \
          git \
          squashfs-tools \
          wget \
          imagemagick \
          sudo \
          base-devel \
          python-pip

    - name: Create build user
      run: |
        useradd -m builder
        echo "builder ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
        chown -R builder:builder .

    - name: Prepare
      run: sudo -u builder ./prepare.sh

    - name: Build ISO
      run: ./mkarchiso -v "."

    - name: Upload ISO artifact
      uses: actions/upload-artifact@v4
      with:
        name: Antergos-NeXT-ISO
        path: out/

    - name: Upload to Internet Archive
      if: github.ref == 'refs/heads/master'
      env:
        IA_ACCESS_KEY: ${{ secrets.IA_ACCESS_KEY }}
        IA_SECRET_KEY: ${{ secrets.IA_SECRET_KEY }}
      run: |
        pip install --break-system-packages internetarchive
        mkdir -p ~/.config/internetarchive
        printf '[s3]\naccess = %s\nsecret = %s\n' "$IA_ACCESS_KEY" "$IA_SECRET_KEY" > ~/.config/internetarchive/ia.ini
        chmod 600 ~/.config/internetarchive/ia.ini
        ISO=$(ls out/*.iso | head -1)
        ia upload antergos-next "$ISO" \
          --metadata="title:Antergos NeXT Live ISO" \
          --metadata="creator:Antergos NeXT" \
          --metadata="date:$(date +%Y-%m-%d)" \
          --metadata="year:$(date +%Y)" \
          --metadata="description:Antergos NeXT live installation ISO based on Arch Linux" \
          --metadata="subject:Antergos NeXT; Arch Linux; live ISO; Linux distribution" \
          --metadata="language:eng" \
          --metadata="mediatype:software" \
          --metadata="collection:opensource"
