1#!/bin/bash
2
3# Since the C toolkit cleanasn changes from time to time, sometimes
4# you need to rebuild all the files created from it.
5# This also rebuilds template_expanded, etc. and whatever else it can,
6# just to be sure.
7
8# This takes no arguments.
9
10# edited July 28, 2011
11# edited again Oct 13-14, 2011 so it works in the C++ build directory
12# Nov 4, 2011: use my personal cleanasn
13
14# Just to prevent people from running it accidentally and wreaking havoc
15# (Once you've updated it, you will probably change the string
16# that's checked against whoami)
17if [[ $(whoami) != kornbluh ]] ; then
18    die please edit the source of this script file and make sure it does exactly what you expect and works with your particular files
19fi
20
21set -x
22
23TEST_DIR=/home/kornbluh/c++.opt/src/objtools/cleanup/test/test_cases/
24EASY_AUTO_REPLACE=/home/kornbluh/c++.opt/src/objtools/cleanup/test/test_cases/+easy_auto_replace
25RUN_TESTS=/home/kornbluh/c++.opt/src/objtools/cleanup/test/test_cases/+run_tests
26# This is a simple program that takes two args: the file to truncate, and
27# the size to truncate to.  In C, the main() could be as simple as:
28# return truncate( argv[1], atoi(argv[2]) );
29FILE_TRUNCATOR=/home/kornbluh/bin/mytrunc
30CLEANASN=/home/kornbluh/c_toolkit/ncbi/build/cleanasn
31
32echo START $(date)
33
34die()
35{
36    echo "$@" >&2
37    exit 1
38}
39
40must()
41{
42    # run successfully or die
43    "$@" && return 0
44    die FAILURE: "$@"
45}
46
47silent()
48{
49    # run and don't care about output or failure
50    "$@" > /dev/null 2>&1
51}
52
53sort_by_middle_num()
54{
55    template_root=$1
56    file_ext=$2
57
58
59    # find highest-numbered
60    MAX_NUM=0
61    for file in ${template_root}.*.${file_ext} ; do
62        file=$(basename $file)
63        while [[ $file != [0-9]* ]] ; do
64            file=${file#*.}
65        done
66        file=${file%%.*}
67        if (( $file > $MAX_NUM )) ; then
68            MAX_NUM=$file
69        fi
70    done
71
72    # print them from 0 to $MAX_NUM
73    for num in $(seq 0 $MAX_NUM) ; do
74        echo ${template_root}.${num}.${file_ext}
75    done
76}
77
78generate_cleanasns_output()
79{
80    template_root=$1
81
82    # for simplicity (but certainly not speed!) we split the template file
83    must $EASY_AUTO_REPLACE ${template_root}.template
84
85    # no "must" because might fail just because of change
86    $RUN_TESTS $( sort_by_middle_num ${template_root} test ) -wco -ca=$CLEANASN -e-auto
87
88    silent rm ${template_root}.cleanasns_output # make sure empty
89    for file in $( sort_by_middle_num ${template_root} cleanasns_output ) ; do
90        cat $file >> ${template_root}.cleanasns_output
91        # But, remove the extra newline at the end
92        file_size=$(stat --format %s ${template_root}.cleanasns_output )
93        must $FILE_TRUNCATOR ${template_root}.cleanasns_output $(( $file_size - 1 ))
94    done
95    # needs a newline at the end
96    echo >> ${template_root}.cleanasns_output
97
98    silent rm -f ${template_root}.*.test
99    silent rm -f ${template_root}.*.cleanasns_output
100}
101
102can_process()
103{
104    file_name=$1
105
106    # certain files can't be processed
107    if [[ $file_name == *highest_level* || $file_name == *unusable* ]] ; then
108        return 1 # failure
109    else
110        return 0 # success
111    fi
112}
113
114must cd $TEST_DIR
115must [ -x $EASY_AUTO_REPLACE ]
116must [ -x $RUN_TESTS ]
117must [ -x $FILE_TRUNCATOR ]
118must [ -x $CLEANASN ]
119
120# handle .template files
121for template_file_gz in $(find -name '*.template.gz') ; do
122
123    if can_process $template_file_gz ; then
124        echo $(date) processing $template_file_gz
125    else
126        echo $(date) skipping $template_file_gz
127        continue
128    fi
129
130    # varibles of the files we might look at (without the .gz extension,
131    # if any)
132    template_file=${template_file_gz%.gz}
133    template_root=${template_file%.template}
134    template_expanded_file=${template_root}.template_expanded
135    cleanasns_output_file=${template_root}.cleanasns_output
136    answer_file=${template_root}.answer
137    template_file_old_gz=${template_file}_OLD.gz
138    template_expanded_file_old_gz=${template_expanded_file}_OLD.gz
139    cleanasns_output_file_old_gz=${cleanasns_output_file}_OLD.gz
140
141    # these will store the old so we can compare if they've changed
142    must cp $template_file_gz $template_file_old_gz
143    must mv ${template_expanded_file}.gz $template_expanded_file_old_gz
144    silent mv ${cleanasns_output_file}.gz  $cleanasns_output_file_old_gz
145
146    # regenerate the template_expanded file
147    must gunzip $template_file_gz
148    must $EASY_AUTO_REPLACE -1 $template_file
149
150    # if there's no answer file, we recreate the cleanasns_output
151    if [[ ! -f ${answer_file}.gz ]] ; then
152        generate_cleanasns_output $template_root
153    fi
154
155    # zip the files back up (or restore from old one)
156    must rm -f $template_file
157    must mv $template_file_old_gz $template_file_gz
158
159    if zdiff -q $template_expanded_file $template_expanded_file_old_gz ; then
160        must rm -f $template_expanded_file
161        must mv $template_expanded_file_old_gz ${template_expanded_file}.gz
162    else
163        must gzip $template_expanded_file
164        must rm -f $template_expanded_file_old_gz
165    fi
166
167    if [[ -f $cleanasns_output_file ]] ; then
168        if zdiff -q $cleanasns_output_file $cleanasns_output_file_old_gz ; then
169            must rm -f $cleanasns_output_file
170            must mv $cleanasns_output_file_old_gz ${cleanasns_output_file}.gz
171        else
172            must gzip $cleanasns_output_file
173            must rm -f $cleanasns_output_file_old_gz
174        fi
175    fi
176done
177
178echo $(date) now processing raw_test files
179
180# handle .raw_test files
181for raw_test_gz in $(find -name '*.raw_test.gz') ; do
182    if can_process $raw_test_gz ; then
183        echo $(date) processing $raw_test_gz
184    else
185        echo $(date) skipping $raw_test_gz
186        continue
187    fi
188
189    # varibles of the files we might look at (without the .gz extension,
190    # if any)
191    raw_test=${raw_test_gz%.gz}
192    raw_root=${raw_test%.raw_test}
193    cleanasns_output_file=${raw_root}.cleanasns_output
194    answer_file=${raw_root}.answer
195    raw_test_old_gz=${raw_test}_OLD.gz
196    cleanasns_output_file_old_gz=${raw_root}.cleanasns_output_OLD.gz
197
198    # if there's already an answer, get out of here
199    if [[ -f ${answer_file}.gz ]] ; then
200        must [ ! -f ${cleanasns_output_file}.gz ]
201        continue
202    fi
203
204    cp $raw_test_gz $raw_test_old_gz
205    must gunzip $raw_test_gz
206
207    # erase stale file
208    # store the old so we can compare if it's changed
209    must mv ${cleanasns_output_file}.gz $cleanasns_output_file_old_gz
210
211    # no "must" because might fail just because of difference
212    $RUN_TESTS $raw_test -wco -ca=$CLEANASN -e-auto
213
214    # zip the files back up
215    must rm -f $raw_test
216    must mv $raw_test_old_gz $raw_test_gz
217
218    if zdiff -q $cleanasns_output_file $cleanasns_output_file_old_gz ; then
219        must mv $cleanasns_output_file_old_gz ${cleanasns_output_file}.gz
220        must rm -f $cleanasns_output_file
221    else
222        must gzip $cleanasns_output_file
223        must rm -f $cleanasns_output_file_old_gz
224    fi
225
226done
227
228echo $(date): clean up .test files that are still lying around
229find . -name '*.test' -exec rm {} \;
230
231echo FINISH $(date)
232
233exit 0
234