#! /bin/sh -e

. /usr/share/debconf/confmodule

if CODENAME="$(lsb_release -cs)"; then
	# TODO cjwatson 2006-04-07: wrong for Debian, I think
	db_set mirror/suite "$CODENAME"
	db_set mirror/codename "$CODENAME"
fi

rm -f /target/etc/apt/sources.list
rm -f /target/etc/apt/sources.list.d/dvd.list
PATH="/usr/lib/ubiquity/apt-setup:/usr/lib/ubiquity/choose-mirror:$PATH" \
	OVERRIDE_BASE_INSTALLABLE=1 OVERRIDE_LEAVE_CD_MOUNTED=1 \
	/usr/lib/ubiquity/apt-setup/apt-setup --log-output /target

#Trisquel hack to regenerate ssl keys after installation
if [ -f /target/etc/ssh/ssh_host_rsa_key ]; then
    rm -f /target/etc/ssh/ssh_host_*
    ssh-keygen -q -f /target/etc/ssh/ssh_host_rsa_key -N '' -t rsa
    ssh-keygen -q -f /target/etc/ssh/ssh_host_dsa_key -N '' -t dsa
fi
if [ -d /etc/italc ]
then
    rm -rf /etc/italc/*
    /usr/bin/ica -role teacher -createkeypair >/dev/null
    /usr/bin/ica -role admin -createkeypair >/dev/null
    /usr/bin/ica -role supporter -createkeypair >/dev/null

    rm /target/etc/italc/ -rf
    cp /etc/italc/ /target/etc/ -a

    chown root:admin /target/etc/italc/keys/private/*/key
fi

# Hacks for accessibility
if [ $(su trisquel -c "gsettings get org.mate.applications-at screen-reader-enabled") = "true" ] ||    [ $(su trisquel -c "gsettings get org.gnome.desktop.a11y.applications screen-reader-enabled") = "true" ]
then
  echo "Accessibility profile set to 'blindness', enabling screen reader by default in target system"
  echo "[org.mate.applications-at]
screen-reader-enabled=true

[org.mate.applications-at-visual]
startup=true

[org.mate.desktop.interface]
accessibility=true

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

[org.gnome.desktop.interface]
toolkit-accessibility=true

[com.canonical.unity-greeter]
screen-reader=true
" >> /target/usr/share/glib-2.0/schemas/99_accessibility.gschema.override
  chroot /target glib-compile-schemas /usr/share/glib-2.0/schemas

  if grep -q lightdm /target/etc/passwd; then
    mkdir -p /target/var/lib/lightdm/.cache/lightdm-gtk-greeter/
    echo "[a11y-states]
    reader=true" > /target/var/lib/lightdm/.cache/lightdm-gtk-greeter/state
    chroot /target chown lightdm.lightdm /var/lib/lightdm/ -R
    chmod 750 /target/var/lib/lightdm/
  fi

else 
  echo "Accessibility profile set to none, disabling screen reader by default in target system"
  rm /target/home/*/.local/share/orca/user-settings.conf -f
fi

