#!/bin/bash

# Bash Currency on-line converter via Google Finance
# Copyright (C) 2008 - Luis León Cárdenas Graide < lcardena [@] nic [.] cl >
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#
# If you think this is useful, you can donate to the author:
# https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=1290721
# Thank you, very much.

declare -a curr_codes=('AED' 'ANG' 'ARS' 'AUD' 'BGN' 'BHD' 'BND' 'BOB' 'BRL' 'BWP' 'CAD' 'CHF' 'CLP' 'CNY' 'COP' 'CSD' 'CZK' 'DKK' 'EEK' 'EGP' 'EUR' 'FJD' 'GBP' 'HKD' 'HNL' 'HRK' 'HUF' 'IDR' 'ILS' 'INR' 'ISK' 'JPY' 'KRW' 'KWD' 'KZT' 'LKR' 'LTL' 'MAD' 'MUR' 'MXN' 'MYR' 'NOK' 'NPR' 'NZD' 'OMR' 'PEN' 'PHP' 'PKR' 'PLN' 'QAR' 'RON' 'RUB' 'SAR' 'SEK' 'SGD' 'SIT' 'SKK' 'THB' 'TRY' 'TTD' 'TWD' 'UAH' 'USD' 'VEB' 'ZAR')
declare -a curr_names=('United Arab Emirates Dirham' 'Netherlands Antillean Gulden' 'Argentine Peso' 'Australian Dollar' 'Bulgarian Lev' 'Bahraini Dinar' 'Brunei Dollar' 'Bolivian Boliviano' 'Brazilian Real' 'Botswana Pula' 'Canadian Dollar' 'Swiss Franc' 'Chilean Peso' 'Chinese Yuan (renminbi)' 'Colombian Peso' 'Serbian Dinar' 'Czech Koruna' 'Danish Krone' 'Estonian Kroon' 'Egyptian Pound' 'Euro' 'Fijian Dollar' 'British Pound' 'Hong Kong Dollar' 'Honduran Lempira' 'Croatian Kuna' 'Hungarian Forint' 'Indonesian Rupiah' 'New Israeli Sheqel' 'Indian Rupee' 'Icelandic Króna' 'Japanese Yen' 'South Korean Won' 'Kuwaiti Dinar' 'Kazakhstani Tenge' 'Sri Lankan Rupee' 'Lithuanian Litas' 'Moroccan Dirham' 'Mauritian Rupee' 'Mexican Peso' 'Malaysian Ringgit' 'Norwegian Krone' 'Nepalese Rupee' 'New Zealand Dollar' 'Omani Rial' 'Peruvian Nuevo Sol' 'Philippine Peso' 'Pakistani Rupee' 'Polish Złoty' 'Qatari Riyal' 'New Romanian Leu' 'Russian Ruble' 'Saudi Riyal' 'Swedish Krona' 'Singapore Dollar' 'Slovenian Tolar' 'Slovak Koruna' 'Thai Baht' 'New Turkish Lira' 'Trinidad and Tobago Dollar' 'New Taiwan Dollar' 'Ukrainian Hryvnia' 'United States Dollar' 'Venezuelan Bolívar' 'South African Rand')

license() {
  echo 'LICENSE:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.


If you think this is useful, you can donate to the author:
https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=1290721
Thank you, very much.
'
}

usage() {
  local exit_code="$1"
  local self=$(basename "$0")
  echo
  echo Bash Currency on-line converter via Google Finance
  echo Version 1.0
  echo
  echo Usage: "$self" currency_from_code currency_to_code '[ quantity ]'
  echo
  echo If ommited, '<quantity>' defaults to 1.
  echo
  echo Currency Codes:
  for n in "${!curr_codes[@]}"
  do
    echo ${curr_codes[$n]}: ${curr_names[$n]}
  done
  echo
  echo COPYRIGHT 2008 '(C)' Luis León Cárdenas Graide '< lcardena [@] nic [.] cl >'
  echo
  license
  exit "$exit_code"
}

from="$1"
to="$2"
money="$3"

if [ -z "$to" ]
then
  echo Missing '<currency_to_code>'
  usage 2
fi

if [ -z "$money" ]
then
  money=1
fi

if [[ ! "$money" =~ ^[0-9]*(\.[0-9]*)?$ ]]
then
  echo Ilegal quantity: "$money"
  usage 2
fi

for var in from to
do
  if [[ ! "${!var}" =~ ^[a-zA-Z]{3}$ ]]
  then
    echo Ilegal Currency Code: "${!var}"
    usage 2
  fi

  declare $var=$(echo "${!var}" | tr a-z A-Z)

  exists=0
  for n in "${!curr_codes[@]}"
  do
    if [ "${curr_codes[$n]}" == "${!var}" ]
    then
      exists=1
      break
    fi
  done

  if [ "$exists" == 0 ]
  then
    echo Currency Code non existant: "${!var}"
    usage 2
  fi
done

if [ "$from" == "$to" ]
then
  echo 1
else
  wget -q -O - 'http://finance.google.com/finance/converter?from='"$from"'&to='"$to"'&a='"$money" |\
    perl -ne 'if(($n)=m!<span class=bld>([^&]+)&nbsp;!){$n=~s/\.(\d*[1-9])0*$/.$1/;print"$n\n"}'
fi


