#!/bin/bash

#$1 is app name
#$2 is trigger

DIRECTORY="$(dirname "$(readlink -f "$(dirname "$0")")")"

function error {
  echo -e "\e[91m$1\e[39m"
  exit 1
}

app="$1"
trigger="$2"

if [ "$(cat "${DIRECTORY}/data/settings/Enable analytics")" == 'Yes' ];then
  #this is used to let Botspot see the daily installs for each app.
  #Botspot created an individual bitly link for each app. When you install an app, the cooresponding bit.ly link is "clicked" by a headless chromium-browser instance.
  #Chromium accesses the bit.ly link, then exits.
  #Allowing analytics does not identify you, or any personal information about you. Botspot can only see the total number of "clicks", as well as the top 3 countries of origin.
  
  bitlylink="https://bit.ly/pi-apps-$trigger-$(echo "$app" | tr -d ' ' | sed 's/[^a-zA-Z0-9]//g')"
  curl -L --user-agent "Pi-Apps Raspberry Pi app store" "$bitlylink" &>/dev/null &
  #echo "clicked $bitlylink" 1>&2
fi
