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