#!/bin/sh
###############################################################################
# BRLTTY - A background process providing access to the Linux console (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2003 by The BRLTTY Team. All rights reserved.
#
# BRLTTY comes with ABSOLUTELY NO WARRANTY.
#
# This is free software, placed under the terms of the
# GNU General Public License, as published by the Free Software
# Foundation.  Please see the file COPYING for details.
#
# Web Page: http://mielke.cc/brltty/
#
# This software is maintained by Dave Mielke <dave@mielke.cc>.
###############################################################################

# Installation script for binary BRLTTY distributions.
# Usage: brltty-install dest-prefix [source-prefix]

programName="${0##*/}"
programMessage()
{
   echo "${programName}: ${1}"
}
syntaxError()
{
   programMessage "${1}"
   exit 2
}

execute()
{
   "${@}"
   returnCode="${?}"
   [ "${returnCode}" -eq 0 ] || exit "${returnCode}"
   return 0
}
makeDirectory()
{
   if [ ! -a "${1}" ]
   then
      echo "Creating ${2} directory: ${1}"
      execute mkdir -p -- "${1}"
   elif [ ! -d "${1}" ]
   then
      syntaxError "not a directory: ${1}"
   fi
}
copyContents()
{
   execute cp -pR -- "${1}/"* "${2}"
}
copyFiles()
{
   for f in $4
   do
      if [ -f "${1}/${f}" ]
      then
         echo "Installing ${3}: ${2}/${f}"
         execute cp -p -- "${1}/${f}" "${2}"
      elif [ -a "${1}/${f}" ]
      then
         programMessage "not a file: ${1}/${f}"
      else
         programMessage "${3} not found: ${1}/${f}"
      fi
   done
}

if [ "${#}" -eq 0 ]
then
   syntaxError "missing destination directory."
fi
to="${1}"
shift

if [ "${#}" -eq 0 ]
then
   from=""
elif [ "${#}" -eq 1 ]
then
   from="${1}"
   if [ ! -d "${from}" ]
   then
      syntaxError "source file hierarchy not found: ${from}"
   fi
   if [ ! -d "${from}" ]
   then
      syntaxError "source is not a directory."
   fi
else
   syntaxError "too many parameters."
fi

if [ "${to}" = "${from}" ]
then
   syntaxError "source and destination are the same."
fi
makeDirectory "${to}" "destination"

makeDirectory "${to}/bin" "executables"
copyFiles "${from}/bin" "${to}/bin" "executable" "brltty brltty-install"

conf="brltty.conf"
if [ -f "${from}/etc/${conf}" ]
then
   makeDirectory "${to}/etc" "configuration"
   copyFiles "${from}/etc" "${to}/etc" "configuration file" "${conf}"
else
   programMessage "no configuration file: ${from}/etc/${conf}"
fi

makeDirectory "${to}/lib/brltty" "library"
echo "Installing library files."
copyContents "${from}/lib/brltty" "${to}/lib/brltty"

makeDirectory "${to}/etc/brltty" "data"
echo "Installing data files."
copyContents "${from}/etc/brltty" "${to}/etc/brltty"

echo "Installation complete."
exit 0
