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

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