########################################################################
#                                                                      #
#               This software is part of the ast package               #
#                 Copyright (c) 2006-2012 Roland Mainz                 #
#                      and is licensed under the                       #
#                 Eclipse Public License, Version 1.0                  #
#                    by AT&T Intellectual Property                     #
#                                                                      #
#                A copy of the License is available at                 #
#          http://www.eclipse.org/org/documents/epl-v10.html           #
#         (with md5 checksum b35adb5213ca9657e911e9befb180842)         #
#                                                                      #
#                                                                      #
#                 Roland Mainz <roland.mainz@nrubsig.org>              #
#                                                                      #
########################################################################

#
# Copyright (c) 2006, 2012, Roland Mainz. All rights reserved.
#

#
# Script buld fix for CR #6733971 ("Lint targets not POSIX shell (=ksh93) safe in OS/Net")
#

# Solaris needs /usr/xpg6/bin:/usr/xpg4/bin because the tools in /usr/bin are not POSIX-conformant
export PATH='/usr/xpg6/bin:/usr/xpg4/bin:/bin:/usr/bin'

# Make sure all math stuff runs in the "C" locale to avoid problems
# with alternative # radix point representations (e.g. ',' instead of
# '.' in de_DE.*-locales). This needs to be set _before_ any
# floating-point constants are defined in this script).
if [[ "${LC_ALL-}" != '' ]] ; then
	export \
		LC_MONETARY="${LC_ALL}" \
		LC_MESSAGES="${LC_ALL}" \
		LC_COLLATE="${LC_ALL}" \
		LC_CTYPE="${LC_ALL}"
		unset LC_ALL
fi
export LC_NUMERIC='C'


typeset -r filelist='cr_6733971_touched_files.txt'

function fatal_error
{
	print -u2 "${progname}: $*"
	exit 1
}

function filter_makefile
{
	sed 's/($(LHEAD)/$(LHEAD)/g;s/$(LTAIL))/$(LTAIL)/g;s/#.*[[:blank:]]*ident.*SMI.*//' |
	sed 's/Copyright [[:digit:]][[:digit:]][[:digit:]][[:digit:]]-[[:digit:]][[:digit:]][[:digit:]][[:digit:]]/Copyright 2009/g' |
	sed 's/Copyright [[:digit:]][[:digit:]][[:digit:]][[:digit:]], [[:digit:]][[:digit:]][[:digit:]][[:digit:]]/Copyright 2009/g' |
	sed 's/Copyright [[:digit:]][[:digit:]][[:digit:]][[:digit:]]/Copyright 2009/g' |
	cddl_fix
	return 0
}

function cddl_fix
{
	typeset s
	typeset in_cddl=false
	
	while IFS='' read -r s ; do
		if [[ "$s" == "# CDDL HEADER START" ]] ; then
			in_cddl=true
			continue
		fi
		
		if ${in_cddl} && [[ "$s" == "# CDDL HEADER END" ]] ; then
			in_cddl=false

cat <<+++
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
+++
			continue
		fi
		
		${in_cddl} && continue
		
		printf "%B\n" "s"
	done
	
	return 0
}

function build_filelist
{
	ggrep -r -l -E '(\(\$\(LHEAD\)|\$\(LTAIL\)\))' "$SRC" | egrep -v '/(\.svn|\.hg)' >${filelist}
	return 0
}

function show_diffs
{
	set -o errexit # safeguard against amok script runs
	
	typeset filename
	
	[[ -r "${filelist}" ]] || fatal_error "Can't open ${filelist}."
	
	cat "${filelist}" | while read filename ; do
		[[ -r "${filename}" ]] || fatal_error "Can't open source file ${filename}."
		
		diff -u "${filename}" <(cat "${filename}" | filter_makefile) || true
	done
	
	return 0
}

function build_diffs
{
	set -o errexit # safeguard against amok script runs
	
	typeset filename
	typeset tmpfilename="/tmp/buildcr6733971fix_$PID_$$.tmp"
	
	[[ -r "${filelist}" ]] || fatal_error "Can't open ${filelist}."
	
	cat "${filelist}" | while read filename ; do
		[[ -r "${filename}" ]] || fatal_error "Can't open source file ${filename}."
		
		str="$(filter_makefile <"${filename}")"
		printf "%B\n" str >"${filename}"
	done
	
	return 0
}


function main_dispatch
{
	case $1 in
		build_filelist)
			build_filelist
			return 0
			;;
		show_diffs)
			show_diffs
			return 0
			;;
		build_diffs)
			build_diffs
			return 0
			;;
		*)
			fatal_error "Unknown option ${1}."
			;;
	esac

	return 0
}




## main
typeset -r progname="buildcr6733971fix.sh"

# prechecks
[[ "$SRC" != "" ]] || fatal_error "SRC not set."
[[ -d "$SRC" ]] || fatal_error "SRC not a directory."

main_dispatch "$@"
exit $?

