#! /bin/sh -e
# Boot Parameters to Configuration File

# This script processes the brltty= boot parameters in /proc/cmdline and
# writes a corresponding BRLTTY configuration file to /target/etc/brltty.conf.

. /usr/share/debconf/confmodule

parse()
{
    brailleDriver=""
   brailleDevice=""
   textTable=""

   found=false
   while [ "${#}" -gt 0 ]
   do
      case "${1}"
      in
         "brltty="*)
            found=true
            parameters="${1#*=}"

            number=1
            while [ "${number}" -le 3 ]
            do
               parameter="${parameters%%,*}"
               if [ -n "${parameter}" ]
               then
                  case "${number}"
                  in
                     1)
                        brailleDriver="${parameter}"
                        ;;
                     2)
                        brailleDevice="${parameter}"
                        ;;
                     3)
                        textTable="${parameter}"
                        ;;
                  esac
               fi

               number="`expr ${number} + 1`"
               new_parameters="${parameters#*,}"
               [ "${new_parameters}" != "${parameters}" ] || break
               parameters="${new_parameters}"
            done
            ;;
      esac

      shift
   done

   if "${found}"
   then
      echo "# Created by ${0}"
      [ -z "${brailleDriver}" ] && brailleDriver=auto
      echo "braille-driver ${brailleDriver}"
      [ -z "${brailleDevice}" ] && brailleDevice=usb:
      echo "braille-device ${brailleDevice}"
      [ -n "${textTable}" ] && echo "text-table ${textTable}"
   fi

   true
}

unset DEBIAN_HAS_FRONTEND
unset DEBIAN_FRONTEND
unset DEBCONF_FRONTEND
unset DEBCONF_REDIR

pid=/var/run/brltty.pid

if [ -r $pid ] && kill -0 `cat $pid`; then
    if apt-install brltty 1>&2; then
        file="/proc/cmdline"
        if [ -f $file ]; then
            if grep brltty= $file >/dev/null ; then
                parse `cat "${file}"` >/target/etc/brltty.conf
                sed -i -e 's/^RUN_BRLTTY=.*/RUN_BRLTTY=yes/' /target/etc/default/brltty
            fi
        fi

	# Gnome 2
	/bin/in-target su -s /bin/sh -c "gconftool-2 --type bool --set /desktop/gnome/applications/at/screen_reader_enabled true" Debian-gdm || true

	mkdir -p /target/usr/share/glib-2.0/schemas
	cat > /target/usr/share/glib-2.0/schemas/15_brltty.gschema.override <<END
# Setup by debian-installer (via finish-install.d/07brltty) since
# braille support was enabled during initial installation

[org.gnome.desktop.a11y.applications]
screen-reader-enabled=true

[org.gnome.desktop.sound]
event-sounds=true
END

	in-target dpkg-trigger --no-await /usr/share/glib-2.0/schemas || true
	in-target dpkg --triggers-only -a || true

	db_get passwd/username
	USERNAME="$RET"
	if [ -n "$USERNAME" ]
	then
		# Gnome 2
		/bin/in-target su -s /bin/sh -c "gconftool-2 --type bool --set /desktop/gnome/interface/accessibility true" "$USERNAME" || true
		/bin/in-target su -s /bin/sh -c "gconftool-2 --type bool --set /desktop/gnome/applications/at/visual/startup true" "$USERNAME" || true
		/bin/in-target su -s /bin/sh -c "gconftool-2 --type bool --set /desktop/gnome/sound/event_sounds true" "$USERNAME" || true
	fi
    fi
fi

exit 0
