#!/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>.
###############################################################################

version=4.3
subversion=x
apiversion=0.5.6
whole_version=$version-$subversion
autohotkey="/c/Program Files/AutoHotkey"
nsis="/c/Program Files/NSIS"
configure_line="--quiet --prefix=/ --enable-relocatable-install --without-flite --without-mikropuhe --without-speechd --without-swift --without-theta --without-viavoice --disable-i18n"
README_head="\
This is an unofficial build of brltty $version + a few fixes for windows.

You should probably read README.txt and doc/README.Windows.txt for information
on brltty.

For a very quick startguide:

- use the BRLTTY configurator (brlttycnf.exe) or uncomment by hand the proper
  lines from etc/brltty.conf.
- For Python support (e.g. for NVDA), run Brlapi-$apiversion.win32.exe
- for sighted users, use the xw braille driver to get a virtual braille box."
README_foot="
If you are getting troubles, please run run-debug.bat and send us the debug.log
and brltty.conf files.

More documentation can be found in README.txt and doc/

=============================
Technical notes on this build

- brltty was compiled from version $version plus some path fixes using mingw32
  and ./configure $configure_line
- to make life easier to windows users, Programs/brlapi_server.c was modified
  to not use authentication by default: BRLAPI_DEFAUTH turned into "none",
  and only local sockets were enabled: ":0+127.0.0.1:0" turned into ":0"
- *usb0* come from libusb-win32 1.2.1.0 available on
  http://libusb-win32.sourceforge.net/
- braille.dll comes from http://libbraille.sourceforge.net/
- python bindings are provided in Brlapi-$apiversion.win32.exe
- C bindings are provided in include/ and lib/: a .lib file is provided for
  linking in e.g. Visual C.  Then you can just ship
  bin/brlapi${apiversion%.*}.dll along your .exe application.
"
. "`dirname "${0}"`/prologue.sh"

[ "$MSYSTEM" = "MINGW32" ] || programError 1 "This script is for MinGW32 only"

[ "${#}" -eq 0 ] && syntaxError "missing source root."
sourceRoot="${1}"
shift
cd "${sourceRoot}" || exit "${?}"
sourceRoot="`pwd`"

if [ "${#}" -eq 0 ]
then
   archiveName="`basename "${sourceRoot}"`"
else
   archiveName="${1}"
   shift
fi

if [ "${#}" -eq 0 ]
then
   archiveFile="${archiveName}.zip"
else
   archiveFile="${1}"
   shift
fi
[ "${archiveFile#/}" = "${archiveFile}" ] && archiveFile="${initialDirectory}/${archiveFile}"

[ "${#}" -eq 0 ] || syntaxError "too many parameters."

temporaryDirectory=/tmp/brltty-mkwin
rm -fr ${temporaryDirectory}
mkdir -p ${temporaryDirectory}

set -e

buildRoot="${temporaryDirectory}/build"
cp -p -r -- "${sourceRoot}" "${buildRoot}"
cd "${buildRoot}"

installRoot="${temporaryDirectory}/${archiveName}"
"./configure" $configure_line
make -s
"${autohotkey}/Compiler/Ahk2Exe.exe" //in brlttycnf.ahk //out brlttycnf.exe
make -s install INSTALL_ROOT="${installRoot}" JAVA_JAR_DIR=/lib JAVA_JNI_DIR=/lib
make -s -C Drivers/BrlAPI/WindowEyes we-dynamic-library-windows
cp Bindings/Python/dist/Brlapi-$apiversion.win32.exe "${installRoot}"
cp brlttycnf.exe "${installRoot}"
cp Drivers/BrlAPI/WindowEyes/webrloem109.dll "${installRoot}"

todos() {
  sed -e 's/'$'\033''\[[0-9]*m//g' -e 's/$/'$'\015'/ < "$1" > "$2"
}

todos "${sourceRoot}/LICENSE-GPL" "${installRoot}/LICENSE-GPL.txt"
todos "${sourceRoot}/LICENSE-LGPL" "${installRoot}/LICENSE-LGPL.txt"
todos "${sourceRoot}/README" "${installRoot}/README.txt"

mkdir "${installRoot}/doc"
for i in BrlAPIref Manual-BRLTTY Manual-BrlAPI
do
  for file in `(cd ${sourceRoot}/Documents ; find "$i" -type f ! -name \*.3)`
  do
    mkdir -p "${installRoot}/doc/`dirname "$file"`"
    todos "${sourceRoot}/Documents/$file" "${installRoot}/doc/$file"
  done
done
for i in ChangeLog HISTORY TODO README.windows
do
  todos "${sourceRoot}/Documents/$i" "${installRoot}/doc/$i.txt"
done
todos "${sourceRoot}/Drivers/BrlAPI/WindowEyes/README" "${installRoot}/doc/WindowEyes.txt"
todos "${sourceRoot}/Drivers/BrlAPI/WindowEyes/braille.ini" "${installRoot}/doc/braille.ini"

