diff --git a/scripts/license_manager.sh b/scripts/license_manager.sh new file mode 100644 index 0000000000000000000000000000000000000000..e66579eae8e85d2823dcefb64439eab907a8fa22 --- /dev/null +++ b/scripts/license_manager.sh @@ -0,0 +1,127 @@ +#!/bin/bash + +# ------ WOLF license_manager script ------ +# +# This script is used for managing the license headers of code files (.h, .c, .cpp, .hpp) +# This file is automatically called by the CI in gitlab. + +line_start_mark="//--------LICENSE_START--------" +line_end_mark="//--------LICENSE_END--------" + +#options +tmp=false +mode="none" +path="" +#recursive=true +license="" + +for i in "$@"; do + case $i in + --temp) + tmp=true + shift # past argument=value + ;; + --path=*) + path="${i#*=}" + shift # past argument=value + ;; + --license-header=*) + license="${i#*=}" + shift # past argument=value + ;; + --add) + mode="add" + if [ $mode == "update" ]; then + echo "Error: Script cannot be called with both options: --update or --add" + exit 1 + fi + shift # past argument=value + ;; + --update) + mode="update" + if [ $mode == "add" ]; then + echo "Error: Script cannot be called with both options: --update or --add" + exit 1 + fi + shift # past argument=value + ;; + *) + # unknown option + ;; + esac +done + +# check options +if [ "$path" == "" ]; then + echo 'Please, provide the path to the folder containing the code with --path=/my/folder/code' + exit 1 +else + if [ -d "$path" ]; then + echo "Valid path: ${path}" + else + echo "Error: ${path} not found. Can not continue." + exit 1 + fi +fi + +if [ "$license" == "" ]; then + echo 'Error: Please, provide the path to the folder containing the code with --license-header=/my/license/header/file.txt' + exit 1 +else + if [ -f "$license" ]; then + echo "Valid license header file: ${license} containing:" + cat ${license} + else + echo "Error: License header file ${license} not found. Can not continue." + exit 1 + fi +fi + +echo "mode: ${mode}" + +if [ $mode == "none" ]; then + echo "Error: Script should be called with one of the following options: --update or --add" + exit 1 +fi + +# PATH (AND tmp FOLDER) +folder=$path +if [ $tmp == true ]; then + echo "Creating temporary folder: ${path}_tmp" + mkdir -pv ${path}_tmp + cp -a $path/. ${path}_tmp + folder=${path}_tmp +fi + +# DETECT AND REMOVE EXISTING LICENSE +if [ $mode == "update" ]; then + echo "Recursely removing license header from all files (.c, .cpp, .h, .hpp)" + for i in $(find $folder -name '*.c' -or -name '*.cpp' -or -name '*.h' -or -name '*.hpp') + do + if grep -Fxq ${line_start_mark} $i + then + echo " Removing license header from file ${i}" + line_start="$(grep -wn $line_start_mark ${i} | head -n 1 | cut -d: -f1)" + line_end="$(grep -wn $line_end_mark ${i} | head -n 1 | cut -d: -f1)" + echo ${line_start} + echo ${line_end} + #echo "${line_start},${line_end}d" $i + #sed $('${line_start},${line_end}d)' $i + awk -v m=$line_start -v n=$line_end 'm <= NR && NR <= n {next} {print}' $i > tmpfile && mv tmpfile $i + cat $i + fi + done + #TODO +fi + +# ADD CONTENT OF license-file AT THE BEGINNING OF CODE FILES +echo "Recursively adding license header to all files (.c, .cpp, .h, .hpp)" +for i in $(find $folder -name '*.c' -or -name '*.cpp' -or -name '*.h' -or -name '*.hpp') +do + if grep -Fxq ${line_start_mark} $i; then + echo "skippping ${i}" + else + ( echo ${line_start_mark}$'\n//'; cat ${license}; echo $'//\n'${line_end_mark}; cat $i ) > temp_file + mv temp_file $i + fi +done \ No newline at end of file