#!/bin/bash

keypad_fn()
{
  tput cup 2
  echo -e "\033[33G7      8      9"
  echo -e "\033[33G4      5      6"
  echo -e "\033[33G1      2      3"
  echo -e "\033[33G+      0      -"
  echo -e "\033[33G*      /      ="
  echo -e "\033[33G   C       Q"
  echo
}

highlight_loop_fn()
{
  until [ -z "$key" ]
  do
    highlight_fn
    tput cup $row $column
    column=$((column+1))
    input=$input$key
    unset key
    read -n 1 key
    highlight_loop_fn
  done
}

highlight_fn()
{
  if   [ "$key" = 0 ]; then
       keypad_fn
       tput cup 5 39
       echo -e "\033[41m0\033[0m"
  elif [ "$key" = 1 ]; then
       keypad_fn
       tput cup 4 32
       echo -e "\033[41m1\033[0m"
  elif [ "$key" = 2 ]; then
       keypad_fn
       tput cup 4 39
       echo -e "\033[41m2\033[0m"
  elif [ "$key" = 3 ]; then
       keypad_fn
       tput cup 4 46
       echo -e "\033[41m3\033[0m"
  elif [ "$key" = 4 ]; then
       keypad_fn
       tput cup 3 32
       echo -e "\033[41m4\033[0m"
  elif [ "$key" = 5 ]; then
       keypad_fn
       tput cup 3 39
       echo -e "\033[41m5\033[0m"
  elif [ "$key" = 6 ]; then
       keypad_fn
       tput cup 3 46
       echo -e "\033[41m6\033[0m"
  elif [ "$key" = 7 ]; then
       keypad_fn
       tput cup 2 32
       echo -e "\033[41m7\033[0m"
  elif [ "$key" = 8 ]; then
       keypad_fn
       tput cup 2 39
       echo -e "\033[41m8\033[0m"
  elif [ "$key" = 9 ]; then
       keypad_fn
       tput cup 2 46
       echo -e "\033[41m9\033[0m"
  elif [ "$key" = "+" ]; then
       keypad_fn
       tput cup 5 32
       echo -e "\033[41m+\033[0m"
  elif [ "$key" = "-" ]; then
       keypad_fn
       tput cup 5 46
       echo -e "\033[41m-\033[0m"
  elif [ "`echo $key | wc -m`" = "`ls | wc -m`" ]; then
       keypad_fn
       tput cup 6 32
       echo -e "\033[41m*\033[0m"
  elif [ "$key" = "/" ]; then
       keypad_fn
       tput cup 6 39
       echo -e "\033[41m/\033[0m"
  elif [ "$key" = "C" -o "$key" = "c" ]; then
       keypad_fn
       tput cup 7 35
       echo -e "\033[41mC\033[0m"
  elif [ "$key" = "Q" -o "$key" = "q" ]; then
       keypad_fn
       tput cup 7 43
       echo -e "\033[41mQ\033[0m"
  fi
}

calc_fn()
{
  clear
  echo -e "\033[33G\033[41m*    CCALC    *\033[0m"
  echo
  keypad_fn
  row=9
  column=`echo "$no. $num" | wc -m`
  tput cup $row
  echo -n "$no. $num"
  until [ $row -eq 24 ]
  do
    read -n 1 key
    highlight_loop_fn
    if [ -z $key ]; then
       keypad_fn
       tput cup 6 46
       echo -e "\033[41m=\033[0m"
       tput cup $row $column
    fi
    input=$num$input
    if   echo "$input" | grep -q ^quit; then
         clear
         exit 0
    elif echo "$input" | grep -q ^clear; then
         unset input
         row=24
         no=1
    elif [ -z "$num$input" ]; then
         row=$((row+1))
         no=$((no+1))
    else
         sum=`echo "scale=2; $input" | bc`
         unset input
         tput cup $row $column
         echo -n "= $sum"
         row=$((row+1))
         no=$((no+1))
         stty -echo
         read -n 1 key
         stty echo
         case $key in
           [0-9])
             num=$key
             ;;
           +|-|\*|/)
             num=$sum$key
             ;;
           *)
             unset num
             ;;
         esac
    fi
      highlight_fn
      column=`echo "$no. $num" | wc -m`
      tput cup $row
      echo -n "$no. $num"
  done
  calc_fn
}

unset num
no=1
calc_fn
