#! /bin/sh

set -e

cfg=/etc/lcdproc.conf

get_cmdln()
{
  cmdln=
  if [ -f $cfg ]; then
    . $cfg
    if [ -n "$device" ]; then cmdln="$cmdln -d $device"; fi
    if [ -n "$driver" ]; then cmdln="$cmdln -l $driver"; fi
    if [ -n "$contrast" ]; then cmdln="$cmdln -c $contrast"; fi
    if [ -n "$modes" ]; then cmdln="$cmdln $modes"; fi
  fi
  echo $cmdln
}

driver_help()
{
    echo " MtxOrb - For the Matrix Orbital 20x4 serial LCD display (LCD2041)"
#    echo " HD44780 - For a Hitachi HD44780-based LCD module"
    echo " curses - For simulating an LCD module with curses"
    echo " text - For display to the terminal"
    echo "Default is MtxOrb."
}

modes_help()
{
    echo " C - CPU usage" 
    echo " G - CPU Graph like Xload"
    echo " T - date, time, uptime, and OS version"
    echo " M - Memory Usage"
    echo " X - X-Load"  
    echo " D - Disk statistics"
    echo " B - APM Battery statistics"
    echo " A - About"
}

config_lcdproc()
{
if [ -f $cfg ]; then
  . $cfg
fi

echo "Configuring lcdproc:"

ok=no
while [ ! "$ok" = "yes" ]; do
  echo; echo "Current configuration: `get_cmdln` (runatboot = $runatboot)"

  echo -n "Do you want to change anything (Y/n)? "; read answer
  if [ "$answer" = "N" -o "$answer" = "n" ]; then
    ok=yes
  else
    echo "Do you want LCDproc to run at system boot [$runatboot]? "; echo -n "> "; read answer
    if [ "$answer" = "no" -o "$answer" = "n" ]; then runatboot="no"; else runatboot="yes"; fi

    answer=
    echo "Where is your display [$device]? "; echo -n "> "; read answer
    if [ -n "$answer" ]; then device="$answer"; fi

    answer=
    echo "What contrast setting should be used [$contast]?"; echo -n "> "; read answer
    if [ -n "$answer" ]; then contrast="$answer"; fi

    answer=
    while [ -z "$answer" ]; do
      echo "What type is your display (or help) [$driver]? "; echo -n "> "; read answer
        case $answer in
        text|MtxOrb|curses)
          driver=$answer
          ;;
        h|help)
          driver_help
          answer=
          ;;
        *)
          echo "Sorry, "$answer" is not a recognised display type."
          echo "Try help"
          answer=
          ;;
        esac
    done

    answer=
    while [ -z "$answer" ]; do
      echo "What modes should be displayed [$modes]? "; echo -n "> "; read answer
      if [ -n "$answer" ]; then
        case $answer in
        h|help)
          modes_help
          answer=
          ;;
        *)
          modes="$answer"
          ;;
        esac
      else
        if [ -n "$modes" ]; then
          answer="$modes"
        fi
      fi
    done
                 
    if [ -n "$device" -a "$device" != "/dev/lcd" ]; then
      rm -f /dev/lcd
      (cd /dev; ln -sf `echo $device|cut -d/ -f3` lcd)
    fi

    cat $cfg \
       | sed \
         -e "s,^[ ]*runatboot=.*,runatboot=$runatboot,g" \
         -e "s,^[ ]*device=.*,device=$device,g" \
         -e "s,^[ ]*driver=.*,driver=$driver,g" \
         -e "s,^[ ]*contrast=.*,contrast=$contrast,g" \
         -e "s,^[ ]*modes=.*\$,modes=\"$modes\",g" \
         > $cfg.new
    mv -f $cfg.new $cfg
                 
  fi
done 
}

config_lcdproc
