#!/bin/bash
if [ -z ${1+x} ];
    then
	echo -e "Error: Please provide a path!";
    else
	find $1 -type f -iname "*.man" -exec bash -c '
	    for file do
		FILENAME=$(basename $file | sed s/\\.man//g);
        	echo -en "$file";
        	echo "   ...changing values";
        	echo -n > ./temporary_file.tmp
        	echo "...write in temporary file";
        	while IFS='"'"''"'"' read -r line || [[ -n "$line" ]]; do
            	    COMP=$(echo "$line" | grep -E "\\\\(sub)*section\\s" | grep -v $FILENAME);
            	    if [ -n "$COMP" ];
            		then
                	    echo -E "$line" | sed '"'"'s/\(\\\(sub\)*section\s\)/\1'"'"'$FILENAME'"'"'_/g'"'"' >> ./temporary_file.tmp;
            		else
                	    echo -E "$line" >> ./temporary_file.tmp
            	    fi;
        	done < "$file";
        	echo "...finished";
        	echo "...override original file";
        	cp -f ./temporary_file.tmp $file;
        	echo -e "done\n";
    	    done;
	' sh {} +
fi;
