name: "Update data sources"
on:
  workflow_dispatch:
  schedule:
    - cron: '0 12 * * 2'
jobs:
  update-sources:
    runs-on: debian-latest
    steps:
      - name: Install deps
        run: apt-get install --update --assume-yes sudo git

      - name: Install nix
        uses: https://github.com/cachix/install-nix-action@v31
        with:
          extra_nix_config: |
            experimental-features = nix-command flakes

      - name: Git config
        run: |
          git config --global user.name 'Gitea Clanker'
          git config --global user.email 'gitea@pvv.ntnu.no'

      - name: Checkout repository
        uses: actions/checkout@v2

      - name: Update jmdict
        run: |
          nix run .#update-jmdict

          if ! git diff --quiet; then
            OLD_VERSION=$(git show HEAD:./jmdict/version.txt)
            NEW_VERSION=$(cat ./jmdict/version.txt)
            git add jmdict
            git commit -m "JMdict: $OLD_VERSION -> $NEW_VERSION"
            git push
          else
            echo "No changes to commit"
          fi

      - name: Update kanjidic2
        run: |
          nix run .#update-kanjidic2

          if ! git diff --quiet; then
            OLD_VERSION=$(git show HEAD:./kanjidic2/version.txt)
            NEW_VERSION=$(cat ./kanjidic2/version.txt)
            git add kanjidic2
            git commit -m "KANJIDIC2: $OLD_VERSION -> $NEW_VERSION"
            git push
          else
            echo "No changes to commit"
          fi

      - name: Update radkfile
        run: |
          nix run .#update-radkfile

          if ! git diff --quiet; then
            OLD_VERSION=$(git show HEAD:./radkfile/version.txt)
            NEW_VERSION=$(cat ./radkfile/version.txt)
            git add radkfile
            git commit -m "RADKFILE: $OLD_VERSION -> $NEW_VERSION"
            git push
          else
            echo "No changes to commit"
          fi

      - name: Update housou-kinshi-yougo
        run: |
          nix run .#update-housou-kinshi-yougo

          if ! git diff --quiet; then
            OLD_VERSION=$(git show HEAD:./housou-kinshi-yougo/version.txt)
            NEW_VERSION=$(cat ./housou-kinshi-yougo/version.txt)
            git add housou-kinshi-yougo
            git commit -m "housou-kinshi-yougo: $OLD_VERSION -> $NEW_VERSION"
            git push
          else
            echo "No changes to commit"
          fi
