Skip to content
Snippets Groups Projects
Commit 7c1de2ba authored by Joan Vallvé Navarro's avatar Joan Vallvé Navarro
Browse files

script working!

parent 5661c55c
No related branches found
No related tags found
1 merge request!427Resolve "Wolf license"
Pipeline #7276 failed
This commit is part of merge request !427. Comments created here will be created in the context of that merge request.
// Copyright (C) 2021-2023 Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
// Authors: Joan Solà Ortega (jsola@iri.upc.edu)
// All rights reserved.
//
// This file is part of WOLF
// WOLF is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
#!/bin/bash #!/bin/bash
line_start_mark="//--------LICENSE_START--------"
line_end_mark="//--------LICENSE_END--------"
#options #options
tmp=false tmp=false
mode="none"
path="" path=""
#recursive=true #recursive=true
license="" license=""
remove=false
for i in "$@"; do for i in "$@"; do
case $i in case $i in
...@@ -21,6 +24,22 @@ for i in "$@"; do ...@@ -21,6 +24,22 @@ for i in "$@"; do
license="${i#*=}" license="${i#*=}"
shift # past argument=value 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
;;
# --non-recursive) # --non-recursive)
# recursive=false # recursive=false
# shift # past argument=value # shift # past argument=value
...@@ -45,7 +64,7 @@ else ...@@ -45,7 +64,7 @@ else
fi fi
if [ "$license" == "" ]; then if [ "$license" == "" ]; then
echo "Error: Please, provide the path to the folder containing the code with --license-header=/my/license/header/file.txt" echo 'Error: Please, provide the path to the folder containing the code with --license-header=/my/license/header/file.txt'
exit 1 exit 1
else else
if [ -f "$license" ]; then if [ -f "$license" ]; then
...@@ -57,9 +76,16 @@ else ...@@ -57,9 +76,16 @@ else
fi fi
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) # PATH (AND tmp FOLDER)
folder=$path folder=$path
if $tmp; then if [ $tmp == true ]; then
echo "Creating temporary folder: ${path}_tmp" echo "Creating temporary folder: ${path}_tmp"
mkdir -pv ${path}_tmp mkdir -pv ${path}_tmp
cp -a $path/. ${path}_tmp cp -a $path/. ${path}_tmp
...@@ -67,17 +93,34 @@ if $tmp; then ...@@ -67,17 +93,34 @@ if $tmp; then
fi fi
# DETECT AND REMOVE EXISTING LICENSE # DETECT AND REMOVE EXISTING LICENSE
#TODO 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 # ADD CONTENT OF license-file AT THE BEGINNING OF CODE FILES
echo "Recursively adding license header to all files (.c, .cpp, .h, .hpp)" 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') for i in $(find $folder -name '*.c' -or -name '*.cpp' -or -name '*.h' -or -name '*.hpp')
do do
if grep -Fxq "//LICENSE_START" $i if grep -Fxq ${line_start_mark} $i; then
then
echo "skippping ${i}" echo "skippping ${i}"
else else
( echo $'//LICENSE_START\n'; cat ${license}; echo $'\n//LICENSE_END\n'; cat $i ) > temp_file ( echo ${line_start_mark}$'\n//'; cat ${license}; echo $'//\n'${line_end_mark}; cat $i ) > temp_file
mv temp_file $i mv temp_file $i
fi fi
done done
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment