on:
  workflow_dispatch:
  push:
    branches: [ release, main ]
jobs:

  init:
    runs-on: [ "shell" ]
    outputs:
      ip: ${{ steps.config.outputs.ip }}
      id: ${{ steps.config.outputs.id }}
      hostname: ${{ steps.config.outputs.hostname }}
      cores: ${{ steps.config.outputs.cores }}
      memory: ${{ steps.config.outputs.memory }}
      swap: ${{ steps.config.outputs.swap }}
      disk: ${{ steps.config.outputs.disk }}
      boot: ${{ steps.config.outputs.boot }}
      mount: ${{ steps.config.outputs.mount }}
    steps:
      - name: Checkout repository
        uses: tasks/checkout@v6
      - name: Set configuration
        id: config
        run: |
          for env in container{.env,.local.env}; do [[ -f "$env" ]] && source "$env" || true; done
          echo "hostname=config" >> $GITEA_OUTPUT
          echo "ip=$IP" >> $GITEA_OUTPUT
          echo "id=$ID" >> $GITEA_OUTPUT
          echo "cores=$CORES" >> $GITEA_OUTPUT
          echo "memory=$MEMORY" >> $GITEA_OUTPUT
          echo "swap=$SWAP" >> $GITEA_OUTPUT
          echo "disk=$DISK" >> $GITEA_OUTPUT
          echo "boot=$BOOT" >> $GITEA_OUTPUT
          echo "mount=$MOUNT" >> $GITEA_OUTPUT

  base:
    runs-on: [ "shell" ]
    needs: init
    steps:
      - id: init
        uses: main/base/.gitea/workflows@main
        with:
          ip: ${{ needs.init.outputs.ip }}
          id: ${{ needs.init.outputs.id }}
          hostname: ${{ needs.init.outputs.hostname }}
          cores: ${{ needs.init.outputs.cores }}
          memory: ${{ needs.init.outputs.memory }}
          swap: ${{ needs.init.outputs.swap }}
          disk: ${{ needs.init.outputs.disk }}
          boot: ${{ needs.init.outputs.boot }}
          mount: ${{ needs.init.outputs.mount }}
          share: true
        if: ${{ gitea.ref == 'refs/heads/release' }}

  config:
    runs-on: [ "shell" ]
    needs: [ init, base ]
    steps:
      - name: Checkout repository
        uses: tasks/checkout@v6
        with:
          path: 'config'
          submodules: true
      - name: Configure container
        env:
          ip: ${{ needs.init.outputs.ip }}
          id: ${{ needs.init.outputs.id }}
          login: ${{ vars.LOGIN }}
          password: ${{ vars.PASSWORD }}
        run: |
          git -C config submodule update --remote --recursive
          tar -c config -cz | ssh -o StrictHostKeyChecking=no -i "/share/.keys/${id}" "config@${ip}" 'sudo tar xz -C /tmp
            CONFIG_FILE="-j /tmp/config/local/config.json"; [ -f /tmp/config/local/config.local.json ] && CONFIG_FILE="-j /tmp/config/local/config.local.json"
            sudo -E IP="'"${ip}"'" ID="'"${id}"'" LOGIN="'"${login}"'" PASSWORD="'"${password}"'" PWD=/tmp/config \
              cinc-client --local-mode $CONFIG_FILE --config-option cookbook_path="/tmp/config" -o config'

  share:
    runs-on: [ "shell" ]
    needs: [ init, base ]
    steps:
      - name: Checkout repository
        uses: tasks/checkout@v6
        with:
          path: 'config'
          submodules: true
      - name: Configure container
        env:
          ip: ${{ needs.init.outputs.ip }}
          id: ${{ needs.init.outputs.id }}
          mount: ${{ needs.init.outputs.mount }}
          login: ${{ vars.LOGIN }}
          password: ${{ vars.PASSWORD }}
        run: |
          tar -c config -cz | ssh -o StrictHostKeyChecking=no -i "/share/.keys/${id}" "config@${ip}" 'sudo tar xz -C /tmp
            sudo -E IP="'"${ip}"'" ID="'"${id}"'" LOGIN="'"${login}"'" PASSWORD="'"${password}"'" \
              cinc-client --local-mode --config-option cookbook_path="[\"/tmp/config\", \"/tmp/config/libs\"]" -o share'
    if: ${{ gitea.ref == 'refs/heads/release' }}