#!/bin/sh -e

[ -n "$GLOBAL_HOMEROS" ] || exit 0
[ -n "$GLOBAL_HOMEROS_FRIEND" ] || exit 0

cat <<EOF > /usr/local/bin/tts-switch-next-eng
#!/bin/sh -e

if /usr/bin/voiceman-enabled mbrola; then
    /usr/bin/voiceman-disable mbrola 
    /usr/bin/voiceman-enable espeak
    /sbin/service voiceman reload
    exit 0
fi

if /usr/bin/voiceman-enabled espeak; then
    /usr/bin/voiceman-disable espeak
    /usr/bin/voiceman-enable mbrola
    /sbin/service voiceman reload
    exit 0
fi
EOF

cat <<EOF > /usr/local/bin/tts-switch-next-rus
#!/bin/sh -e

if /usr/bin/voiceman-enabled rhvoice; then
    /usr/bin/voiceman-disable rhvoice
    /usr/bin/voiceman-enable ru_tts
    /sbin/service voiceman reload
    exit 0
fi

if /usr/bin/voiceman-enabled ru_tts; then
    /usr/bin/voiceman-disable ru_tts
    /usr/bin/voiceman-enable espeak-ru
    /sbin/service voiceman reload
    exit 0
fi

if /usr/bin/voiceman-enabled espeak-ru; then
    /usr/bin/voiceman-disable espeak-ru
    /usr/bin/voiceman-enable rhvoice
    /sbin/service voiceman reload
    exit 0
fi
EOF

/bin/chmod 755 /usr/local/bin/tts-switch-next-eng
/bin/chmod 755 /usr/local/bin/tts-switch-next-rus

cat <<EOF > /root/.emacs.d/site-start.d/hot-tts-switch.el
;; The script to handle hot TTS switching command;

(cond
 (window-system
  (global-set-key [S-f9] (lambda () (interactive)(shell-command-to-string "/usr/local/bin/tts-switch-next-eng")))
(global-set-key [S-f10] (lambda () (interactive)(shell-command-to-string "/usr/local/bin/tts-switch-next-rus"))))
 (:else
  (global-set-key [f19] (lambda () (interactive)(shell-command-to-string "/usr/local/bin/tts-switch-next-eng")))
  (global-set-key [f20] (lambda () (interactive)(shell-command-to-string "/usr/local/bin/tts-switch-next-rus")))))
EOF

cat <<EOF > /home/altlinux/.emacs.d/site-start.d/hot-tts-switch.el
;; The script to handle hot TTS switching command;

(cond
 (window-system
  (global-set-key [S-f9] (lambda () (interactive)(shell-command-to-string "sudo /usr/local/bin/tts-switch-next-eng")))
(global-set-key [S-f10] (lambda () (interactive)(shell-command-to-string "sudo /usr/local/bin/tts-switch-next-rus"))))
 (:else
  (global-set-key [f19] (lambda () (interactive)(shell-command-to-string "sudo /usr/local/bin/tts-switch-next-eng")))
  (global-set-key [f20] (lambda () (interactive)(shell-command-to-string "sudo /usr/local/bin/tts-switch-next-rus")))))
EOF

/bin/chown altlinux:altlinux /home/altlinux/.emacs.d/site-start.d/hot-tts-switch.el