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
fa95b1
#
fa95b1
# This program is free software; you can redistribute it and/or modify
fa95b1
# it under the terms of the GNU General Public License as published by
dcd347
# the Free Software Foundation; either version 2 of the License, or (at
dcd347
# your option) any later version.
fa95b1
#
74a058
# This program is distributed in the hope that it will be useful, but
74a058
# 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
dcd347
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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
}