#! /bin/sh

# ******************************************************************************
# Header
#
# Project:      AVRtoolbox
#
# This file:    install
# Version:      V0.1
#
# Description:  Install script for AVRtoolbox module
#
# Copyright:    (C) 2002 by Michael Baeuerle <micha@hilfe-fuer-linux.de>
# 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 2 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, write to:
#               Free Software Foundation Inc.
#               59 Temple Place, Suite 330
#               Boston MA  02111-1307  USA
#
# Language:     sh
# Style rules:  -
#
# Written for:  Platform:       all
#               OS:             UNIX
# Tested with:  Interpreter:    GNU bash (Version 2.3.0)
#               Platform:       IA32-SMP (2x PentiumPro)
#               OS:             GNU/Linux (Kernel version: 2.2.26)
# Do not work:  -
#
# Changelog:
# V0.0          2002-05-06  Michael Baeuerle
#               Ask for installation directory, check if manager is present
#                and install module
#
# V0.1          2005-08-28  Michael Baeuerle
#               No longer evaluate 'test' expressions in subshells
#               Use 'dialog' for user interface if available
#               Function 'leave_arena' added
#               Temporary file 'TMPFILE' added
#               Constant 'DIALOG_TITLE' added
#               Variables 'TMP_STRING' and 'INSTALL_STATE' added
#               If the specified directory does not exist, ask again
#               Check for AVRsuite version matches module
#               Check for absolute path added
#               Remove 'TMPFILE' on exit
#               Display license
#               Variables 'MODULE_VERSION' added
#
#
# To do:        -
# ******************************************************************************


# ******************************************************************************
# Code
# ******************************************************************************

# ------------------------------------------------------------------------------
# Configuration

MODULE_NAME="NEdit"
MODULE_VERSION="V0R1"
LOGFILE="/tmp/AVRtoolbox_install.log"
TMPFILE="/tmp/AVRtoolbox.tmp"
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Constants

DIALOG_TITLE="AVRtoolbox module '$MODULE_NAME $MODULE_VERSION' install script\
 (V0.1)"
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Variables

TMP_STRING=""
INSTALL_STATE="ERR"
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Functions

leave_arena () {
   echo >>$LOGFILE
   echo "Exiting ..." >>$LOGFILE
   echo >>$LOGFILE
   if [ "$USE_DIALOG" = "YES" ]; then
      clear
      echo
      echo "Logfile: '$LOGFILE'"
   fi
   rm $TMPFILE >/dev/null 2>&1
   echo
   echo "Exiting ..."
   echo
   exit 1
}
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Display startup message

clear
echo
echo "'$MODULE_NAME' module for AVRtoolbox"
echo "(Use with AVRsuite V0.x toolbox manager only!)"
echo
echo $DIALOG_TITLE
echo

echo "Logfile: '$LOGFILE'"
echo >$LOGFILE
echo "'$MODULE_NAME' module for AVRtoolbox" >>$LOGFILE
echo "Install log" >>$LOGFILE
echo >>$LOGFILE
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Check whether dialog is available

echo -n "Searching for 'dialog' ... " >>$LOGFILE
dialog -foo >/dev/null 2>&1
if ( [ $? -eq 127 ] ); then
   echo "Not found" >>$LOGFILE
   USE_DIALOG=NO
else
   echo "Found" >>$LOGFILE
   USE_DIALOG=YES
fi
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Display license

if [ "$USE_DIALOG" = "YES" ]; then
   dialog --title "License" \
    --backtitle "$DIALOG_TITLE" \
    --textbox "./$MODULE_NAME/LICENSE" 21 78
fi
# ------------------------------------------------------------------------------

until [ "$INSTALL_STATE" = "OK" ]; do

# ------------------------------------------------------------------------------
# Ask for installation directory

