#!/bin/sh
###############################################################################
# BRLTTY - A background process providing access to the console screen (when in
#          text mode) for a blind person using a refreshable braille display.
#
# Copyright (C) 1995-2014 by The BRLTTY Developers.
#
# 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; either version 2 of the License, or (at your option) any
# later version. Please see the file LICENSE-GPL 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
}

. "`dirname "${0}"`/brltty-config" || exit "${?}"

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

if [ "${#}" -eq 0 ]
then
   from="${BRLTTY_EXECUTE_ROOT}"
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}${BRLTTY_PROGRAM_DIRECTORY}" "executables"
copyFiles "${from}${BRLTTY_PROGRAM_DIRECTORY}" "${to}${BRLTTY_PROGRAM_DIRECTORY}" "executable" "brltty brltty-config brltty-install"

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

makeDirectory "${to}${BRLTTY_LIBRARY_DIRECTORY}" "library"
echo "Installing library files."
copyContents "${from}${BRLTTY_LIBRARY_DIRECTORY}" "${to}${BRLTTY_LIBRARY_DIRECTORY}"

makeDirectory "${to}${BRLTTY_DATA_DIRECTORY}" "data"
echo "Installing data files."
copyContents "${from}${BRLTTY_DATA_DIRECTORY}" "${to}${BRLTTY_DATA_DIRECTORY}"

echo "Installation complete."
exit 0
