fix quote in last field
[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 csv_split() { ## USAGE: csv_split CSV_RECORD
56         local record=${1%"${CR}"}
57         local right
58         local vnum=0
59         unset values
60         while [ -n "$record" ]
61         do
62
63                 case $record in
64                         \"*) right=${record#*\",}
65                                 value=${record%%\",*}
66                                 values[$vnum]=${value#\"}
67                                 ;;
68                 *) values[$vnum]=${record%%,*}
69                         right=${record#*,}
70                         ;;
71                 esac
72
73                 case $record in
74                         *,*) record=${right} ;;
75                         *) record=${record#\"}
76                                 values[$vnum]=${record%\"}
77                                 break;;
78                 esac
79
80                 echo ${values[$vnum]}
81
82                 vnum=$(( $vnum + 1 ))
83
84         done
85
86         echo ${record%\"}
87 }
88
89 # Set column names:
90 eval "$(
91   col=0
92   csv_split `head --lines=1 "$data_file"` | 
93   while read name; do
94     let col++
95     if [ "$var_type" == "name" ]; then
96       echo col_name[$col]=$( echo $name | sed "s/[][ \$'\"]/_/g" )
97     else
98       echo col_name[$col]=$col
99     fi
100     echo tot_col=$col
101   done
102 )"
103
104 # '
105
106 eval "$(
107   echo "$extra_vars" | sed 's/|/\n/g' |
108   while read extra; do
109     key="$( echo "$extra" | sed 's/^.*=>\(.*\)$/\1/g' )"
110     for i in $( seq $tot_col ); do
111       if [ "${col_name[$i]}" = "$key" ]; then
112         echo extracol[$i]="'$( echo "$extra" | sed 's/^\(.*\)=>.*$/\1/g' )'"
113       fi
114     done
115   done
116 )"
117
118 if [ "$preview" = "true" ]; then
119   # debug:
120   txt=''
121   for i in $( seq $tot_col ); do
122     txt="$txt\n%VAR_${col_name[$i]}%"
123   done
124   for i in $( seq $tot_col ); do
125     [ "${extracol[$i]}" != "" ] && txt="$txt\n${extracol[$i]}"
126   done
127   zenity --info --title="Generator Variables" \
128          --text="The replaceable text, based on your configuration and on the CSV are:\n$txt"
129 fi
130
131 eval "output=\"$output\""
132
133 [ "$( dirname "$output" )" != "" ] && mkdir --parents "$( dirname "$output" )"
134
135 [ "$format" = "" ] && format=PDF
136 format=$( echo $format | tr a-z A-Z )
137
138 if ! ( echo "$output" | grep -qi "$format\$" ); then
139   if zenity --question --text="
140 Your output pattern has a file extension diferent from the export format.
141
142 Did you want to add the file extension?"; then
143     output="$output.$( echo $format | tr A-Z a-z )"
144   fi
145 fi
146
147 tmp_svg=$( mktemp )
148 tmp_png=$( mktemp )
149 ink_error=$( mktemp )
150
151 my_pid=$$
152 function the_end {
153   rm $tmp_svg $tmp_png $ink_error
154   kill $my_pid
155 }
156
157 function ink-generate {
158   f="$( echo "$1" | sed 's/^[^=]*=\(.*\)$/\1/' )"
159   rm "$f" 2>/dev/null
160   ( inkscape --without-gui \
161              "$1" --export-dpi="$dpi" \
162              $tmp_svg 2>&1 ) > $ink_error
163   if ! test -f "$f"; then
164     zenity --error --title="Inkscape Converting Error" \
165            --text="$(cat $ink_error |
166                      sed 's/&/&amp;/g; s/</&lt;/g; s/>/&gt;/g' )"
167     the_end
168     exit 1
169   fi
170 }
171
172 function show_preview {
173   case $format in
174     SVG)
175       ( inkview "$1" || inkscape "$1" ) 2>/dev/null ||
176       echo 'There is no visualizator for SVG' >&2 ;;
177     PDF)
178       ( evince "$1" || kpdf "$1" || xpdf "$1" || gs "$1" ) 2>/dev/null ||
179       echo 'There is no visualizator for PDF' >&2 ;;
180     PS)
181       ( evince "$1" || gs "$1" ) 2>/dev/null ||
182       echo 'There is no visualizator for PS'  >&2 ;;
183     EPS)
184       ( evince "$1" || gs "$1" ) 2>/dev/null ||
185       echo 'There is no visualizator for EPS' >&2 ;;
186     PNG)
187       ( eog "$1" || kview "$1" || display "$1" ) 2>/dev/null ||
188       echo 'There is no visualizator for PNG' >&2 ;;
189     JPG)
190       ( eog "$1" || kview "$1" || display "$1" ) 2>/dev/null ||
191       echo 'There is no visualizator for JPG' >&2 ;;
192   esac
193   echo 100
194   the_end
195 }
196
197 tot_lines=$( wc --lines "$data_file" | sed 's/^\([0-9]\+\).*/\1/' )
198 if [ "$var_type" == "name" ]; then
199   let tot_lines--
200 fi
201
202 cur_line=0
203
204 cat "$data_file" | (
205   [ "$var_type" == "name" ] && read cut_frist_line
206   while read line; do
207     col=0
208     replace="$(
209       csv_split "$line" |
210       while read val; do
211         let col++
212         echo -n "s/%VAR_${col_name[$col]}%/$(
213                  echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
214                  )/g; "
215         if [ "${extracol[$col]}" != "" ]; then
216           echo -n "s/${extracol[$col]}/$(
217                    echo "$val" | sed "s/\//\\\\\//g; s/'/\´/g"
218                    )/g; "
219         fi
220       done
221     )"
222     eval "sed '$replace' '$svg_file' > $tmp_svg"
223     out_file="$( echo "$output" | sed "$replace" )"
224     #echo "Gerando $out_file ..."
225     case $format in
226       SVG)
227         cp "$tmp_svg" "$out_file" ;;
228       PDF)
229         ink-generate --export-pdf="$out_file" ;;
230       PS)
231         ink-generate --export-ps="$out_file" ;;
232       EPS)
233         ink-generate --export-eps="$out_file" ;;
234       PNG)
235         ink-generate --export-png="$out_file" ;;
236       JPG)
237         ink-generate --export-png="$tmp_png"
238         if ! ( echo "$output" | grep -qi '.jpe\?g$' ); then
239           output="$output.jpg"
240         fi
241         convert $tmp_png "$out_file" ;;
242     esac
243     let cur_line++
244     echo $(( ( $cur_line * 100 ) / $tot_lines ))
245     [ "$preview" = "true" ] && show_preview "$out_file" && exit 0
246   done |
247   zenity --progress --title="Generator" \
248          --text="Generating..." --auto-close --width=400
249 )
250
251 [ "$preview" = "true" ] && exit 0
252
253 the_end