
# convert source directory is unspecified.
if [ "$1" == "" ]; then
  echo "$0 <source directory>"
  exit 0
fi
INPUT_DIR=`echo $1 | cut -d "/" -f 1`

# skip "./" path prefix.
if [ "$INPUT_DIR" == "." ]; then
  INPUT_DIR=`echo $1 | cut -d "/" -f 2`
fi

# avoid work on root path by to confirm path does start with "/".
# Known bug: If you as .//****/ as to describe it is determined erroneously.
if [ "$INPUT_DIR" == "" ]; then
  echo "[!!COUTION!!] Cannot set absolute path, because danger work."
  echo "Please set base on current path, You can set ./****/ or ****/"
  exit 0
fi

# The first run time will create a duplicate directory as a destination.
if [ ! -e $INPUT_DIR\_comment_removed ]; then
  cp -ra $INPUT_DIR $INPUT_DIR\_comment_removed
fi

# Start the converting.
rm ./remove.sh
# It supports following extensions. [C] .c .h [C++] .cpp .hpp .cxx .hxx .cc .hh .cp .hp [CUDA C/C++] .cu .cuh
# I hate to use the symbol characters in filenames. Therefore, .c++ and .h++ is not supported.
# Uppercase letters are used to the old file name format (e.g. 8.3 DOS filename format),
# but do not support because I does not want a mix of extension of uppercase and lowercase letters.
find $INPUT_DIR/ -type f -name "*.[ch]" -o -name "*.[ch]pp" -o -name "*.[ch]xx" -o -name "*.c[cup]" -o -name "*.h[hp]" -o -name "*.cuh" | awk '{print "cat HEADER > "$1"; gcc -fpreprocessed -dD -E -P "$1" >> "$1}' | sed -E "s/> "$INPUT_DIR"/> "$INPUT_DIR"_comment_removed/g" >> ./remove.sh
chmod -c 777 ./remove.sh
./remove.sh

