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