#!/bin/bash

MCDSD=/usr/share/mc/syntax

MCINI=~/.config/mc/ini
MCSDS=".config/mc/mcedit .local/share/mc/syntax"

##################################################################################################
# Modify INI file

if [ -f $MCINI ]; then

	# turn off auto ident
	sed -i 's/^editor_return_does_auto_indent=1$/editor_return_does_auto_indent=0/' $MCINI
	# turn off fake half tabs
	sed -i 's/^editor_fake_half_tabs=1$/editor_fake_half_tabs=0/' $MCINI
	# set tab spacing to 8 chars
	sed -i 's/^editor_tab_spacing=.+$/editor_tab_spacing=8/' $MCINI
	# turn on syntax highlighting
	sed -i 's/^editor_syntax_highlighting=0$/editor_syntax_highlighting=1/' $MCINI

fi


##################################################################################################
# Create syntax highlighting files

if [ -d ${MCDSD} ]; then

if ! [ -f ${MCDSD}/cfg.syntax ]; then
cat << EOF > ${MCDSD}/cfg.syntax
# DOS / Windows INI files

context default cyan
    keyword = brightred

# groups
context [ ] yellow

# comments:
context # \n brown
    spellcheck

context linestart ; \n brown
    spellcheck

context linestart // \n brown
    spellcheck

context exclusive = \n brightcyan
EOF
fi

if ! [ -f ${MCDSD}/yaml.syntax ]; then
cat << EOF > ${MCDSD}/yaml.syntax
caseinsensitive

context default cyan
    keyword #*\n brown
    keyword {{*}} brightred
    keyword linestart *: yellow

context "{{ }}" brightred

context [ ] yellow

context " "\n brightgreen
    keyword {{*}} brightred

context ' '\n brightgreen
    keyword {{*}} brightred
EOF
fi

fi

##################################################################################################
# Re-generate user Syntax file

for mcsdr in $MCSDS; do

	MCSD=~/${mcsdr}

	if ! [ -d ${MCSD} ]; then
		mkdir -p ${MCSD}
	fi

	cp ${MCDSD}/Syntax ${MCSD}/Syntax

	# adding custom file formats

sed -i '/^file \.\\\* unknown$/i\
file ..\\*\\\\.([cC][fF]|[cC][fF][gG]|[cC][oO][nN][fF]|[cC][nN][fF])$ CFG\\sFile\
include cfg.syntax\
\
file ..\\*\\\\.d\\/..\\*$ conf.d\\sFile\
include cfg.syntax\
\
file ..\\*\\\\.(ya?ml|sls|YML|jinja|j2)$ YAML\\sFile\
include yaml.syntax\
' ${MCSD}/Syntax

done
