#!/bin/bash

#ident $Id$ (c) 2014 Bruker BioSpin GmbH

XORGCONFIG=/etc/X11/xorg.conf

###
# Some utilities
die() {
    echo -e "ERROR:\n" $@
    exit 1
}

warn() {
    echo -e  "WARNING:\n" $@
}

usage() {
    local myself=`basename $0`
    echo -e "usage:\n $myself [-h | -u]"
    echo " -h       : print this message"
    echo " -u       : enable ctrl-alt-fn again"
}

append_serverflags()
{
    test -f ${XORGCONFIG} && \
	cp -a  ${XORGCONFIG} ${XORGCONFIG}.before_append_serverflags

    cat >> ${XORGCONFIG} <<EOF
Section "ServerFlags"
    Option "DontVTSwitch" "true"
EndSection
EOF
}

remove_serverflags()
{
    grep 'Option.*DontVTSwitch.*true'   ${XORGCONFIG}  > /dev/null && \
	sed -i.before_remove_serverflags '/Section "ServerFlags"/,/EndSection/d' ${XORGCONFIG}
}


while getopts 'hu' opt
do
   case $opt in
       h) usage;  exit 0 ;;
       u) remove_serverflags; exit 0 ;;
       *) usage; exit 1 ;;
   esac
done



grep DontVTSwitch ${XORGCONFIG} > /dev/null || append_serverflags