source: branches/bibliolocean/src/add_loceanbibid.sh @ 354

Last change on this file since 354 was 354, checked in by pinsard, 10 years ago

fix thanks to coding rules; typo

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