#!/bin/bash

set -e

source /usr/share/mediawiki-shell/common

## example:
#[[ -v SOURCE_WIKI_URL ]] || SOURCE_WIKI_URL='https://www.whonix.org/w'

## These variables should be set by the calling script as environment variables.
check_vars_exist SOURCE_WIKI_URL wiki_script

[[ -v wiki_error_on_fetch ]] || wiki_error_on_fetch=true
[[ -v mw_api_start_from ]] || mw_api_start_from=""

## XXX
WIKI_URL="$SOURCE_WIKI_URL"

source /usr/share/mediawiki-shell/wiki-config

echo "$0: INFO: TMPFOLDER         : $TMPFOLDER"
echo "$0: INFO: SOURCE_WIKI_URL: $SOURCE_WIKI_URL"

# ## XXX
DESTINATION_WIKI_URL="$SOURCE_WIKI_URL"

## Not required for public wiki.
#mw-logout
#mw-login

allpages_file="${TMPFOLDER}/allpages.txt"

rm -f "$allpages_file"
log_run env QUERY_TYPE=allpages SOURCE_WIKI_URL="$SOURCE_WIKI_URL" mw-all-pages "$allpages_file"

test -r "$allpages_file"

counter_total="$(awk 'END {print NR}' "$allpages_file")"

counter_currently=0
counter_chunk=0
chunk_max_size=10
pid_list=""
start_from_here="not-yet"

while IFS=$'\n' read -r item_from_all_pages ; do
   counter_currently=$(( $counter_currently + 1 ))
   backup_page_item=$(set_backup_page_item "$item_from_all_pages")
   backup_filename_item=$(set_backup_filename_item "$backup_page_item")

   ## XXX
   wiki_page_item="$backup_page_item"
   wiki_article_to_fetch="$backup_page_item"

   counter_chunk=$(( $counter_chunk + 1 ))

   ## TODO:
   #mw_api_start_from="Network_Time_Synchronization"

   if [ "$mw_api_start_from" = "" ]; then
      start_from_here=true
   else
      if [ "$backup_page_item" = "$mw_api_start_from" ]; then
         start_from_here=true
      fi
   fi

   if [ ! "$start_from_here" = "true" ]; then
      echo "SKIP: $counter_currently / $counter_total | counter_chunk: $counter_chunk | $backup_page_item | $backup_filename_item"
      continue
   fi
   echo "DO: $counter_currently / $counter_total | counter_chunk: $counter_chunk | $backup_page_item | $backup_filename_item"

   if [ "$backup_page_item" = "Changelog" ]; then
      continue
   fi

   TMPFOLDER_SEPARATE="$TMPFOLDER/separate/$counter_currently"
   mkdir --parents "$TMPFOLDER_SEPARATE"

   log_run_background \
      env \
         counter_currently="$counter_currently" \
         counter_chunk="$counter_chunk" \
         TMPFOLDER="$TMPFOLDER_SEPARATE" \
         WIKI_URL="$SOURCE_WIKI_URL" \
         backup_page_item="$backup_page_item" \
         backup_filename_item="$backup_filename_item" \
         wiki_page_item="$wiki_page_item" \
         wiki_article_to_fetch="$wiki_article_to_fetch" \
         mw-retry-wrapper $wiki_script

   pid_list+=" $pid"

   if [ "$counter_chunk" -ge "$chunk_max_size" ] || [ "$counter_currently" -ge "$counter_total" ] ; then
      counter_chunk=0
      echo "$0: INFO: wait $pid_list"

      for check_pid in $pid_list ; do
         if ! wait "$check_pid" ; then
            error "check_pid '$check_pid' failed!"
         fi
      done

      pid_list=""
   fi

done < "$allpages_file"
