#! /bin/bash

####################################################################################################
# 
# PyResistorColorCode - Python Electronic Tools
# Copyright (C) Salvaire Fabrice 2012
# 
####################################################################################################

root_dir=`python -c "import os ; print os.path.dirname(os.path.dirname(os.path.realpath('$0')))"`

src='# PyResistorColorCode - Python Electronic Tools.*# Copyright (C) Salvaire Fabrice 2012'

dst='# PyResistorColorCode - Python Electronic Tools.\n# Copyright (C) 2012 Salvaire Fabrice\n#\n# This program is free software: you can redistribute it and/or modify\n# it under the terms of the GNU General Public License as published by\n# the Free Software Foundation, either version 3 of the License, or\n# (at your option) any later version.\n# \n# This program is distributed in the hope that it will be useful,\n# but WITHOUT ANY WARRANTY; without even the implied warranty of\n# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n# GNU General Public License for more details.\n# \n# You should have received a copy of the GNU General Public License\n# along with this program.  If not, see <http://www.gnu.org/licenses/>.'

for i in `find ${root_dir} -type f ! -wholename "*/.git/*" ! -wholename "*/.bzr/*" ! -name change-license-header  | sort -n` ; do
  grep -q 'Copyright (C) Salvaire Fabrice' $i;
  rc=$?
  # rc = 0 if match else 1
  if [ $rc == 0 ]; then
    echo $i
    # # if the first line copy the pattern to the hold buffer
    # 1h
    # # if not the first line then append the pattern to the hold buffer
    # 1!H
    # # if the last line then
    # $ {
    #     # copy from the hold to the pattern buffer
    #     g
    #     # do the search and replace
    #     s/src/dst/g
    #     # print
    #     p
    # }
    sed -n "1h;1!H;\${;g;s|$src|$dst|g;p;}" -i $i
  fi
done

####################################################################################################
#
# End
#
####################################################################################################
