#!/bin/bash

CONFIG_FILE=`echo $HOME`/.config/fcitx/config

if [ ! -f ${CONFIG_DIR} ]; then
    exit;
fi

hotkey=0
TRIGGER_KEY="TriggerKey="
SWITCH_KEY="SwitchKey="
IMSWITCH_KEY="IMSwitchKey="
for line in `cat ${CONFIG_FILE}`
do
	if [ "${line}" == "[Hotkey]" ]; then
		hotkey=1
	elif [ "${line:0:1}" == "[" ]; then
		hotkey=0
	fi
	if [ "${hotkey}" -eq "1" ]; then
		if [[ ${line} =~  ${TRIGGER_KEY} ]]; then
			if [ "$1" = "--enable" ]; then
				sed -i 's/^#*TriggerKey=[a-zA-Z0-9_ \t]*/#TriggerKey=/g' $CONFIG_FILE
			elif [ "$1" = "--disable" ]; then
				sed -i 's/^#*TriggerKey=[a-zA-Z0-9_ \t]*/TriggerKey=/g' $CONFIG_FILE
			fi
		fi
		if [[ ${line} =~ ${SWITCH_KEY} ]]; then
			if [ "$1" = "--enable" ]; then
				sed -i 's/^#*SwitchKey=[a-zA-Z0-9_ \t]*/SwitchKey=L_SHIFT/g' $CONFIG_FILE
			elif [ "$1" = "--disable" ]; then
				sed -i 's/^#*SwitchKey=[a-zA-Z0-9_ \t]*/SwitchKey=Custom/g' $CONFIG_FILE
			fi
		fi
		if [[ ${line} =~ ${IMSWITCH_KEY} ]]; then
			if [ "$1" = "--enable" ]; then
				sed -i 's/^#*IMSwitchKey=[a-zA-Z0-9_ \t]*/IMSwitchKey=True/g' $CONFIG_FILE
			elif [ "$1" = "--disable" ]; then
				sed -i 's/^#*IMSwitchKey=[a-zA-Z0-9_ \t]*/IMSwitchKey=False/g' $CONFIG_FILE
			fi
		fi
	fi
done
fcitx-remote -r
