name: mirror

on:
  push:
    tags:
    - 'i2p-*.*.*'
  workflow_dispatch: # Allows manual triggering

jobs:
  dl:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v4
      with:
        sparse-checkout: |
            changelog.txt
    - name: download release artifacts
      run: |
        sudo apt-get install wget ca-certificates gpg sed curl git
        export I2P_VERSION=$(echo ${{ github.ref_name }} | sed 's|i2p-||g' |sed 's|-.*||')
        echo "$I2P_VERSION"
        export ATTEMPTS=0
        while true; do
        sleep 60m
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pinstall_"$I2P_VERSION"_windows.exe && break
        ATTEMPTS=$(( $ATTEMPTS + 1 ))
        if [ $ATTEMPTS -eq 25 ]; then
            exit 1
        fi
        done
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/I2P-Easy-Install-Bundle-"$I2P_VERSION"-signed.exe
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/I2P-Easy-Install-Bundle-"$I2P_VERSION".exe
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pinstall_"$I2P_VERSION".jar
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pinstall_"$I2P_VERSION".jar.sig
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pinstall_"$I2P_VERSION"_windows.exe
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pinstall_"$I2P_VERSION"_windows.exe.sig
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2psource_"$I2P_VERSION".tar.bz2
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2psource_"$I2P_VERSION".tar.bz2.sig
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pupdate-"$I2P_VERSION".su3
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pupdate-"$I2P_VERSION".su3.torrent
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pupdate.su3
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pupdate_"$I2P_VERSION".zip
        wget -v https://files.i2p-projekt.de/"$I2P_VERSION"/i2pupdate_"$I2P_VERSION".zip.sig'
        echo ""$I2P_VERSION"" > CHANGES.md
        echo "===========" >> CHANGES.md
        echo "" >> CHANGES.md
        echo "" >> CHANGES.md
        curl https://raw.githubusercontent.com/i2p/i2p.newsxml/master/data/entries.html | sed -n '/<article/,/<\/article/p' | sed -n '1,/<\/article>/p' | sed -n '/<\/details>/,$p' | sed '1d' | sed 's/<\/\{0,1\}p>//g' | sed 's|<\/article>||g' >> CHANGES.md
        echo "" >> CHANGES.md
        echo "" >> CHANGES.md
        echo '```' >> CHANGES.md
        head -n 25 changelog.txt >> CHANGES.md
        echo '```' >> CHANGES.md
        echo "" >> CHANGES.md
        echo "" >> CHANGES.md
        echo "## Checksums" >> CHANGES.md
        echo "" >> CHANGES.md
        echo "" >> CHANGES.md
        echo '```' >> CHANGES.md
        sha256sum * >> CHANGES.md
        echo '```' >> CHANGES.md
        echo "" >> CHANGES.md
        echo "" >> CHANGES.md
        echo '```' >> CHANGES.md
        file * >> CHANGES.md
        echo '```' >> CHANGES.md
        echo "" >> CHANGES.md
        echo "" >> CHANGES.md
        cat CHANGES.md
    - name: Upload artifacts
      uses: ncipollo/release-action@v1
      with:
        artifacts: "*"
        skipIfReleaseExists: true
        bodyFile: "CHANGES.md"
