Fix qchess/update.sh
[progcomp2013.git] / qchess / update.sh
1 #!/bin/bash
2
3 # I still can't believe I am doing this
4
5 # (This can't be done with gnu make, because of circular dependency issues)
6
7 target=qchess.py
8 components="piece.py board.py player.py thread_util.py game.py graphics.py main.py"
9 # The below seems nicer, but doesn't work because things need to be imported in the right order :(
10 #components=$(ls *.py | tr '\t' '\n' | grep -v $target)
11
12 header="#!/usr/bin/python -u"
13 footer="# EOF - created from update.sh on $(date)"
14
15
16
17 # If the target was modified more recently than any of the components, update the component file
18 target_mod=$(stat -c %Y $target 2>/dev/null)
19
20 if [ $? -ne 0 ]; then
21         merge_required=true
22 else
23         merge_required=false
24
25         for f in $components; do
26                 
27                 component_mod=$(stat -c %Y $f 2>/dev/null)
28                 if [ $? -ne 0 ]; then
29                         update_required=true
30                 elif [ $component_mod -lt $target_mod ]; then
31                         update_required=true
32                 else
33                         update_required=false
34                 fi
35
36                 if $update_required; then
37                         echo "$0 - update $f from $target"
38                         sanity=$(egrep "(+++ $f +++)|(--- $f ---)" $target | wc -l)
39                         if [ $sanity -ne 2 ]; then
40                                 $(echo "$0 - $target does not have markers for $f in it!") 1>&2
41                                 exit 1
42                         fi
43                         cp $f $f~
44                         new_contents=$(nawk "/+++ $f +++/, /--- $f ---/" $target | grep -v "+++ $f +++" | grep -v "\--- $f ---")
45                         
46                         echo "$new_contents" > $f
47                 else
48                         echo "$0 - $f is newer than $target"
49                         merge_required=true
50                 fi
51         done
52 fi
53
54 # If any components were modified more recently than the target, merge the components into the target
55 if $merge_required; then
56         echo $header > $target
57         for f in $components; do
58                 echo "$0 - merge $f into $target"
59                 echo "# +++ $f +++ #" >> $target
60                 cat $f >> $target
61                 echo "# --- $f --- #" >> $target
62         done
63
64         echo $footer >> $target
65         chmod u+x $target
66 fi

UCC git Repository :: git.ucc.asn.au