echo -n "Ask for installation directory ... " >>$LOGFILE
if [ "$USE_DIALOG" = "YES" ]; then
   # dialog version
   dialog --title "AVRtoolbox installation directory" \
    --backtitle "$DIALOG_TITLE" \
    --inputbox "Specify the absolute path where AVRtoolbox is installed" \
               8 78 "$TMP_STRING" \
    2>$TMPFILE
   if [ $? -eq 0 ]; then
      echo "OK" >>$LOGFILE
      echo -n "Installation directory: " >>$LOGFILE
      read <$TMPFILE
      INSTALL_DIR=$REPLY
      TMP_STRING=$INSTALL_DIR
   else
      echo "Aborted" >>$LOGFILE
      leave_arena
   fi
else
   # Command line version
   echo "Specify the absolute path where AVRtoolbox is installed: "
   echo -n "-> "
   read
   echo "OK" >>$LOGFILE
   echo -n "Installation directory: " >>$LOGFILE
   INSTALL_DIR=$REPLY
fi
echo "$INSTALL_DIR" >>$LOGFILE
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Check whether specified directory exist

echo -n "Check for absolute path ... " >>$LOGFILE
if  ! [ "$USE_DIALOG" = "YES" ]; then
   echo -n "Check for absolute path ... "
fi
if  [ "`echo "$INSTALL_DIR" | head -c 1`" != "/" ]; then
   if [ "$USE_DIALOG" = "YES" ]; then
      # dialog version
      dialog --title "Error" \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "Specified path does not start with '/'" 5 44 \
       2>>$LOGFILE
   else
      # Command line version
      echo
      echo "ERROR: Specified path does not start with '/'!"
   fi
   echo "Failed" >>$LOGFILE
else
   echo "OK" >>$LOGFILE
   if ! [ "$USE_DIALOG" = "YES" ]; then
      echo "OK"
   fi

   # ...........................................................................

   echo -n "Check whether specified directory exist ... " >>$LOGFILE
   if  ! [ "$USE_DIALOG" = "YES" ]; then
      echo -n "Check whether specified directory exist ... "
   fi
   if  ! [ -d "$INSTALL_DIR" ]; then
      if [ "$USE_DIALOG" = "YES" ]; then
         # dialog version
         dialog --title "Error" \
          --backtitle "$DIALOG_TITLE" \
          --msgbox "Specified directory not found" 5 40 \
          2>>$LOGFILE
      else
         # Command line version
         echo
         echo "ERROR: Directory '$INSTALL_DIR' not found!"
      fi
      echo "Not found" >>$LOGFILE
   else
      echo "OK" >>$LOGFILE
      if ! [ "$USE_DIALOG" = "YES" ]; then
         echo "OK"
      fi
      INSTALL_STATE=OK
   fi
fi
# ------------------------------------------------------------------------------

done  # Ask again if not an absolute path or directory does not exist

# ------------------------------------------------------------------------------
# Check for AVRsuite to be present and have matching major version number

echo -n "Searching for AVRsuite ... " >>$LOGFILE
if ! [ "$USE_DIALOG" = "YES" ]; then
   echo -n "Searching for AVRsuite ... "
fi
if ! [ -f "$INSTALL_DIR/AVRsuite/DETECT" ]; then
   if [ "$USE_DIALOG" = "YES" ]; then
      # dialog version
      dialog --title "Searching for AVRsuite ..." \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "AVRsuite not found in specified directory" 5 45 \
       2>>$LOGFILE
   else
      # Command line version
      echo
      echo "ERROR: AVRsuite not found in specified directory!"
   fi
   echo "Not found" >>$LOGFILE
   leave_arena
fi
echo "OK" >>$LOGFILE
if ! [ "$USE_DIALOG" = "YES" ]; then
   echo "OK"
fi

# ..............................................................................

echo -n "Checking AVRsuite version ... " >>$LOGFILE
if ! [ "$USE_DIALOG" = "YES" ]; then
   echo -n "Checking AVRsuite version ... "
