Blame Scripts/Functions/cli_readFileContent.sh

4f3327
#!/bin/bash
4f3327
#
6713ca
# cli_readFileContent.sh -- This function outputs content of files,
6713ca
# passed as first argument, to standard output as specified by second
4f3327
# argument.
4f3327
#
4f3327
# Copyright (C) 2009-2011 Alain Reguera Delgado
4f3327
# 
4f3327
# This program is free software; you can redistribute it and/or
4f3327
# modify it under the terms of the GNU General Public License as
4f3327
# published by the Free Software Foundation; either version 2 of the
4f3327
# License, or (at your option) any later version.
4f3327
# 
4f3327
# This program is distributed in the hope that it will be useful, but
4f3327
# WITHOUT ANY WARRANTY; without even the implied warranty of
4f3327
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
4f3327
# General Public License for more details.
4f3327
#
4f3327
# You should have received a copy of the GNU General Public License
4f3327
# along with this program; if not, write to the Free Software
4f3327
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
4f3327
# USA.
4f3327
# 
4f3327
# ----------------------------------------------------------------------
4f3327
# $Id$
4f3327
# ----------------------------------------------------------------------
4f3327
4f3327
function cli_readFileContent {
4f3327
4f3327
    local FILES="$1"
bf2ee9
    local PATTERN='Copyright (\(C\)|©) [0-9]+(-[0-9]+)? .+'
4f3327
6713ca
    # Verify existence of files but don't stop if it doesn't exist.
4f3327
    cli_checkFiles "$FILES"
4f3327
bf2ee9
    # Print content of files to standard output.
4f3327
    case "$2" in
4f3327
bf2ee9
        '--copyright-line' )
bf2ee9
            cat "$FILES" | egrep "^ +${PATTERN}$" | head -n 1 | sed -r 's!^ +!!'
4f3327
            ;;
4f3327
4f3327
        '--last-line' )
4f3327
            cat "$FILES" | tail -n 1
4f3327
            ;;
4f3327
bf2ee9
        '--copyright-year' )
bf2ee9
            if [[ $(cli_readFileContent "$FILES" '--copyright-line') =~ "^${PATTERN}$" ]];then
bf2ee9
                cli_readFileContent "$FILES" '--copyright-line' | cut -d' ' -f3
bf2ee9
            fi
bf2ee9
            ;;
bf2ee9
bf2ee9
        '--copyright-holder' )
bf2ee9
            if [[ $(cli_readFileContent "$FILES" '--copyright-line') =~ "^${PATTERN}$" ]];then
bf2ee9
                cli_readFileContent "$FILES" '--copyright-line' | cut -d' ' -f4-
4f3327
            fi
4f3327
            ;;
4f3327
4f3327
        '--all-lines' | * )
4f3327
            cat "$FILES"
bf2ee9
            ;;
bf2ee9
4f3327
    esac
4f3327
4f3327
}