#!/bin/sh
#Script to generate NML highlight script for Notepad++
#Based on original list script by Terken, nml_syntax.sh, 2011-07-28 17:45
#Modified by FooBar to generate four word list instead of two and build a np++ xml file directly
#Modified slightly by planetmaker to run from the place as in the repo
#Version 1.2, 11 March 2013

#INSTRUCTIONS
#The script must be run in a folder that contains a "nml" folder with the nml repository.

relpath=".."
output_file="nml_notepadpp.xml"

#Create list of blocks, functions and units
cat $relpath/nml/unit.py                    | grep "^units\['" | awk -F\' '{ print $2 }' >  temp_1.txt
cat $relpath/nml/tokens.py                  | grep "^    '"    | awk -F\' '{ print $2 }' >> temp_1.txt
cat $relpath/nml/expression/functioncall.py | grep "^    '"    | awk -F\' '{ print $2 }' >> temp_1.txt
cat $relpath/nml/actions/action2layout.py   | grep "^    '"    | awk -F\' '{ print $2 }' >> temp_1.txt
#No easy way to get action14 stuff
echo "int bool" >> temp_1.txt

#Create list of properties and variables
cat $relpath/nml/actions/action2var_variables.py | grep "^    '"         | awk -F\' '{ print $2 }' >  temp_2.txt
cat $relpath/nml/actions/action0properties.py    | grep "^    '"         | awk -F\' '{ print $2 }' >> temp_2.txt
cat $relpath/nml/actions/action2layout.py        | grep "^            '" | awk -F\' '{ print $2 }' >> temp_2.txt
cat $relpath/nml/actions/action3_callbacks.py    | grep "^    '"         | awk -F\' '{ print $2 }' >> temp_2.txt
#No easy way to get action14 stuff
echo "grfid name desc version min_compatible_version type bit min_value max_value def_value names" >> temp_2.txt

#Create list of features
cat $relpath/nml/ast/general.py     | grep "^    '" | awk -F\' '{ print $2 }' >  temp_3.txt
cat $relpath/nml/actions/action3.py | grep "^    '" | awk -F\' '{ print $2 }' >> temp_3.txt
cat $relpath/nml/ast/switch.py      | grep "^    '" | awk -F\' '{ print $2 }' >> temp_3.txt

#Create list of callbacks constants
cat $relpath/nml/global_constants.py    | grep "^    '" | awk -F\' '{ print $2 }' >  temp_4.txt
cat $relpath/nml/actions/action5.py     | grep "^    '" | awk -F\' '{ print $2 }' >> temp_4.txt
cat $relpath/nml/actions/action12.py    | grep "^    '" | awk -F\' '{ print $2 }' >> temp_4.txt
cat $relpath/nml/actions/actionB.py     | grep "^    '" | awk -F\' '{ print $2 }' >> temp_4.txt
cat $relpath/nml/actions/real_sprite.py | grep "^    '" | awk -F\' '{ print $2 }' >> temp_4.txt
cat $relpath/nml/ast/basecost.py        | grep "^    '" | awk -F\' '{ print $2 }' >> temp_4.txt

#Sort the files and remove duplicates
sort temp_1.txt | uniq | awk '{ printf "%s ", $0 }' > words_1.txt
sort temp_2.txt | uniq | awk '{ printf "%s ", $0 }' > words_2.txt
sort temp_3.txt | uniq | awk '{ printf "%s ", $0 }' > words_3.txt
sort temp_4.txt | uniq | awk '{ printf "%s ", $0 }' > words_4.txt

#Define parts of np++ xml file
string1="<NotepadPlus>
    <UserLang name=\"nml\" ext=\"nml pnml\">
        <Settings>
            <Global caseIgnored=\"no\" />
            <TreatAsSymbol comment=\"no\" commentLine=\"yes\" />
            <Prefix words1=\"no\" words2=\"no\" words3=\"no\" words4=\"no\" />
        </Settings>
        <KeywordLists>
            <Keywords name=\"Delimiters\">000000</Keywords>
            <Keywords name=\"Folder+\">{</Keywords>
            <Keywords name=\"Folder-\">}</Keywords>
            <Keywords name=\"Operators\">( ) , : ; [ ]</Keywords>
            <Keywords name=\"Comment\">1/* 2*/ 0//</Keywords>
			<!-- blocks, functions and units -->
            <Keywords name=\"Words1\">"
string2="</Keywords>
			<!-- properties and variables-->
            <Keywords name=\"Words2\">"
string3="</Keywords>
			<!-- features -->
            <Keywords name=\"Words3\">"
string4="</Keywords>
			<!-- callbacks and constants -->
            <Keywords name=\"Words4\">"
string5="</Keywords>
        </KeywordLists>
        <Styles>
            <WordsStyle name=\"DEFAULT\" styleID=\"11\" fgColor=\"000000\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"FOLDEROPEN\" styleID=\"12\" fgColor=\"000080\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"1\" />
            <WordsStyle name=\"FOLDERCLOSE\" styleID=\"13\" fgColor=\"000080\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"1\" />
            <WordsStyle name=\"KEYWORD1\" styleID=\"5\" fgColor=\"0000FF\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"KEYWORD2\" styleID=\"6\" fgColor=\"800000\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"KEYWORD3\" styleID=\"7\" fgColor=\"D1802E\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"1\" />
            <WordsStyle name=\"KEYWORD4\" styleID=\"8\" fgColor=\"008040\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"COMMENT\" styleID=\"1\" fgColor=\"FF9900\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"COMMENT LINE\" styleID=\"2\" fgColor=\"FF9900\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"NUMBER\" styleID=\"4\" fgColor=\"000000\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"OPERATOR\" styleID=\"10\" fgColor=\"000080\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"1\" />
            <WordsStyle name=\"DELIMINER1\" styleID=\"14\" fgColor=\"000000\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"DELIMINER2\" styleID=\"15\" fgColor=\"000000\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
            <WordsStyle name=\"DELIMINER3\" styleID=\"16\" fgColor=\"000000\" bgColor=\"FFFFFF\" fontName=\"\" fontStyle=\"0\" />
        </Styles>
    </UserLang>
</NotepadPlus>"

#Build np++ xml file
printf "%s" "$string1" >  $output_file
cat    words_1.txt     >> $output_file
printf "%s" "$string2" >> $output_file
cat    words_2.txt     >> $output_file
printf "%s" "$string3" >> $output_file
cat    words_3.txt     >> $output_file
printf "%s" "$string4" >> $output_file
cat    words_4.txt     >> $output_file
printf "%s" "$string5" >> $output_file

#cleanup
rm temp_1.txt
rm temp_2.txt
rm temp_3.txt
rm temp_4.txt
rm words_1.txt
rm words_2.txt
rm words_3.txt
rm words_4.txt