mkdir "${installRoot}/doc/BrailleDrivers"
for i in `cd Drivers/Braille ; ls` ; do
  [ -r "Drivers/Braille/$i/README" ] && todos "${sourceRoot}/Drivers/Braille/$i/README" "${installRoot}/doc/BrailleDrivers/$i.txt"
done

todos "${buildRoot}/Documents/brltty.conf" "${installRoot}/etc/brltty.conf"
todos "${buildRoot}/Tables/README-ATB" "${installRoot}/etc/brltty/README-ATB.txt"
todos "${buildRoot}/Tables/README-CTB" "${installRoot}/etc/brltty/README-CTB.txt"
todos "${buildRoot}/Tables/README-DOTS" "${installRoot}/etc/brltty/README-DOTS.txt"
todos "${buildRoot}/Tables/README-TTB" "${installRoot}/etc/brltty/README-TTB.txt"

for i in `find ${installRoot}/share/man -type f -name \*.3 -o -name \*.1`
do
  target="${i/%[13]}"txt
  groff -T ascii -mandoc 2> /dev/null < "$i" | sed -e 's/'$'\033''\[[0-9]*m//g' > "$target"
  [ -s "$target" ] || rm -f "$i" "$target"
done

sed -e 's/$/'$'\015''/' > "${installRoot}/README.first.txt" << EOF
$README_head
- to register BRLTTY as a service so that it gets started automatically at boot,
  run install.bat. To unregister it, run uninstall.bat
- If your braille device is USB,
  * if you have installed your manufacturer's driver, you should rather
    select its virtual COM port if it provides one, else select "USB:" to use
    libusb-win32's filter,
  * if you do not want or can not install your manufacturer's driver, you can
    select "USB:" here and install libusb-win32's driver.
- to install the libusb-win32 filter, fetch it from
  http://libusb-win32.sourceforge.net/
- to install libusb-win32 drivers, right-click on bin/brltty.inf
  and select install
- If your braille device is serial, is connect through a serial to USB
  converter, just select the proper COM port.  Make sure to select the proper
  braille display as incorrect probing seems to possibly brick some devices.
- If your braille device is bluetooth, you can either use the windows bluetooth
  facilities to create a virtual COM port which you can then direct brltty to,
  or you can configure by hand the braille-device line in the brltty.conf file.
$README_foot
EOF

sed -e 's/$/'$'\015''/' > "${installRoot}/install.bat" << EOF
@echo off
.\bin\brltty -I
net start BrlAPI
EOF

sed -e 's/$/'$'\015''/' > "${installRoot}/uninstall.bat" << EOF
@echo off
net stop BrlAPI
.\bin\brltty -R
EOF

sed -e 's/$/'$'\015''/' > "${installRoot}/run-debug.bat" << EOF
@echo off
echo Running brltty in debugging mode.  When done, close this window (use the
echo alt-space menu for instance) and see %CD%\debug.log
.\bin\brltty -l debug -n -e > debug.log 2>&1
EOF

cp ${sourceRoot}/Hotplug/brltty.inf "${installRoot}/bin"
echo "not yet" > "${installRoot}/bin/brltty.cat"
echo "not yet" > "${installRoot}/bin/brltty_x64.cat"
cp "$SYSTEMROOT/system32/drivers/libusb0.sys" "${installRoot}/bin"
cp "$SYSTEMROOT/system32/drivers/libusb0_x64.sys" "${installRoot}/bin"
cp "$SYSTEMROOT/system32/libusb0.dll" "${installRoot}/bin"
cp "$SYSTEMROOT/system32/libusb0_x64.dll" "${installRoot}/bin"
cp "$SYSTEMROOT/system32/braille.dll" "${installRoot}/bin"
cp "$SYSTEMROOT/system32/msvcr90.dll" "${installRoot}/bin"
(cd "${installRoot}/lib" ; lib /def:brlapi.def /name:brlapi-0.5.dll /out:brlapi.lib )
cp /mingw/bin/libiconv-2.dll "${installRoot}/bin"
cp /mingw/bin/curses2.dll "${installRoot}/bin"
rm -f "${installRoot}/bin/brltty-config"
rm -f "${installRoot}/bin/brltty-install"
rm -f "${installRoot}/etc/brlapi.key"
rmdir -p "${installRoot}/lib/brltty/rw" 2> /dev/null || true

cd ..
rm -f "${archiveFile}"
zip -q -A -r "${archiveFile}" "${archiveName}"

sed -e 's/$/'$'\015''/' > "${installRoot}/README.first.txt" << EOF
$README_head
$README_foot
EOF

cp "${buildRoot}/brltty.nsi" .
cp "${buildRoot}/nsistrings.txt" "${installRoot}/"
"${nsis}/makensis" -DVERSION=${whole_version} brltty.nsi
rm -f "${initialDirectory}/brltty-win-${whole_version}.exe"
mv brltty-win-${whole_version}.exe "${initialDirectory}/brltty-win-${whole_version}.exe"

exit 0
