name: Get IP2Location-Lite database

on:
  workflow_dispatch:
  schedule:
    - cron: "20 4 1,15 * *"

env:
  RELEASE_NAME: $(date +%Y%m%d%H%M)

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
      - uses: actions/checkout@v4

      - name: Install dependencies
        run: |
          sudo apt-get update
          sudo apt-get install csvkit

      - name: Get IP2Location-Lite database
        env:
          DOWNLOAD_TOKEN: ${{ secrets.IP2LOCATION_DOWNLOAD_TOKEN }}
        run: |
          curl -sSL "https://www.ip2location.com/download/?token=${DOWNLOAD_TOKEN}&file=DB1LITECSV" -o IP2LOCATION-LITE-DB1.CSV.ZIP
          unzip IP2LOCATION-LITE-DB1.CSV.ZIP -d IP2LOCATION-LITE-DB1
      
      - name: install ip2location-csv-converter script
        run: pip install ip2location-python-csv-converter
    
      - name: Convert database from numeric to cidr
        run: |
          ip2location-csv-converter -cidr -replace IP2LOCATION-LITE-DB1/IP2LOCATION-LITE-DB1.CSV IP2LOCATION-LITE-DB1.CIDR.CSV
    
      - name: Extract Countries IPs
        run: |
          mkdir -p ip2location
          cat IP2LOCATION-LITE-DB1.CIDR.CSV | awk -F"," '{print $2}' | sort -u | sed -e '1d' -e 's/\"//g' > countries.txt
          while read l
          do
            csvgrep -c 2 -m "$l" IP2LOCATION-LITE-DB1.CIDR.CSV | csvcut -c 1 | sed '1d' > "ip2location/$l.txt"
          done < countries.txt

      - name: Push assets to ip2location branch
        run: |
          cd ip2location || exit 1
          git init
          git config --local user.name "github-actions[bot]"
          git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
          git checkout -b ip2location
          git add .
          git commit -m "${{ env.RELEASE_NAME }}"
          git remote add origin "https://${{ github.actor }}:${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}"
          git push -f origin ip2location
