1595f13673ac9f50e15d1e073af67cb65cbaf2ea
[ink-generator.git] / generator.sh
1 #!/bin/bash
2
3 #   Generator - a Inkscape extension to generate end-use files from a model
4 #   Copyright (C) 2008  Aurélio A. Heckert
5 #
6 #   Added CSV parser from
7 #   http://fixunix.com/unix/83523-how-do-i-read-split-derive-csv-using-bash-script.html
8 #   by Chris F.A. Johnson <cfaj@freeshell.org> http://cfaj.freeshell.org
9 #
10 #   This program is free software: you can redistribute it and/or modify
11 #   it under the terms of the GNU General Public License as published by
12 #   the Free Software Foundation, either version 3 of the License, or
13 #   (at your option) any later version.
14 #
15 #   This program is distributed in the hope that it will be useful,
16 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
17 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 #   GNU General Public License for more details.
19
20 #   Version 0.4
21
22 # Collect all args and create local variables:
23 for arg in "$@"; do
24   if ( echo -n $arg | grep -q '^--.*=' ); then
25     key="$( echo -n "$arg" |
26             sed 's/^--\([^=]*\)=.*$/\1/; s/[^a-zA-Z0-9]/_/' )"
27     val="$( echo -n "$arg" |
28             sed 's/^--\([^=]*\)=\(.*\)$/\2/' )"
29     eval "$key=\"$val\""
30   else
31     svg_file="$arg"
32   fi
33 done
34
35 if ! zenity --version 2>/dev/null; then
36   function zenity() {
37     echo -e "\n$@\n" >&2
38   }
39   zenity "You must to install Zenity to have a better interaction
40 with this script, but it will work anyway."
41 fi
42
43 if [ "${data_file:0:1}" != "/" ]; then
44   data_file="$(pwd)/$data_file"
45 fi
46
47 if ! test -f "$data_file"; then
48   zenity --error --title="User Information Error" \
49          --text="The CSV file \"$data_file\" was not found.
50
51 Please, give the right name, or give the full path for the CSV file."
52   exit 1
53 fi
54
55 #function sep_line {
56 #  sed 's/^\([^"]*\("[^"]*"[^"]*\)*"[^"]*\),/\1#COMMAHACK#/g' |
57 #  sed 's/,/\n/g' | sed "s/\"\"/\"/g; s/^['\"]\|['\"]$//g"
58 #}
59
60 function csv_split() { ## USAGE: csv_split CSV_RECORD
61         local record=${1%"${CR}"}
62         local right
63         local vnum=0
64         unset values
65         while [ -n "$record" ]
66         do
67
68                 case $record in
69                         \"*) right=${record#*\",}
70                                 value=${record%%\",*}
71                                 values[$vnum]=${value#\"}
72                                 ;;
73                 *) values[$vnum]=${record%%,*}
74                         right=${record#*,}
75                         ;;
76                 esac
77
78                 case $record in
79                         *,*) record=${right} ;;
80                         *) record=${record#\"}
81                                 values[$vnum]=${record%\"}
82                                 break;;
83                 esac
84
85                 echo ${values[$vnum]}
86
87                 vnum=$(( $vnum + 1 ))
88
89         done
90 }
91
92 # Set column names:
93 eval "$(
94   col=0
95   csv_split `head --lines=1 "$data_file"` | 
96   while read name; do
97     let col++
98     if [ "$var_type" == "name" ]; then
99       echo col_name[$col]=$( echo $name | sed "s/[][ \$'\"]/_/g" )
100     else
101       echo col_name[$col]=$col
102     fi
103     echo tot_col=$col
104   done
105 )"
106
107 # '
108
109 eval "$(
110   echo "$extra_vars" | sed 's/|/\n/g' |
111   while read extra; do
112     key="$( echo "$extra" | sed 's/^.*=>\(.*\)$/\1/g' )"
113     for i in $( seq $tot_col ); do
114       if [ "${col_name[$i]}" = "$key" ]; then
115         echo extracol[$i]="'$( echo "$extra" | sed 's/^\(.*\)=>.*$/\1/g' )'"
116       fi
117     done
118   done
119 )"
120
121 if [ "$preview" = "true" ]; then
122   # debug:
123   txt=''
124   for i in $( seq $tot_col ); do
125     txt="$txt\n%VAR_${col_name[$i]}%"
126   done
127   for i in $( seq $tot_col ); do
128     [ "${extracol[$i]}" != "" ] && txt="$txt\n${extracol[$i]}"
129   done
130   zenity --info --title="Generator Variables" \
131          --text="The replaceable text, based on your configuration and on the CSV are:\n$txt"
132 fi
133
134 eval "output=\"$output\""
135
136 [ "$( dirname "$output" )" != "" ] && mkdir --parents "$( dirname "$output" )"
137
138 [ "$format" = "" ] && format=PDF
139 format=$( echo $format | tr a-z A-Z )
140
141 if ! ( echo "$output" | grep -qi "$format\$" ); then
142   if zenity --question --text="
143 Your output pattern has a file extension diferent from the export format.
144
145 Did you want to add the file extension?"; then
146     output="$output.$( echo $format | tr A-Z a-z )"
147   fi
148 fi
149
150 tmp_svg=$( mktemp )
151 tmp_png=$( mktemp )
152 ink_error=$( mktemp )
153
154 my_pid=$$
155 function the_end {
156   rm $tmp_svg $tmp_png $ink_error
157   kill $my_pid
158 }
159
160 function ink-generate {
161   f="$( echo "$1" | sed 's/^[^=]*=\(.*\)$/\1/' )"
162   rm "$f" 2>/dev/null
163   ( inkscape --without-gui \
164              "$1" --export-dpi="$dpi" \
165              $tmp_svg 2>&1 ) > $ink_error
166   if ! test -f "$f"; then
167     zenity --error --title="Inkscape Converting Error" \
168            --text="$(cat $ink_error |
169                      sed 's/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g' )"
170     the_end
171     exit 1
172   fi
173 }
174
175 function show_preview {
176   case $format in
177     SVG)
178       ( inkview "$1" || inkscape "$1" ) 2>/dev/null ||
179       echo 'There is no visualizator for SVG' >&2 ;;
180     PDF)
181       ( evince "$1" || kpdf "$1" || xpdf "$1" || gs "$1" ) 2>/dev/null ||
182       echo 'There is no visualizator for PDF' >&2 ;;
183     PS)
184       ( evince "$1" || gs "$1" ) 2>/dev/null ||
185       echo 'There is no visualizator for PS'  >&2 ;;
186     EPS)
187       ( evince "$1" || gs "$1" ) 2>/dev/null ||
188       echo 'There is no visualizator for EPS' >&2 ;;
189     PNG)
190       ( eog "$1" || kview "$1" || display "$1" ) 2>/dev/null ||
191       echo 'There is no visualizator for PNG' >&2 ;;
192     JPG)
193       ( eog "$1" || kview "$1" || display "$1" ) 2>/dev/null ||
194       echo 'There is no visualizator for JPG' >&2 ;;
195   esac
196   echo 100
197   the_end
198 }
199
200 tot_lines=$( wc --lines "$data_file" | sed 's/^\([0-9]\+\).*/\1/' )
201 if [ "$var_type" == "name" ]; then
202   let tot_lines--
203 fi
204
205 cur_line=0
206
207 cat "$data_file" | (
208   [ "$var_type" == "name" ] && read cut_frist_line
209   while read line; do
210     col=0
211     replace="$(
212       csv_split "$line" |
213       while read val; do
214         let col++
215         echo -n "s/%VAR_${col_name[$col]}%/$(
216                  echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
217                  )/g; " | sed 's/#COMMAHACK#/,/g'
218         if [ "${extracol[$col]}" != "" ]; then
219           echo -n "s/${extracol[$col]}/$(
220                    echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
221                    )/g; " | sed 's/#COMMAHACK#/,/g'
222         fi
223       done
224     )"
225     eval "sed '$replace' '$svg_file' > $tmp_svg"
226     out_file="$( echo "$output" | sed "$replace" )"
227     #echo "Gerando $out_file ..."
228     case $format in
229       SVG)
230         cp "$tmp_svg" "$out_file" ;;
231       PDF)
232         ink-generate --export-pdf="$out_file" ;;
233       PS)
234         ink-generate --export-ps="$out_file" ;;
235       EPS)
236         ink-generate --export-eps="$out_file" ;;
237       PNG)
238         ink-generate --export-png="$out_file" ;;
239       JPG)
240         ink-generate --export-png="$tmp_png"
241         if ! ( echo "$output" | grep -qi '.jpe\?g$' ); then
242           output="$output.jpg"
243         fi
244         convert $tmp_png "$out_file" ;;
245     esac
246     let cur_line++
247     echo $(( ( $cur_line * 100 ) / $tot_lines ))
248     [ "$preview" = "true" ] && show_preview "$out_file" && exit 0
249   done |
250   zenity --progress --title="Generator" \
251          --text="Generating..." --auto-close --width=400
252 )
253
254 [ "$preview" = "true" ] && exit 0
255
256 the_end