added irreco_theme_save function
[irreco.git] / irreco / irreco-backend-maker.sh
blob7a70f7e9e50372013d85b3394f5a1244891dc277
1 #!/bin/bash
3 HEADER='./src/api/irreco_backend_api.h'
5 backend_maker()
7 if [ "$1" == "" ]; then
8 echo "Usage: $0 OUTPUT_FILE"
9 echo ""
10 echo "This script reads example function implementations from"
11 echo "irreco_backend_api.h header and creates a matching template"
12 echo "source file based on them. Then someone can use this template"
13 echo "to build an Irreco Backend."
14 echo ""
15 exit
16 else
17 OUTPUT_FILE="$1"
18 if [ -f "$OUTPUT_FILE" ]; then
19 if [ "$( stat -c %Y "$OUTPUT_FILE" )" -gt \
20 "$( stat -c %Y "$HEADER" )" ]; then
21 echo "\"$OUTPUT_FILE\" is up to date."
22 exit 0
23 else
24 rm -f "$OUTPUT_FILE"
29 echo "/*" >> "$OUTPUT_FILE"
30 echo " * This file was autogenerated by" >> "$OUTPUT_FILE"
31 echo " * $0" >> "$OUTPUT_FILE"
32 echo " * from " >> "$OUTPUT_FILE"
33 echo " * $HEADER" >> "$OUTPUT_FILE"
34 echo " */" >> "$OUTPUT_FILE"
35 echo "" >> "$OUTPUT_FILE"
36 echo '#define IRRECO_DEBUG_PREFIX "MYBA"' >> "$OUTPUT_FILE"
37 echo "" >> "$OUTPUT_FILE"
38 echo "#include <gtk/gtk.h>" >> "$OUTPUT_FILE"
39 echo "#include <irreco_backend_api.h>" >> "$OUTPUT_FILE"
40 echo "" >> "$OUTPUT_FILE"
41 echo "typedef struct _MyBackend MyBackend;" >> "$OUTPUT_FILE"
42 echo "struct _MyBackend {" >> "$OUTPUT_FILE"
43 echo " gint important_variable;" >> "$OUTPUT_FILE"
44 echo " /** @todo Implement. */" >> "$OUTPUT_FILE"
45 echo "};" >> "$OUTPUT_FILE"
46 echo "" >> "$OUTPUT_FILE"
47 echo "" >> "$OUTPUT_FILE"
49 cat "$HEADER" \
50 | grep -v '^/\*\*.*\*/' \
51 | api_parser "$@"
54 api_parser()
56 CODEPOS=0
57 COMMENTPOS=0
58 local STATE='find'
59 while read LINE; do
61 # State change logic wich applies from this line.
62 case "$STATE" in
63 'find')
64 echo "$LINE" \
65 | grep '^/\*\*' > /dev/null \
66 && STATE='comment'
68 'comment')
69 echo "$LINE" \
70 | fgrep '@par Implement' > /dev/null \
71 && IGNORE_LINE=1
73 echo "$LINE" \
74 | fgrep '@code' > /dev/null && STATE='startcode' \
75 && CODEFOUND=1
77 echo "$LINE" \
78 | fgrep '@addtogroup' > /dev/null \
79 && IGNORE=1
81 'startcode')
82 STATE='code'
84 'code')
85 echo "$LINE" \
86 | fgrep '@endcode' > /dev/null && STATE='endcode'
88 'endcode')
89 STATE='comment'
91 esac
93 # Print line with state.
94 if [ "$IGNORE_LINE" == "1" ]; then
95 echo "Ignored: $LINE"
96 unset IGNORE_LINE
97 else
98 echo "$STATE: $LINE"
100 # Store intresting parts to arrays.
101 case "$STATE" in
102 'code') CODE[$CODEPOS]="$LINE"; ((CODEPOS++));;
103 'comment') COMMENT[$COMMENTPOS]="$LINE"; ((COMMENTPOS++));;
104 esac
107 # Detect ending of comment block.
108 if [ "$STATE" == 'comment' ] && echo "$LINE" \
109 | grep '\*/' > /dev/null; then
111 # Print function prototype if it was found.
112 if [[ "$CODEFOUND" == "1" && "$IGNORE" != "1" ]]; then
113 echo
114 echo "Writing function to \"$OUTPUT_FILE\"."
115 echo
117 POS=0
118 while [ "${#COMMENT[@]}" -gt $POS ]; do
119 echo "${COMMENT[$POS]}" \
120 | sed 's|^\*| *|' >> "$OUTPUT_FILE"
121 ((POS++))
122 done
124 POS=0
125 while [ "${#CODE[@]}" -gt $POS ]; do
126 echo "${CODE[$POS]}" \
127 | sed -r -e 's|^\*[ ]{0,1}||' \
128 -e 's|/_|/*|' -e 's|_/|*/|' \
129 >> "$OUTPUT_FILE"
130 ((POS++))
131 done
132 echo >> "$OUTPUT_FILE"
135 # Reset state machine.
136 STATE='find'
137 unset CODE
138 unset COMMENT
139 CODEPOS=0
140 COMMENTPOS=0
141 CODEFOUND=0
142 IGNORE=0
144 done
147 backend_maker "$@"