Optimus black and stuff
[wiki.git] / wt.sh
1 #!/bin/sh
2
3 # Replicant redmine wiki templates
4 #
5 # Copyright (C) 2014-2015 Paul Kocialkowski <contact@paulk.fr>
6 #
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
20 WT_SOURCE=$1
21 WT_DESTINATION=$( basename -s ".wt" $WT_SOURCE )
22 WT_DEFINES="\\WT_SOURCE=$WT_SOURCE\\WT_DESTINATION=$WT_DESTINATION"
23
24 command_define() {
25         arguments=$1
26         variable=$( echo "$arguments" | sed "s/\([^[:space:]]*\)[[:space:]]*.*/\1/g" )
27         value=$( echo "$arguments" | sed "s/[^[:space:]]*[[:space:]]*\(.*\)/\1/g" )
28
29         WT_DEFINES="$WT_DEFINES\\$variable=$value"
30 }
31
32 command_print() {
33         arguments=$1
34         start=$( echo "$arguments" | sed "s,\(.*\)//print \([^[:space:]/]*\)//\(.*\),\1,g" )
35         print=$( echo "$arguments" | sed "s,\(.*\)//print \([^[:space:]/]*\)//\(.*\),\2,g" )
36         end=$( echo "$arguments" | sed "s,\(.*\)//print \([^[:space:]/]*\)//\(.*\),\3,g" )
37
38         if [ "$print" != "$arguments" ]
39         then
40                 print=$( echo $WT_DEFINES | sed "s,.*\\$print=\([^\]*\).*,\1,g" )
41                 command_print "$start$print$end"
42         else
43                 echo "$arguments"
44         fi
45 }
46
47 command_ifeq() {
48         arguments=$1
49         variable=$( echo "$arguments" | sed "s/\([^[:space:]]*\)[[:space:]]*.*/\1/g" )
50         test=$( echo "$arguments" | sed "s/[^[:space:]]*[[:space:]]*\(.*\)/\1/g" )
51         value=$( echo $WT_DEFINES | sed "s,.*\\$variable=\([^\]*\).*,\1,g" )
52
53         if [ "$value" = "$test" ]
54         then
55                 return 0
56         else
57                 return 1
58         fi
59 }
60
61 command_ifneq() {
62         arguments=$1
63         variable=$( echo "$arguments" | sed "s/\([^[:space:]]*\)[[:space:]]*.*/\1/g" )
64         test=$( echo "$arguments" | sed "s/[^[:space:]]*[[:space:]]*\(.*\)/\1/g" )
65         value=$( echo $WT_DEFINES | sed "s,.*\\$variable=\([^\]*\).*,\1,g" )
66
67         if [ "$value" = "$test" ]
68         then
69                 return 1
70         else
71                 return 0
72         fi
73 }
74
75 command_include() {
76         file=$1
77
78         depth_current=0
79         depth_display=0
80
81         while read line
82         do
83                 command=$( echo "$line" | sed "s/^\/\/\([^[:space:]]*\)[[:space:]]*\(.*\)/\1/g" )
84                 arguments=$( echo "$line" | sed "s/^\/\/\([^[:space:]]*\)[[:space:]]*\(.*\)/\2/g" )
85
86                 case $command in
87                         "ifeq")
88                                 if [ $depth_display -ge $depth_current ]
89                                 then
90                                         if command_ifeq "$arguments"
91                                         then
92                                                 depth_display=$(( $depth_current + 1 ))
93                                         fi
94                                 fi
95
96                                 depth_current=$(( $depth_current + 1 ))
97
98                                 continue
99                                 ;;
100                         "ifneq")
101                                 if [ $depth_display -ge $depth_current ]
102                                 then
103                                         if command_ifneq "$arguments"
104                                         then
105                                                 depth_display=$(( $depth_current + 1 ))
106                                         fi
107                                 fi
108
109                                 depth_current=$(( $depth_current + 1 ))
110
111                                 continue
112                                 ;;
113                         "else")
114                                 if [ $depth_display -ge $depth_current ]
115                                 then
116                                         depth_display=$(( $depth_current - 1 ))
117                                 else
118                                         if [ $depth_display -eq $(( $depth_current - 1 )) ]
119                                         then
120                                                 depth_display=$depth_current
121                                         fi
122                                 fi
123
124                                 continue
125                                 ;;
126                         "endif")
127                                 if [ $depth_display -ge $depth_current ]
128                                 then
129                                         depth_display=$(( $depth_current - 1 ))
130                                 fi
131
132                                 depth_current=$(( $depth_current - 1 ))
133
134                                 continue
135                                 ;;
136                 esac
137
138                 if [ $depth_current -gt $depth_display ]
139                 then
140                         continue
141                 fi
142
143                 case $command in
144                         "define")
145                                 command_define "$arguments"
146                                 ;;
147                         "include")
148                                 command_include "$arguments"
149                                 ;;
150                         *)
151                                 command_print "$line"
152                                 ;;
153                 esac
154         done < "$file"
155 }
156
157 command_include $WT_SOURCE > $WT_DESTINATION