#!/bin/bash
#***************************************************************************************************
#                                                                                                  *
# siliconBrainInstall.bash: install a siliconBrain package.                                        *
#                                                                                                  *
#***************************************************************************************************

#***************************************************************************************************
#                                                                                                  *
#     Copyright (C) 2003, 2004 Joerg Kunze                                                               *
#                                                                                                  *
#     This file is part of siliconBrain.                                                           *
#                                                                                                  *
#     siliconBrain 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.                                                          *
#                                                                                                  *
#     siliconBrain 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 the Free Software                                  *
#     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                    *
#                                                                                                  *
#***************************************************************************************************
siliconBrainRelease='$siliconBrainRelease: 0.1.1 $'
siliconBrainRcsIdentifier='$Id: siliconBrainInstall.bash,v 1.24 2004/12/14 23:31:26 joerg Exp $'
siliconBrainSaveStamp='$siliconBrainSaveStamp: 2004/12/14 22:27:07, Joerg Kunze$'

. distribution/data/releaseInformation

#***************************************************************************************************
#                                                                                                  *
# help:                                                                                            *
#                                                                                                  *
#***************************************************************************************************
function help {
   echo "You have to specify the directory to which you like to install"
   echo "the package $siliconBrainPackageName."
   echo "Either you set the variable siliconBrainInstallPath (because"
   echo "you constantly compile and install this package) or you give"
   echo "it as first parameter of this script (because it is part of an"
   echo "automated installation)."
}

#***************************************************************************************************
#                                                                                                  *
# getInstallPath: this function get interactivly some data from the user.                          *
#                                                                                                  *
#***************************************************************************************************
function getInstallPath {
   local target
   doInstall=true

   #---------------------------------------------#
   # we only work if someone is sitting at a TTY #
   #---------------------------------------------#
   if tty -s; then
      echo "IMPORTANT: you don't have to install a siliconBrain package to use it."
      echo "           you can use it from just where it is."
      echo "           choose 'doNotInstall' if that's your way."
      echo
      echo "Enter the path whereto you like to install $siliconBrainPackageName:"

      PS3='where to install? '
      select target in "doNotInstall" ~ "/usr/local" "other"; do
         if [ $target = "doNotInstall" ]; then
            target=$PWD
            doInstall=false
            break
         fi
         if [ $target ]; then
            if [ $target = "other" ]; then
               echo "OK, enter your choice:"
               read -e target
               target=${target%/}
            fi
            break
         fi
      done
      siliconBrainInstallPath=$target
      if $doInstall; then
         PS3='which directory name to install into? '
         select siliconBrainInstallDirectory
         in "$siliconBrainPackageName" "${siliconBrainPackageName}_$(<RELEASE)"; do
            if [ -n "$siliconBrainInstallDirectory" ]; then
               break
            fi
         done
      else
         siliconBrainInstallDirectory=temporary
      fi

   else
      #------------------------#
      # no TTY, sorry, no data #
      #------------------------#
      echo "stdin is not a tty; specify installation path:"
      help
      exit 42
   fi
}

#***************************************************************************************************
#                                                                                                  *
# checkInstallTarget:                                                                              *
#                                                                                                  *
#***************************************************************************************************
function checkInstallTarget {
   if [ ! -e "$siliconBrainInstallPath" ]; then
      echo "$siliconBrainInstallPath does not exist."
      exit 42
   fi

   if [ ! -d "$siliconBrainInstallPath" ]; then
      echo "$siliconBrainInstallPath is not a directory."
      exit 42
   fi

   if [ ! -w "$siliconBrainInstallPath" ]; then
      echo "$siliconBrainInstallPath has no write permission."
      exit 42
   fi

   if [ ! -x "$siliconBrainInstallPath" ]; then
      echo "$siliconBrainInstallPath cannot cd'ed into (no execute permission)."
      exit 42
   fi
}

#***************************************************************************************************
#                                                                                                  *
# main:                                                                                            *
#                                                                                                  *
#***************************************************************************************************
# siliconBrainInstallPath       = /usr/local
# siliconBrainInstallDirectory  = %p_%r

doInstall=true

#--------------------------------------#
# greetings: print version information #
#--------------------------------------#
{
   echo
   echo 'This is siliconBrainInstall:'
   echo 'This is the siliconBrain way to install a package.'
   echo
   echo $siliconBrainRelease
   echo $siliconBrainRcsIdentifier
   echo $siliconBrainSaveStamp
   echo
} >&2

if [ "$1" = "-i" ] || [ "$1" = "--interactive" ]; then

   #------------------------------------------------#
   # get installation target from user interactivly #
   #------------------------------------------------#
   getInstallPath >&2

#-------------------------------------------------------------#
# test for command line options; they will overwrite env vars #
#-------------------------------------------------------------#
elif [ -n "$1" ]; then
   siliconBrainInstallPath=$1
   siliconBrainInstallDirectory=${2:-'%p'}

#---------------------------#
# test for environment vars #
#---------------------------#
elif [ -n "$siliconBrainInstallPath" ]; then
   siliconBrainInstallDirectory=${siliconBrainInstallDirectory:-'%p'}

else
   echo "By calling \"siliconBrainInstall\" without any parameters, you will NOT install the
package. And you don't have to.
IMPORTANT: you can use $siliconBrainPackageName just from where it is.
You just have to include the last two lines printed, into your invironment." >&2

   siliconBrainInstallDirectory=distribution
   siliconBrainInstallPath=$PWD
   doInstall=false
fi

#-------------------------------------#
# replace placeholders in installDir: #
# %p = packageName                    #
# %r = releaseNumber                  #
#-------------------------------------#
siliconBrainInstallDirectory=${siliconBrainInstallDirectory//\\%p/$siliconBrainPackageName}
siliconBrainInstallDirectory=${siliconBrainInstallDirectory//\\%r/$release}
siliconBrainInstallDirectory=${siliconBrainInstallDirectory//\\%R/$releaseWithUnderscores}

#---------------------------------------#
# if an installation is required, do it #
#---------------------------------------#
if $doInstall ; then
   checkInstallTarget >&2

   target=$siliconBrainInstallPath/$siliconBrainInstallDirectory

   echo "installing into $target ..."

   #------------------------#
   # clean target directory #
   #------------------------#
   rm --force --recursive $target

   #-----------------#
   # copy everything #
   #-----------------#
   cp --recursive --verbose distribution $target >&2

fi >&2

{
   echo
   echo "And finally include the following two lines, wherever you"
   echo "what to use this package:"
   echo
} >&2

#----------------------------------------------------------------------------------------#
# These following two lines are intended to be placed into a .profile or similar of the  #
# later user of a package. These two lines are the only two, which are not redirected to #
# stderr (2). So a user can save them by doing: siliconBrainInstall > myProfileExtension #
#----------------------------------------------------------------------------------------#
echo "export ${siliconBrainPackageName}Path=$siliconBrainInstallPath/$siliconBrainInstallDirectory"
echo ". \$${siliconBrainPackageName}Path/setEnvironment"

# $Log: siliconBrainInstall.bash,v $
# Revision 1.24  2004/12/14 23:31:26  joerg
# published for new release 0.2.3
#
# Revision 1.23  2004/12/14 23:17:05  joerg
# published for new release 0.2.2
#
# Revision 1.22  2004/12/14 22:42:23  joerg
# allFiles: all sources have a Log CVS keyword at the end now.
#