#!/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: install-brltty dest-prefix [source-prefix]

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

execute()
{
   "${@}"
   returnCode="${?}"
   if [ "${returnCode}" -ne 0 ]
   then
      exit "${returnCode}"
   fi
   return 0
}
makeDirectory()
{
   if [ ! -a "${1}" ]
   then
      echo -n "Creating ${2} directory..."
      execute mkdir -p -- "${1}"
      echo "done."
   elif [ ! -d "${1}" ]
   then
      syntaxError "not a directory: ${1}"
   fi
}

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

if [ "${#}" -eq 0 ]
then
   from=""
elif [ "${#}" -eq 1 ]
then
   from="${1}"
   if [ ! -a "${from}" ]
   then
      syntaxError "source does not exist."
   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}/usr/bin" "executables"
echo -n "Installing executables..."
for executable in brltty install-brltty
do
   execute cp -p -- "${from}/usr/bin/${executable}" "${to}/usr/bin"
done
unset executable
echo "done."

makeDirectory "${to}/usr/lib64/brltty" "library"
echo -n "Installing library files..."
execute cp -pR "${from}/usr/lib64/brltty" "${to}/usr/lib64/brltty"
echo "done."

makeDirectory "${to}/etc/brltty" "data"
echo -n "Installing data files..."
cp -pR "${from}/etc/brltty" "${to}/etc/brltty"
echo "done."

echo "Installation complete."
exit 0
