1 | #! /bin/sh |
---|
2 | #+ |
---|
3 | # |
---|
4 | # ================== |
---|
5 | # add_loceanbibid.sh |
---|
6 | # ================== |
---|
7 | # |
---|
8 | # SYNOPSIS |
---|
9 | # ======== |
---|
10 | # |
---|
11 | # ``add_loceanbibid.sh -i ifile -o ofile`` |
---|
12 | # |
---|
13 | # DESCRIPTION |
---|
14 | # =========== |
---|
15 | # |
---|
16 | # add a field loceanbibid to each reference |
---|
17 | # |
---|
18 | # -i input file (ifile) |
---|
19 | # -o output file |
---|
20 | # |
---|
21 | # We assume here that the input file is written with this pattern of block:: |
---|
22 | # |
---|
23 | # @reftyp{Id |
---|
24 | # ... |
---|
25 | # } |
---|
26 | # |
---|
27 | # EXAMPLES |
---|
28 | # ======== |
---|
29 | # |
---|
30 | # To add loceanid in each ref of the biblio of LOCEAN:: |
---|
31 | # |
---|
32 | # $ add_loceanbibid.sh -i biblioref.bib -o biblioref_withid.bib |
---|
33 | # |
---|
34 | # Check can be made :: |
---|
35 | # |
---|
36 | # sed -e "/loceanbibid={.*}/d" biblioref_withid.bib > ginette |
---|
37 | # diff biblioref.bib ginette |
---|
38 | # |
---|
39 | # Output contains lines which are before the first block, after the last |
---|
40 | # block and lines between blocks. |
---|
41 | # |
---|
42 | # PREAMBLE and comment blocks are preserve. |
---|
43 | # |
---|
44 | # TODO |
---|
45 | # ==== |
---|
46 | # |
---|
47 | # check for dupe loceanbibid |
---|
48 | # |
---|
49 | # remove timestap hard coded value |
---|
50 | # |
---|
51 | # EVOLUTIONS |
---|
52 | # ========== |
---|
53 | # |
---|
54 | # $Id$ |
---|
55 | # |
---|
56 | # - fplod 20120419 |
---|
57 | # |
---|
58 | # * creation |
---|
59 | # |
---|
60 | #- |
---|
61 | # |
---|
62 | system=$(uname) |
---|
63 | case "${system}" in |
---|
64 | AIX|IRIX64) |
---|
65 | echo " www : no specific posix checking" |
---|
66 | ;; |
---|
67 | *) |
---|
68 | set -o posix |
---|
69 | ;; |
---|
70 | esac |
---|
71 | unset system |
---|
72 | # |
---|
73 | LANG=POSIX |
---|
74 | # |
---|
75 | set -u |
---|
76 | # |
---|
77 | action=$(basename ${0} .sh) |
---|
78 | command=$(basename ${0}) |
---|
79 | log_date=$(date -u +"%Y%m%dT%H%M%SZ") |
---|
80 | # |
---|
81 | usage="Usage : ${command} -i ifile -o ofile" |
---|
82 | # |
---|
83 | hostname=$(hostname) |
---|
84 | # |
---|
85 | # default |
---|
86 | # N.A. |
---|
87 | # |
---|
88 | minargcount=4 |
---|
89 | if [ ${#} -lt ${minargcount} ] |
---|
90 | then |
---|
91 | echo "eee : not enought arguments" |
---|
92 | echo "${usage}" |
---|
93 | exit 1 |
---|
94 | fi |
---|
95 | # |
---|
96 | while [ ${#} -gt 0 ] |
---|
97 | do |
---|
98 | case ${1} in |
---|
99 | -i) |
---|
100 | ifile=${2} |
---|
101 | shift |
---|
102 | ;; |
---|
103 | -o) |
---|
104 | ofile=${2%/} |
---|
105 | shift |
---|
106 | ;; |
---|
107 | -h) |
---|
108 | echo "${usage}" |
---|
109 | exit 0 |
---|
110 | ;; |
---|
111 | *) |
---|
112 | # anything else |
---|
113 | echo "eee : unknown option ${1}" |
---|
114 | echo "eee : ${usage}" |
---|
115 | exit 1 |
---|
116 | ;; |
---|
117 | esac |
---|
118 | # next flag |
---|
119 | shift |
---|
120 | done |
---|
121 | # |
---|
122 | # |
---|
123 | # check for ${PROJECT_LOG} |
---|
124 | checkprojectlog |
---|
125 | status=${?} |
---|
126 | if [ ${status} -ne 0 ] |
---|
127 | then |
---|
128 | echo "${command} : eee : pb with ${PROJECT_LOG}" |
---|
129 | exit 1 |
---|
130 | fi |
---|
131 | unset status |
---|
132 | # |
---|
133 | # check for ifile |
---|
134 | if [ ! -r ${ifile} ] |
---|
135 | then |
---|
136 | echo "eee : ${ifile} not accessible" |
---|
137 | exit 1 |
---|
138 | fi |
---|
139 | # |
---|
140 | # check for ofile |
---|
141 | # the idea is to prevent unwanted overwrite |
---|
142 | if [ -f ${ofile} ] |
---|
143 | then |
---|
144 | echo "eee : ${ofile} already exist" |
---|
145 | exit 1 |
---|
146 | fi |
---|
147 | # |
---|
148 | log=$(buildlogfile ${action} ${log_date}) |
---|
149 | echo "[Context]" 1>> ${log} |
---|
150 | echo "command=$(basename ${0})" 1>>${log} |
---|
151 | echo "hostname=${hostname}" 1>> ${log} |
---|
152 | echo "runtime=${log_date}" 1>> ${log} |
---|
153 | echo "log=${log}" 1>> ${log} |
---|
154 | unset log_date |
---|
155 | echo "" 1>> ${log} |
---|
156 | # |
---|
157 | # get the number of ref. |
---|
158 | # |
---|
159 | nbref=$(grep -c '^@.*{.*,$' ${ifile}) |
---|
160 | # |
---|
161 | # remove one because of @PREAMBLE |
---|
162 | nbref=$(( ${nbref} - 1)) |
---|
163 | echo "${command} : iii : nbref= ${nbref}" >> ${log} 2>&1 |
---|
164 | echo "${command} : iii : nbref= ${nbref}" |
---|
165 | awkfile=${PROJECT_LOG}/add_loceanid${$}.awk |
---|
166 | cat > ${awkfile} << EOF |
---|
167 | BEGIN{ |
---|
168 | nbref=${nbref} |
---|
169 | iref=${nbref}+1 |
---|
170 | } |
---|
171 | { |
---|
172 | if (index(\$0,"@")==1) { |
---|
173 | if (index(\$0,"@PREAMBLE")!=1) { |
---|
174 | print \$0 |
---|
175 | printf " loceanbibid={%5.5d},\n",iref |
---|
176 | printf " timestamp={20120419},\n",iref |
---|
177 | iref=iref-1; |
---|
178 | } |
---|
179 | else { |
---|
180 | print \$0 |
---|
181 | } |
---|
182 | } |
---|
183 | else { |
---|
184 | print \$0 |
---|
185 | } |
---|
186 | } |
---|
187 | EOF |
---|
188 | # |
---|
189 | #more ${awkfile} |
---|
190 | # apply awk command |
---|
191 | awk -f ${awkfile} ${ifile} > ${ofile} 2>&1 |
---|
192 | status=${?} |
---|
193 | if [ ${?} -ne 0 ] |
---|
194 | then |
---|
195 | echo "${command} : eee : pb with awk command" |
---|
196 | exit 1 |
---|
197 | fi |
---|
198 | # |
---|
199 | echo "iii : ${ifile} has been completed with loceanbibid in ${ofile}" |
---|
200 | # |
---|
201 | # clean |
---|
202 | rm ${awkfile} |
---|
203 | unset ifile |
---|
204 | unset ofile |
---|
205 | # |
---|
206 | # end |
---|
207 | exit 0 |
---|