#!/bin/sh
# If you are looking to change accessibility profile settings, plesae look in
# bin/casper-a11y-enable.

PREREQ=""
DESCRIPTION="Configuring accessibility options..."

prereqs()
{
	echo "$PREREQ"
}

# Args: $1 - The profile name
setup_profile()
{
	if [ -d /root/usr/share/a11y-profile-manager/profiles/$1 ] &&
	   [ -x /root/usr/bin/a11y-profile-manager ] &&
	   [ -x /root/usr/bin/dconf ]; then
		mkdir -p /root/tmp/a11y-profile-casper.d
		chroot /root /usr/bin/a11y-profile-manager -s $1 -D /tmp/a11y-profile-casper.d/01-profile
		chroot /root /usr/bin/dconf compile /tmp/dconf.$USERNAME /tmp/a11y-profile-casper.d
		mkdir -p /root/home/$USERNAME/.config/dconf
		mv /root/tmp/dconf.$USERNAME /root/home/$USERNAME/.config/dconf/user
		chroot /root chown $USERNAME.$USERNAME -R /home/$USERNAME/.config
		rmdir /root/tmp/a11y-profile-casper.d
	fi
}

case $1 in
# get pre-requisites
	prereqs)
		prereqs
	       exit 0
	       ;;
esac

. /scripts/casper-functions

log_begin_msg "$DESCRIPTION"

for x in $(cat /proc/cmdline); do
	case $x in
		# Lesser Visual Impairment
		access=v1)
			setup_profile high-contrast
                        ;;
		# Moderate Visual Impairment
		access=v2)
			setup_profile magnifier
                        ;;
		# Blindness
		access=v3)
			NO_A11Y_DESKTOP_FILE=1
			setup_profile blindness
                        ;;
		# Braille
		braille=ask)
			setup_profile braille
                        ;;
		# Minor Motor Difficulties
		access=m1)
			setup_profile keyboard-modifiers
                        ;;
		# Motor Difficulties - pointing devices
		access=m2)
			setup_profile onscreen-keyboard
                        ;;
	esac
done

# Write out a desktop file that will be triggered if the screen-reader-enabled
# gsettings key is enabled, to set up the screen reader profile. This key
# is enabled if the user launches orca with a shortcut key. We don't want
# to write out the file if the user is running oem-config, or boots directly
# into ubiquity, where there is code to deal with the setting of the profile.
if [ -z "$NO_A11Y_DESKTOP_FILE" ] &&
   [ -x /root/usr/bin/a11y-profile-manager ]; then
	PROFILE_NAME=$(basename /root/usr/share/a11y-profile-manager/profiles/*blindness)
	cat <<EOF > /root/etc/xdg/autostart/screen-reader-profile.desktop
[Desktop Entry]
Type=Application
Name=Screen Reader Accessibility Profile
Exec=/usr/bin/a11y-profile-manager -s $PROFILE_NAME
NoDisplay=true
AutostartCondition=GSettings org.gnome.desktop.a11y.applications screen-reader-enabled
X-GNOME-AutoRestart=false
OnlyShowIn=GNOME;MATE;Unity;
EOF
fi
log_end_msg