fi
grep -q AVRsuite <$INSTALL_DIR/AVRsuite/DETECT
if [ $? -ne 0 ]; then
   if [ "$USE_DIALOG" = "YES" ]; then
      # dialog version
      dialog --title "Error" \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "AVRsuite installation corrupt" 5 36 \
       2>>$LOGFILE
   else
      # Command line version
      echo
      echo "ERROR: AVRsuite installation corrupt!"
   fi
   echo "Failed" >>$LOGFILE
   leave_arena
fi
{ read ; read ; } <$INSTALL_DIR/AVRsuite/DETECT
if [ "$REPLY" != "0" ]; then
   if [ "$USE_DIALOG" = "YES" ]; then
      # dialog version
      dialog --title "Error" \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "AVRsuite installation with major version V$REPLY found" \
                6 36 \
       2>>$LOGFILE
      dialog --title "Hint" \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "This module must be used with AVRsuite V0" \
                6 36 \
       2>>$LOGFILE
   else
      # Command line version
      echo
      echo "ERROR: AVRsuite installation with major version V$REPLY found!"
      echo "This module must be used with AVRsuite V0"
   fi
   echo "Failed" >>$LOGFILE
   leave_arena
fi
echo "OK" >>$LOGFILE
if ! [ "$USE_DIALOG" = "YES" ]; then
   echo "OK"
fi
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Install module

echo -n "Installing module ... " >>$LOGFILE
if ! [ "$USE_DIALOG" = "YES" ]; then
   echo -n "Installing module ... "
fi
if ! [ -d "./$MODULE_NAME" ]; then
   if [ "$USE_DIALOG" = "YES" ]; then
      # dialog version
      dialog --title "Error" \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "Data not found" 5 20 \
       2>>$LOGFILE
      dialog --title "Hint" \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "The installation script must be run as './install'" 6 36 \
       2>>$LOGFILE
   else
      # Command line version
      echo
      echo "ERROR: Data not found! (called from other directory?)"
   fi
   echo "Failed" >>$LOGFILE
   leave_arena
elif [ -d "$INSTALL_DIR/$MODULE_NAME" ]; then
   if [ "$USE_DIALOG" = "YES" ]; then
      # dialog version
      dialog --title "Error" \
       --backtitle "$DIALOG_TITLE" \
       --msgbox "Module already exist" 5 26 \
       2>>$LOGFILE
   else
      # Command line version
      echo
      echo "ERROR: Module already exist!"
   fi
   echo "Failed" >>$LOGFILE
   leave_arena
else
   cp -a ./$MODULE_NAME $INSTALL_DIR >> $LOGFILE 2>&1
   if ! [ $? -eq 0 ]; then
      if [ "$USE_DIALOG" = "YES" ]; then
         # dialog version
         dialog --title "Error" \
          --backtitle "$DIALOG_TITLE" \
          --msgbox "Cannot copy data (Check '$LOGFILE')" 6 60 \
          2>>$LOGFILE
      else
         # Command line version
         echo
         echo "ERROR: Cannot copy data! (Check '$LOGFILE')"
      fi
      echo "Failed" >>$LOGFILE
      leave_arena
   fi
fi
echo "OK" >>$LOGFILE
if ! [ "$USE_DIALOG" = "YES" ]; then
   echo "OK"
fi
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Success

echo >>$LOGFILE
echo "Success." >>$LOGFILE

if [ "$USE_DIALOG" = "YES" ]; then
   dialog --title "Success" \
    --backtitle "$DIALOG_TITLE" \
    --msgbox "Installation successfully completed" 5 46 \
    2>>$LOGFILE
    clear
else
   echo
   echo "Installation successful."
fi
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# Clean up

rm $TMPFILE >/dev/null 2>&1
echo >>$LOGFILE
echo "Exiting ..." >>$LOGFILE
echo >>$LOGFILE
echo
echo "Exiting ..."
echo
exit 0
# ------------------------------------------------------------------------------


# EOF
