#!/bin/bash

export DISPLAY=:0

INPUT="$1"
ID=$2

unquote(){
  echo $1 | sed 's/"//g'
}

echo $INPUT

URL=$(unquote $(echo $INPUT | jq .url))
DOMAINS=`node array-to-lines.js "$(echo $INPUT | jq .third_party_domains)"`

source ./utils.sh
source ./bloater.sh

PREVIEW="TRUE" # set to "TRUE" in order to enable automatic screenshots kept in preview.png 

if [ "$PREVIEW" = "TRUE" ];
then
  (while true; do
     grab_screen_to_public $ID
     sleep 0.7
   done) &
  refresher_pid=$!;
fi

ORIGIN_DOMAIN=$(sed -e 's/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/' <<< "$URL")

bloat_firefox $INPUT
grab bloat_firefox

click 1270 217 # the "trash" icon, so requests from plamienie don't appear in the screenshots

load_website "$URL" "$URL"
sleep 7 # sometimes the consent popup needs a little time
echo "{\"current_action\": \"Strona $ORIGIN_DOMAIN wczytana. Przygotowywanie do analizy...\"}"
grab load_website
open_network_inspector
grab open_network_inspector

declare -a pids;
pids=()


index=0
mkdir -p "/opt/static/$ID"
while IFS= read -r DOMAIN; do
    if [ "$DOMAIN" = "" ]; then
      continue
    fi
    echo "{\"current_action\": \"Skanowanie skryptów z domeny $DOMAIN...\"}"
    network_inspector_search "domain:$DOMAIN " # can filter with more granularity: https://developer.mozilla.org/en-US/docs/Tools/Network_Monitor/request_list#filtering_by_properties

    # grab ni_search
    count=0
    while network_inspector_has_more_entries
    do
      screenshot_and_annotate "$ID" $index "$DOMAIN" "$ORIGIN_DOMAIN" &
      pids+=($!)
      ((index++))
      while network_inspector_headers_need_scrolling
      do
        xdotool mousemove 2400 1000
        echo "SCROLLING DOWN"
        xdotool click 5 click 5 click 5 # scroll down
        sleep 0.1
        screenshot_and_annotate "$ID" $index "$DOMAIN" "$ORIGIN_DOMAIN" &
        pids+=($!)
        ((index++))
      done
      network_inspector_next_entry
      ((count++))
      if [ $count -gt 10 ]; then
        break;
      fi
    done
done <<< "$DOMAINS"

if [ "$PREVIEW" = "TRUE" ];
then
  kill $refresher_pid;
fi

echo "{\"current_action\": \"Kończenie...\"}"

for PID in "${pids[@]}"
do
     wait $PID
done

kill -2 %%;

cleanup


echo "Done!"
