#!/bin/bash

if [ $# -gt 0 ] ; then
  TESTCASE="$1"
  echo
  echo "TESTCASE=$TESTCASE"
else
  unset TESTCASE
fi

base8=8
base10=10
base16=16

res1=010 # base8 number
res2=03 # base8 number

echo "res1=$res1" "good 010 base8"
echo "res2=$res2" "good 03 base8"

let "res = res1 + res2 * 3"
echo "res=$res" "good result 17 base10 (res1+3*res2)"

let "res = 10#$res1 + 10#$res2 * 3" # $-sign is needed before both res
echo base10-res=$res "good result 19 base10 (res1+3*res2)"

if [ "$TESTCASE" == '1' ] ; then
  # let: res = base10#010 + base10#03 * 3: syntax error: invalid arithmetic operator (error token is "#010 + base10#03 * 3")
  let "res = base10#$res1 + base10#$res2 * 3" # $-sign is needed before both res
fi
echo base10-res=$res "good result 19 base10 (res1+3*res2)"

if [ "$TESTCASE" == '2' ] ; then
  # without $-signs before both res
  let "res = 10#res1 + 3 * 10#res2"  # let: res = 10#res1: value too great for base (error token is "10#res1")
fi
echo base10-res=$res "good result 19 base10 (res1+3*res2)"

echo res1=$(($base8#$res1)) "good result 8 base10"

echo res1=$((10#$res1)) "good result 10"
echo res1=$(($base10#$res1)) "good result 10"
if [ "$TESTCASE" == '3' ] ; then
  echo $(($base10#res1)) # value too great for base (error token is "10#res1")
fi

let "res = 10#$res1" # good only with $-sign before res
if [ "$TESTCASE" == '4' ] ; then
  let "res = base10#$res1" # syntax error: invalid arithmetic operator (error token is "#010")
fi
if [ "$TESTCASE" == '5' ] ; then
  let "res = base10#res1" # syntax error: invalid arithmetic operator (error token is "#010")
fi

echo "res1=$res1" "good result 010 base8"
if [ "$TESTCASE" == '6' ] ; then
  let "res1 = 10#res1" # value too great for base (error token is "10#res1")
fi
echo base10-res1="$(($base10#$res1))" "good result 10"

echo res1=$((16#$res1)) "good result 16"
if [ "$TESTCASE" == '7' ] ; then
  echo res1=$((base16#$res1)) # base16#010: syntax error: invalid arithmetic operator (error token is "#010")
fi
