#!/bin/bash

## Copyright (C) 2012 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

set -x
set -o pipefail

build_all_error_handler() {
   : echo "
${red}${bold}BASH_COMMAND${reset}: $BASH_COMMAND
${red}${bold}ERROR $BASH_SOURCE: | caller: $(caller)${reset}
"
   echo "Press enter to continue or ctrl +c to break or enter to continue."
   read temp
}

trap "build_all_error_handler" ERR

MYDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd "$MYDIR"
cd ..

cd misc
cd paypal_template

paypal_write() {
   trap "build_all_error_handler" ERR
   echo "$1" >> "$file_name"
}

paypal_create() {
   trap "build_all_error_handler" ERR

   if [ "$onetime" = "true" ]; then
      file_name="onetime_${currency}"
   else
      file_name="subscription_${currency}"
   fi

   rm --force "$file_name"

   paypal_write '<!-- Semi automatically generated by https://github.com/Whonix/developer-meta-files/blob/master/usr/bin/dm-paypal-wiki-templates-create script. -->'
   paypal_write '<html>'
   paypal_write '<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">'

   if [ "$onetime" = "true" ]; then
      paypal_write '<input type="hidden" name="cmd" value="_xclick">'
   else
      paypal_write '<input type="hidden" name="cmd" value="_xclick-subscriptions">'
   fi

   paypal_write '<input type="hidden" name="business" value="patrick_schleizer@web.de">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="Whonix Preferred Support">'

   if [ "$onetime" = "true" ]; then
      paypal_write '<input type="hidden" name="button_subtype" value="services">'
   else
      paypal_write '<input type="hidden" name="no_note" value="1">'
   fi

   if [ "$onetime" = "true" ]; then
      paypal_write '<input type="hidden" name="no_note" value="0">'
   else
      paypal_write '<input type="hidden" name="src" value="1">'
   fi

   paypal_write "<input type=\"hidden\" name=\"currency_code\" value=\"$currency\">"

   if [ "$onetime" = "true" ]; then
      paypal_write '<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">'
   else
      paypal_write '<input type="hidden" name="bn" value="PP-SubscriptionsBF:btn_subscribeCC_LG.gif:NonHostedGuest">'
   fi

   paypal_write "\
<table>
<tr><td><input type=\"hidden\" name=\"on0\" value=\"$text\">$text</td></tr><tr><td><select name=\"os0\" style=\"width:300px;\">"

   for level in $levels; do
      if [ "$onetime" = "true" ]; then
         paypal_write "	<option value=\"${level}.00\">${level}.00</option>"
      else
         paypal_write "	<option value=\"${level}.00\">${level}.00</option>"
      fi
   done

   paypal_write '</select> </td></tr>'
   paypal_write '</table>'

   paypal_write "<input type=\"hidden\" name=\"currency_code\" value=\"$currency\">"

   local i="0"
   for level in $levels; do
      paypal_write "<input type=\"hidden\" name=\"option_select${i}\" value=\"${level}.00\">"
      paypal_write "<input type=\"hidden\" name=\"option_amount${i}\" value=\"${level}.00\">"
      if [ "$onetime" = "true" ]; then
         true
      else
         paypal_write "<input type=\"hidden\" name=\"option_period${i}\" value=\"M\">"
         paypal_write "<input type=\"hidden\" name=\"option_frequency${i}\" value=\"1\">"
      fi
      i="$(( i + 1 ))"
   done

   paypal_write '<input type="hidden" name="option_index" value="0">'

   if [ "$onetime" = "true" ]; then
      paypal_write '<input type="image" src="/w/images/c/cc/Btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">'
   else
      paypal_write '<input type="image" src="/w/images/6/69/Btn_subscribeCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">'
   fi

   paypal_write '<img alt="" border="0" src="/w/images/c/cd/Pixel.gif" width="1" height="1">
</form>
</html>'
}

paypal_create_onetime_and_reoccurring_wrapper() {
   trap "build_all_error_handler" ERR

   onetime="true"
   paypal_create

   onetime="false"
   paypal_create
}

paypal_loop() {
   trap "build_all_error_handler" ERR

   currency="USD"

   text="Choose Your Support Level"
   levels="1 2 3 4 5 7 8 10 15 20 30 40 50 60 70 80 90 100 150 200 300 400 500 600 700 800 900 1000 2000 3000 4000 5000 6000 7000 8000 9000 10000"
   #text_onetime="- one time"
   #text_reoccurring="- monthly"

   paypal_create_onetime_and_reoccurring_wrapper

   currency="EUR"
   onetime="true"

   paypal_create_onetime_and_reoccurring_wrapper
}

paypal_loop
