[2] | 1 | #!/bin/sh |
---|
| 2 | |
---|
| 3 | SED_SCRIPT=".sed_script"; |
---|
| 4 | SED_FILE=".sed_file"; |
---|
| 5 | SOURCE_FILE="New_Component"; |
---|
| 6 | SOURCE_DIR="./$SOURCE_FILE"; |
---|
[15] | 7 | TMP_DIR="/tmp/$SOURCE_FILE" |
---|
[2] | 8 | |
---|
| 9 | #-----[ usage ]------------------------------------------------------ |
---|
| 10 | # input : - |
---|
| 11 | # output : - |
---|
| 12 | |
---|
| 13 | function usage () |
---|
| 14 | { |
---|
| 15 | echo ""; |
---|
| 16 | echo "usage : $0 <directory> <name>"; |
---|
| 17 | echo " - <directory> : localisation of component"; |
---|
| 18 | echo " - <name> : name of component"; |
---|
| 19 | echo ""; |
---|
| 20 | echo " Note : This script must be execute in directory's script"; |
---|
| 21 | echo ""; |
---|
| 22 | |
---|
| 23 | exit; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | #-----[ test_usage ]------------------------------------------------- |
---|
| 27 | # input : all parameters |
---|
| 28 | # output : - |
---|
| 29 | |
---|
| 30 | function test_usage () |
---|
| 31 | { |
---|
| 32 | if test ! -d $SOURCE_DIR; then |
---|
| 33 | echo "Source directory \"$SOURCE_DIR\" invalid"; |
---|
| 34 | usage; |
---|
| 35 | fi; |
---|
| 36 | |
---|
[15] | 37 | if test -d $TMP_DIR; then |
---|
| 38 | echo "Temporary directory \"$TMP_DIR\" exist already"; |
---|
| 39 | usage; |
---|
| 40 | fi; |
---|
| 41 | |
---|
[2] | 42 | if test -f $SED_SCRIPT; then |
---|
| 43 | echo "File \"$SED_SCRIPT\" exist"; |
---|
| 44 | usage; |
---|
| 45 | fi; |
---|
| 46 | if test -f $SED_FILE; then |
---|
| 47 | echo "File \"$SED_FILE\" exist"; |
---|
| 48 | usage; |
---|
| 49 | fi; |
---|
| 50 | |
---|
| 51 | if test $# -ne 2; then |
---|
| 52 | usage; |
---|
| 53 | fi; |
---|
| 54 | |
---|
| 55 | # if test -d $1/$2; then |
---|
| 56 | # echo "Component in Directory \"$1/$2\" already exist"; |
---|
| 57 | # usage; |
---|
| 58 | # fi; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | #-----[ rename_file ]------------------------------------------------ |
---|
| 62 | # input : file_old name_old name_new |
---|
| 63 | # output : - |
---|
| 64 | |
---|
| 65 | function rename_file () |
---|
| 66 | { |
---|
| 67 | FILE_OLD=$1; |
---|
| 68 | NAME_OLD=$2; |
---|
| 69 | NAME_NEW=$3; |
---|
| 70 | |
---|
| 71 | FILE_NEW="`dirname $FILE_OLD`/`basename $FILE_OLD |sed s/"$NAME_OLD"/"$NAME_NEW"/`"; |
---|
| 72 | |
---|
| 73 | if test $FILE_NEW != $FILE_OLD; then |
---|
| 74 | mv $FILE_OLD $FILE_NEW; |
---|
| 75 | fi; |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | #-----[ rename_directory ]------------------------------------------- |
---|
| 79 | # input : dir name_old name_new |
---|
| 80 | # output : - |
---|
| 81 | |
---|
| 82 | function rename_directory () |
---|
| 83 | { |
---|
| 84 | NAME_OLD=$2; |
---|
| 85 | NAME_NEW=$3; |
---|
| 86 | |
---|
| 87 | # recursion |
---|
| 88 | for ENTRY in $1/*; do |
---|
| 89 | |
---|
| 90 | if test -d $ENTRY; then |
---|
| 91 | rename_directory $ENTRY $2 $3; |
---|
| 92 | else |
---|
| 93 | rename_file $ENTRY $2 $3; |
---|
| 94 | fi; |
---|
| 95 | done; |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | #-----[ directory_to ]----------------------------------------------- |
---|
| 99 | # input : directory cmd |
---|
| 100 | # output : - |
---|
| 101 | |
---|
| 102 | function directory_to () |
---|
| 103 | { |
---|
| 104 | res=""; |
---|
| 105 | |
---|
| 106 | for i in `echo "$1" |tr \/ ' '`; do |
---|
| 107 | j=`echo $i |tr [:upper:] [:lower:]`; |
---|
| 108 | case $2 in |
---|
| 109 | "dir_morpheo") |
---|
| 110 | if test -z "$res"; then |
---|
| 111 | res="..\/.."; |
---|
| 112 | else |
---|
| 113 | res="..\/$res"; |
---|
| 114 | fi; |
---|
| 115 | ;; |
---|
| 116 | "component_lower") |
---|
| 117 | res="$j"; |
---|
| 118 | ;; |
---|
| 119 | "component") |
---|
| 120 | res="$i"; |
---|
| 121 | ;; |
---|
| 122 | "directory") |
---|
| 123 | if test -z "$res"; then |
---|
| 124 | res=$i; |
---|
| 125 | else |
---|
| 126 | res="$res\/$i"; |
---|
| 127 | fi; |
---|
| 128 | ;; |
---|
| 129 | "define") |
---|
| 130 | if test -z "$res"; then |
---|
| 131 | res=$j; |
---|
| 132 | else |
---|
| 133 | res="${res}_${j}"; |
---|
| 134 | fi; |
---|
| 135 | ;; |
---|
| 136 | "namespace_begin") |
---|
| 137 | prefixe="namespace "; |
---|
| 138 | postfixe=" {\n"; |
---|
| 139 | if test -z "$res"; then |
---|
| 140 | res="$prefixe$j$postfixe"; |
---|
| 141 | else |
---|
| 142 | res="$res$prefixe$j$postfixe"; |
---|
| 143 | fi; |
---|
| 144 | ;; |
---|
| 145 | "namespace_end") |
---|
| 146 | prefixe="}; \/\/ end namespace "; |
---|
| 147 | postfixe="\n"; |
---|
| 148 | if test -z "$res"; then |
---|
| 149 | res="$prefixe$j$postfixe"; |
---|
| 150 | else |
---|
| 151 | res="$prefixe$j$postfixe$res"; |
---|
| 152 | fi; |
---|
| 153 | ;; |
---|
| 154 | "namespace_use") |
---|
| 155 | if test -z "$res"; then |
---|
| 156 | res="$j"; |
---|
| 157 | else |
---|
| 158 | res="$res::$j"; |
---|
| 159 | fi; |
---|
| 160 | ;; |
---|
| 161 | "namespace_using") |
---|
| 162 | prefixe="using namespace morpheo::behavioural::"; |
---|
| 163 | postfixe=";\n"; |
---|
| 164 | if test -z "$res"; then |
---|
| 165 | res="$prefixe$j$postfixe"; |
---|
| 166 | namespace="$j"; |
---|
| 167 | else |
---|
| 168 | res="$res$prefixe$namespace::$j$postfixe"; |
---|
| 169 | namespace="$namespace::$j"; |
---|
| 170 | fi; |
---|
| 171 | ;; |
---|
| 172 | *) |
---|
| 173 | esac; |
---|
| 174 | done; |
---|
| 175 | |
---|
| 176 | echo $res; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | #-----[ translation_script ]----------------------------------------- |
---|
| 180 | # input : dir component |
---|
| 181 | # output : - |
---|
| 182 | |
---|
| 183 | function translation_script () |
---|
| 184 | { |
---|
| 185 | # All example is the component Generic/RegisterFile |
---|
| 186 | |
---|
| 187 | #@DIR_MORPHEO ../../ |
---|
| 188 | SED=`directory_to $1/$2 "dir_morpheo"`; |
---|
| 189 | echo "s/@DIR_MORPHEO/$SED/g" >> $SED_SCRIPT; |
---|
| 190 | |
---|
| 191 | #@COMPONENT_LOWER registerfile |
---|
| 192 | SED=`directory_to $2 "component_lower"`; |
---|
| 193 | echo "s/@COMPONENT_LOWER/$SED/g" >> $SED_SCRIPT; |
---|
| 194 | |
---|
| 195 | #@COMPONENT RegisterFile |
---|
| 196 | SED=`directory_to $2 "component"`; |
---|
| 197 | echo "s/@COMPONENT/$SED/g" >> $SED_SCRIPT; |
---|
| 198 | |
---|
| 199 | #@DIRECTORY Generic/RegisterFile |
---|
| 200 | SED=`directory_to $1/$2 "directory"`; |
---|
| 201 | echo "s/@DIRECTORY/$SED/g" >> $SED_SCRIPT; |
---|
| 202 | |
---|
| 203 | #@DEFINE generic_registerfile |
---|
| 204 | SED=`directory_to $1/$2 "define"`; |
---|
| 205 | echo "s/@DEFINE/$SED/g" >> $SED_SCRIPT; |
---|
| 206 | |
---|
| 207 | #@NAMESPACE_BEGIN namespace generic { |
---|
| 208 | # namespace registerfile { |
---|
| 209 | SED=`directory_to $1/$2 "namespace_begin"`; |
---|
| 210 | echo "s/@NAMESPACE_BEGIN/$SED/g" >> $SED_SCRIPT; |
---|
| 211 | |
---|
| 212 | #@NAMESPACE_END }; // end namespace registerfile |
---|
| 213 | # }; // end namespace generic |
---|
| 214 | SED=`directory_to $1/$2 "namespace_end"`; |
---|
| 215 | echo "s/@NAMESPACE_END/$SED/g" >> $SED_SCRIPT; |
---|
| 216 | |
---|
| 217 | #@NAMESPACE_USE generic::registerfile |
---|
| 218 | SED=`directory_to $1/$2 "namespace_use"`; |
---|
| 219 | echo "s/@NAMESPACE_USE/$SED/g" >> $SED_SCRIPT; |
---|
| 220 | |
---|
| 221 | #@NAMESPACE_USING using namespace morpheo::behavioural::generic; |
---|
| 222 | SED=`directory_to $1 "namespace_using"`; |
---|
| 223 | echo "s/@NAMESPACE_USING/$SED/g" >> $SED_SCRIPT; |
---|
| 224 | |
---|
| 225 | } |
---|
| 226 | |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | #-----[ translation_file ]------------------------------------------- |
---|
| 230 | # input : file |
---|
| 231 | # output : - |
---|
| 232 | |
---|
| 233 | function translation_file () |
---|
| 234 | { |
---|
| 235 | echo "Translation : $1"; |
---|
| 236 | |
---|
| 237 | cat $1 |sed -f $SED_SCRIPT $1 > $SED_FILE; |
---|
| 238 | mv $SED_FILE $1; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | #-----[ translation_directory_rec ]---------------------------------- |
---|
| 242 | # input : dir |
---|
| 243 | # output : - |
---|
| 244 | |
---|
| 245 | function translation_directory_rec |
---|
| 246 | { |
---|
[15] | 247 | if test `ls $1|grep -c ""` -ne 0; then |
---|
| 248 | # recursion |
---|
| 249 | for ENTRY in $1/*; do |
---|
| 250 | |
---|
| 251 | if test -d $ENTRY; then |
---|
| 252 | translation_directory_rec $ENTRY; |
---|
| 253 | else |
---|
| 254 | translation_file $ENTRY |
---|
| 255 | fi; |
---|
| 256 | done; |
---|
| 257 | fi; |
---|
[2] | 258 | } |
---|
| 259 | |
---|
| 260 | #-----[ translation_directory ]-------------------------------------- |
---|
| 261 | # input : dir component |
---|
| 262 | # output : - |
---|
| 263 | |
---|
| 264 | function translation_directory () |
---|
| 265 | { |
---|
| 266 | touch $SED_SCRIPT; |
---|
| 267 | |
---|
| 268 | translation_script $1 $2; |
---|
| 269 | translation_directory_rec $1/$2; |
---|
| 270 | |
---|
| 271 | #cat $SED_SCRIPT; |
---|
| 272 | rm $SED_SCRIPT; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | |
---|
| 276 | #-----[ main ]------------------------------------------------------- |
---|
| 277 | # input : all parameters |
---|
| 278 | # output : - |
---|
| 279 | |
---|
| 280 | function main () |
---|
| 281 | { |
---|
| 282 | test_usage $*; |
---|
| 283 | |
---|
| 284 | echo "New : $2"; |
---|
| 285 | |
---|
| 286 | # Create directory |
---|
| 287 | mkdir -p $1/$2; |
---|
| 288 | |
---|
| 289 | #Copy |
---|
[15] | 290 | cp -r $SOURCE_DIR $TMP_DIR; |
---|
| 291 | find $TMP_DIR -iname ".svn" -type d -exec rm -fr '{}' \; &> /dev/null |
---|
| 292 | mv $TMP_DIR/* $1/$2; |
---|
| 293 | rmdir $TMP_DIR |
---|
[2] | 294 | |
---|
[15] | 295 | |
---|
[2] | 296 | #translation |
---|
| 297 | rename_directory "$1/$2" $SOURCE_FILE $2; |
---|
| 298 | translation_directory $1 $2; |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | #-----[ body ]------------------------------------------------------- |
---|
| 302 | main $*; |
---|