]> AND Private Git Repository - loba.git/blob - new_loba.sh
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Use credit to try to avoid useless transfers.
[loba.git] / new_loba.sh
1 #!/bin/bash
2
3 # Local variables:
4 # mode: sh
5 # End:
6
7 set -e
8
9 if [ "$1" = "-n" ]; then
10     echo "# Dry run mode!" >&2
11     create_files=0
12     shift
13 else
14     create_files=1
15 fi
16
17 if [ $# -ne 1 ]; then
18     cat >&2 <<EOF
19 Usage: $0 [-n] name
20     Add new load-balancing algorithm \`name'.
21 Options:
22     -n  dry-run mode (no file created, nor modified)
23 EOF
24     exit 1
25 fi
26
27 name=$(echo $1 | tr A-Z a-z)
28 if ! echo "$name" | grep -q '^[a-z0-9_]\+$' \
29     || ! [ $(echo "$name" | wc -l) = 1 ]; then
30     echo "ERROR: invalid name -- \"$name\"" >&2
31     exit 1
32 fi
33
34 uname=$(echo $name | tr a-z A-Z)
35 h_file=$(printf "loba_%s.h" $name)
36 cpp_file=$(printf "loba_%s.cpp" $name)
37
38 if [ -e "$h_file" -o -e "$cpp_file" ]; then
39     echo "ERROR: file $h_file or $cpp_file already exists!" >&2
40     exit 1
41 fi
42
43 echo "# Creating files for algorithm $name ($uname)..."
44 echo "# Creating file $h_file... "
45 if [ "$create_files" = "1" ]; then
46     cat > "$h_file" <<EOF
47 #ifndef LOBA_${uname}_H
48 #define LOBA_${uname}_H
49
50 #include "process.h"
51
52 class loba_${name}: public process {
53 public:
54     loba_${name}(int argc, char* argv[]): process(argc, argv) { }
55     ~loba_${name}()                                           { }
56
57 private:
58     void load_balance();
59 };
60
61 #endif //!LOBA_${uname}_H
62
63 // Local variables:
64 // mode: c++
65 // End:
66 EOF
67 fi
68
69 echo "# Creating file $cpp_file..."
70 if [ "$create_files" = "1" ]; then
71     cat > "$cpp_file" <<EOF
72 #include <xbt/log.h>
73
74 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(loba);
75
76 #include "loba_${name}.h"
77
78 void loba_${name}::load_balance()
79 {
80     // write code here...
81     xbt_die("Load-balancing algorithm \\"${name}\\" not implemented!");
82 }
83
84 // Local variables:
85 // mode: c++
86 // End:
87 EOF
88 fi
89
90 echo "# Updating options.cpp..."
91 tmp="options.cpp.new.$$"
92 trap "rm -f $tmp" EXIT
93
94 awk -vname="${name}" '
95 /^#include "loba_/ {
96     if (!include_line) {
97         h_file = "\"loba_" name ".h\"";
98         include_line = "#include " h_file;
99     }
100     if (!included && $2 >= h_file) {
101         if ($2 != h_file)
102             print "#include " h_file;
103         included = 1;
104     }
105     print;
106     next;
107 }
108
109 !included && include_line {
110     print include_line;
111     included = 1;
112 }
113
114 /loba_algorithms_type::loba_algorithms_type\(\)/ {
115     nol_compare = "NOL_INSERT(\"" name "\","
116     nol_string = "        " nol_compare " \"describe your algorithm here...\",\n";
117     nol_string = nol_string "                   loba_" name ");"
118     nol_insert = 1;
119 }
120
121 nol_insert && $1 ~ /\}/ {
122     if (!nol_inserted)
123         print nol_string;
124     nol_insert = 0;
125 }
126
127 nol_insert && !nol_inserted && /NOL_INSERT/ {
128     if ($1 >= nol_compare) {
129         if ($1 > nol_compare)
130             print nol_string;
131         nol_inserted = 1;
132     }
133 }
134
135 { print }
136 ' options.cpp > $tmp
137
138 if [ "$create_files" = "1" ]; then
139     mv options.cpp options.cpp~
140     mv $tmp options.cpp
141 else
142     if type colordiff &> /dev/null; then
143         diff -u options.cpp $tmp | colordiff
144     else
145         diff -u options.cpp $tmp
146     fi
147 fi
148
149 cat >&2 <<EOF
150 # Done.
151
152 Check for differences, update algorithm description in "options.cpp",
153 write your code in "${cpp_file}", and do not forget to add new files
154 in SCM repository, for example with:
155         \$ git add options.cpp ${h_file} ${cpp_file}
156         \$ git commit -m 'Add algorithm ${name}."
157 EOF