Harald Welte | 3aa901d | 2018-08-13 18:32:36 +0200 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | # Usage: |
| 3 | # ../../move-asn1-headers.sh subdir_name File1.h File2.h ... |
| 4 | # All .h and .c files in the current directory are edited to use #include <...> |
| 5 | # style for the .h files given on the cmdline. The given .h files are also |
| 6 | # moved to ../include/<subdir_name>/ so that #include <...> will work. |
| 7 | |
| 8 | set -e |
| 9 | |
| 10 | base_dir="$(dirname "$0")" |
| 11 | |
| 12 | include_subdir="$1" |
| 13 | shift |
| 14 | |
| 15 | include_dir="$base_dir/include/$include_subdir" |
| 16 | mkdir -p "$include_dir" |
| 17 | echo "$PWD/*.h --> $include_dir" |
| 18 | |
| 19 | collect_sed_commands() { |
| 20 | while [ -n "$1" ]; do |
| 21 | fname="$1" |
| 22 | shift |
| 23 | |
| 24 | echo "s,^#include \"$fname\"$,#include <$include_subdir/$fname>," |
| 25 | done |
| 26 | } |
| 27 | |
| 28 | move_headers() { |
| 29 | echo mv $@ "$include_dir/" |
| 30 | mv $@ "$include_dir/" |
| 31 | } |
| 32 | |
| 33 | # Replace all `#include "foo.h"' with `#include <dir/foo.h>' locally |
| 34 | # - Collect sed commands to replace all header includes, for efficiency |
| 35 | cmds="$(mktemp)" |
| 36 | echo "collecting sed commands..." |
| 37 | collect_sed_commands $@ > "$cmds" |
| 38 | # - Run commands on all h and c files |
| 39 | echo "sed -i -f \"$cmds\" *.[hc]" |
| 40 | sed -i -f "$cmds" *.[hc] |
| 41 | rm "$cmds" |
| 42 | |
| 43 | # Now move sed'ed *.h files to the proper ../include/dir |
| 44 | move_headers $@ |