#!/bin/bash


if [ "$1" == "--help" ] || [ "$1" == "-h" ]
then
echo Usage: $0 [option]
echo without any option - this script will enable gnome-a11y-featuers
echo see also: /etc/a11y.conf
echo options:
echo --help -h - print this help
echo status - displays the a11y status of the system followed by the config settings
echo off - disable gnome-a11y-features
exit 0
fi

if [ "$1" == "livecd" ]
then
  AUTOSTART_DIR="/usr/share/gnome/autostart"
  HOMEDIR="/home/linux"
else
  AUTOSTART_DIR="$HOME/.config/autostart"
  HOMEDIR="$HOME"
fi

ORCA_DESKTOP_FILE="/usr/share/applications/orca.desktop"
ORCA_DEFAULTS="/usr/share/a11y/orca"


# check if we want to disable a11y
if [ "$1" == "off" ]
then
  gconftool-2 -s -t boolean /desktop/gnome/interface/accessibility false 2>/dev/null
  if [ $? != 0 ]
  then
    echo "error disabling a11y with gconftool-2"
    echo "Please check if $HOME/.gconf is configured correctly!"
  fi
  
  if [ -f $AUTOSTART_DIR/orca.desktop ]
  then
    rm -f $AUTOSTART_DIR/orca.desktop
  fi
  
  if [ -d $HOMEDIR/.orca ]
  then
    rm -rf $HOMEDIR/.orca
  fi
  exit 0
fi

. /etc/a11y.conf


# check a11y status
if [ "$1" == "status" ]
then

  # check orca autostart
  if [ -f $AUTOSTART_DIR/orca.desktop ]
  then
    echo "orca autostart yes $ORCA_AUTOSTART"
    else
      echo "orca autostart no $ORCA_AUTOSTART"
  fi
  
  if [ `gconftool-2 -g /desktop/gnome/interface/accessibility` == "true" ]
  then
    echo "a11y enabled yes $A11Y_ENABLE"
    else
      echo "a11y enabled no $A11Y_ENABLE"
  fi
exit 0
fi
    
 
# check /etc/a11y.conf and enable a11y features
if [ "$CHECK_GRUB_F9" == "yes" ]
then
  F9=`grep -o "braille=1" /proc/cmdline`
  
  if [ "$F9" != "braille=1" ]
  then
  echo "a11y was not enabled at grub menu"
  exit 0
  fi

fi
  
  
if [ "$A11Y_ENABLE" != "yes" ]
then
  echo "A11Y_ENABLE is not \"yes\" - nothing to do!"
  exit 0
  else
    if [ "$1" == "livecd" ]
    then
      gconftool-2 --direct --config-source \
                  xml:readwrite:/etc/gconf/gconf.xml.defaults --type bool --set \
                  /desktop/gnome/interface/accessibility true
    else
      gconftool-2 -s -t boolean /desktop/gnome/interface/accessibility true 2>/dev/null
    fi
    
    if [ $? != 0 ]
    then
      echo "unable to turn on a11y features with gconftool-2"
      echo "Please check if $HOME/.gconf is configured correctly!"
      exit 1
    fi
fi

if [ "$ORCA_AUTOSTART" == "yes" ]
then
  if [ ! -d $AUTOSTART_DIR ]
  then
    mkdir -p $AUTOSTART_DIR
  fi
  
  if [ -f $ORCA_DESKTOP_FILE ]
  then
    cp $ORCA_DESKTOP_FILE $AUTOSTART_DIR
    else
      echo "$ORCA_DESKTOP_FILE - file not found"
      exit 2
  fi
fi


# copy default orca settings into $HOME
if [ ! -d $ORCA_DEFAULTS ]
then
  echo "orca defaults $ORCA_DEFAULTS not found"
  exit 3
  else
    if [ -d $HOME/.orca ]
    then
      rm -rf $HOMEDIR/.orca
    fi
    
    cp -r $ORCA_DEFAULTS $HOMEDIR/.orca
    # check if brld is running and enable braille support
    if [ ! -z `pidof brld` ]
    then
      sed -i s/"enableBraille = False"/"enableBraille = True"/ $HOMEDIR/.orca/user-settings.py
    fi
    
    if [ "$1" == "livecd" ]
    then
      chown -R linux $HOMEDIR/.orca
    fi
    
fi

echo "a11y features successfully enabled"
exit 0


  


