1217309Snwhitehorn#! /bin/sh
2251843Sbapt# $Id: headers-sh.in,v 1.11 2011/10/18 23:49:13 tom Exp $
3217309Snwhitehorn##############################################################################
4220749Snwhitehorn# Copyright (c) 2004-2007,2011 Thomas E. Dickey                              #
5217309Snwhitehorn#                                                                            #
6217309Snwhitehorn# Permission is hereby granted, free of charge, to any person obtaining a    #
7217309Snwhitehorn# copy of this software and associated documentation files (the "Software"), #
8217309Snwhitehorn# to deal in the Software without restriction, including without limitation  #
9217309Snwhitehorn# the rights to use, copy, modify, merge, publish, distribute, distribute    #
10217309Snwhitehorn# with modifications, sublicense, and/or sell copies of the Software, and to #
11217309Snwhitehorn# permit persons to whom the Software is furnished to do so, subject to the  #
12217309Snwhitehorn# following conditions:                                                      #
13217309Snwhitehorn#                                                                            #
14217309Snwhitehorn# The above copyright notice and this permission notice shall be included in #
15217309Snwhitehorn# all copies or substantial portions of the Software.                        #
16217309Snwhitehorn#                                                                            #
17217309Snwhitehorn# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
18217309Snwhitehorn# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,   #
19217309Snwhitehorn# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL    #
20217309Snwhitehorn# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER      #
21217309Snwhitehorn# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING    #
22217309Snwhitehorn# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER        #
23217309Snwhitehorn# DEALINGS IN THE SOFTWARE.                                                  #
24217309Snwhitehorn#                                                                            #
25217309Snwhitehorn# Except as contained in this notice, the name(s) of the above copyright     #
26217309Snwhitehorn# holders shall not be used in advertising or otherwise to promote the sale, #
27217309Snwhitehorn# use or other dealings in this Software without prior written               #
28217309Snwhitehorn# authorization.                                                             #
29217309Snwhitehorn##############################################################################
30217309Snwhitehorn#
31217309Snwhitehorn# Adjust includes for header files that reside in a subdirectory of
32217309Snwhitehorn# /usr/include, etc.
33217309Snwhitehorn#
34217309Snwhitehorn# Parameters (the first case creates the sed script):
35217309Snwhitehorn#	$1 is the target directory
36217309Snwhitehorn#	$2 is the source directory
37217309Snwhitehorn# or (the second case does the install, using the sed script):
38217309Snwhitehorn#	$1 is the script to use for installing
39217309Snwhitehorn#	$2 is the target directory
40217309Snwhitehorn#	$3 is the source directory
41217309Snwhitehorn#	$4 is the file to install, editing source/target/etc.
42217309Snwhitehorn
43217309SnwhitehornPACKAGE=@PACKAGE@
44217309SnwhitehornPKGNAME=@PACKAGE_PREFIX@
45217309SnwhitehornCONFIGH=@PACKAGE_CONFIG@
46220749SnwhitehornSUB_INC=@SUB_INC@
47217309Snwhitehorn
48251843Sbapt: ${TMPDIR:=/tmp}
49251843Sbapt
50217309SnwhitehornTMPSED=headers.sed
51217309Snwhitehorn
52217309SnwhitehornDIGIT=0123456789
53217309Snwhitehornalpha=abcdefghijklmnopqrstuvwxyz
54217309SnwhitehornALPHA=ABCDEFGHIJKLMNOPQRSTUVWXYZ
55217309Snwhitehorn
56217309Snwhitehornalnum=_${DIGIT}${alpha}
57217309SnwhitehornALNUM=_${DIGIT}${ALPHA}
58217309SnwhitehornMIXED=_${DIGIT}${ALPHA}${alpha}
59217309Snwhitehorn
60220749Snwhitehornpkgname=`echo "$PKGNAME" | tr "$ALPHA" "$alpha"`
61220749Snwhitehorn
62217309Snwhitehornif test $# = 2 ; then
63217309Snwhitehorn	rm -f $TMPSED
64217309Snwhitehorn	DST=$1
65217309Snwhitehorn	REF=$2
66217309Snwhitehorn	LEAF=`basename $DST`
67217309Snwhitehorn
68217309Snwhitehorn	# map the include-directory, if needed, to the subdirectory
69217309Snwhitehorn	case $DST in
70217309Snwhitehorn	/*/include/$LEAF)
71217309Snwhitehorn		END=`basename $DST`
72217309Snwhitehorn		for i in $REF/*.h
73217309Snwhitehorn		do
74217309Snwhitehorn			NAME=`basename $i`
75217309Snwhitehorn			echo "s/<$NAME>/<$END\/$NAME>/g" >> $TMPSED
76217309Snwhitehorn		done
77217309Snwhitehorn		;;
78217309Snwhitehorn	*)
79220749Snwhitehorn		touch $TMPSED
80217309Snwhitehorn		;;
81217309Snwhitehorn	esac
82217309Snwhitehorn
83217309Snwhitehorn	# cannot do _this_ in -e options:
84217309Snwhitehorn	cat >headers.tmp <<EOF
85217309Snwhitehorns/^#[^ ][^ ]*//
86217309Snwhitehorns/[^'$MIXED']/ /g
87217309Snwhitehorns/[ 	][ 	]*/ /g
88217309Snwhitehorns/^ //
89217309Snwhitehorns/ $//
90217309Snwhitehorn:split
91217309Snwhitehorn	h
92217309Snwhitehorn	s/ .*//
93217309Snwhitehorn	p
94217309Snwhitehorn	t next
95217309Snwhitehorn	b done
96217309Snwhitehorn:next
97217309Snwhitehorn	x
98217309Snwhitehorn	s/^[^ ][^ ]* //
99217309Snwhitehorn	t split
100217309Snwhitehorn:done
101217309SnwhitehornEOF
102217309Snwhitehorn	# pick up autoconf-style symbols used in the application's headers
103251843Sbapt	for name in $REF/*.h
104217309Snwhitehorn	do
105251843Sbapt		sed -e 's/^[ 	][ 	]*#[ 	][ 	]*/#/' $name \
106217309Snwhitehorn		| egrep '^#(if|ifdef|ifndef|elif)' \
107217309Snwhitehorn		| sed	-f headers.tmp \
108217309Snwhitehorn		| sort -u \
109217309Snwhitehorn		| egrep '^(HAVE_|NEED_|NO_|ENABLE_|DISABLE_)' \
110217309Snwhitehorn		| sed	-e 's%^\(.*\)%s/\\<\1\\>/'${PKGNAME}'_\1/g%' >>$TMPSED
111217309Snwhitehorn	done
112217309Snwhitehorn	rm -f headers.tmp
113217309Snwhitehorn
114217309Snwhitehorn	# pick up autoconf-defined symbols in the config.h file
115217309Snwhitehorn	for name in `
116217309Snwhitehorn	egrep '^#define[ 	][ 	]*['$ALNUM']' $REF/$CONFIGH \
117220749Snwhitehorn		| sed \
118220749Snwhitehorn			-e 's/^#define[ 	][ 	]*//' \
119217309Snwhitehorn			-e 's/[ 	].*//' \
120217309Snwhitehorn		| egrep -v "^${PACKAGE}_" \
121251843Sbapt		| sort -u`
122217309Snwhitehorn	do
123217309Snwhitehorn		echo "s/\\<$name\\>/${PKGNAME}_$name/g" >>$TMPSED
124217309Snwhitehorn	done
125217309Snwhitehorn
126220749Snwhitehorn	if test "$SUB_INC" = yes
127220749Snwhitehorn	then
128220749Snwhitehorn		echo "s,#include <${pkgname}_,#include <${PACKAGE}/${pkgname}_," >>$TMPSED
129220749Snwhitehorn	fi
130220749Snwhitehorn
131251843Sbapt	echo '/_FILE_OFFSET_BITS/d' >>$TMPSED
132251843Sbapt
133217309Snwhitehorn	# reduce the count if possible, since some old sed's limit is 100 lines
134217309Snwhitehorn	sort -u $TMPSED >headers.tmp
135217309Snwhitehorn	mv headers.tmp $TMPSED
136217309Snwhitehornelse
137217309Snwhitehorn	PRG=""
138217309Snwhitehorn	while test $# != 3
139217309Snwhitehorn	do
140217309Snwhitehorn		PRG="$PRG $1"; shift
141217309Snwhitehorn	done
142217309Snwhitehorn
143217309Snwhitehorn	DST=$1
144217309Snwhitehorn	REF=$2
145217309Snwhitehorn	SRC=$3
146217309Snwhitehorn
147217309Snwhitehorn	SHOW=`basename $SRC`
148251843Sbapt	TMPSRC=$TMPDIR/${SHOW}-text$$
149251843Sbapt	TMPEDT=$TMPDIR/${SHOW}-edit$$
150251843Sbapt	TMPTMP=$TMPDIR/${SHOW}-temp$$
151217309Snwhitehorn
152217309Snwhitehorn	echo "	... $SHOW"
153217309Snwhitehorn	test -f $REF/$SRC && SRC="$REF/$SRC"
154217309Snwhitehorn
155217309Snwhitehorn	rm -f $TMPSRC
156251843Sbapt	cat $SRC >$TMPSRC
157251843Sbapt
158251843Sbapt	tmp1=1
159251843Sbapt	while true
160251843Sbapt	do
161251843Sbapt		tmp2=`expr $tmp1 + 49`
162251843Sbapt		if test $tmp1 = 1
163251843Sbapt		then
164251843Sbapt			sed "${tmp2}q" $TMPSED >$TMPEDT
165251843Sbapt		else
166251843Sbapt			sed "1,${tmp1}d; ${tmp2}q" $TMPSED >$TMPEDT
167251843Sbapt		fi
168251843Sbapt		test -s $TMPEDT || break
169251843Sbapt		sed -f $TMPEDT $TMPSRC > $TMPTMP
170251843Sbapt		mv $TMPTMP $TMPSRC
171251843Sbapt		tmp1=$tmp2
172251843Sbapt	done
173251843Sbapt
174217309Snwhitehorn	NAME=`basename $SRC`
175217309Snwhitehorn
176217309Snwhitehorn	# Just in case someone gzip'd manpages, remove the conflicting copy.
177217309Snwhitehorn	test -f $DST/$NAME.gz && rm -f $DST/$NAME.gz
178217309Snwhitehorn
179220749Snwhitehorn	if test "$SUB_INC" = yes
180220749Snwhitehorn	then
181220749Snwhitehorn		case $NAME in #(vi
182220749Snwhitehorn		${pkgname}_*) #(vi
183220749Snwhitehorn			case "$PRG" in #(vi
184220749Snwhitehorn			*install*)
185220749Snwhitehorn				test -d $DST/$PACKAGE || mkdir -p $DST/$PACKAGE 
186220749Snwhitehorn				;;
187220749Snwhitehorn			esac
188220749Snwhitehorn			NAME=$PACKAGE/$NAME
189220749Snwhitehorn			;;
190220749Snwhitehorn		*)
191220749Snwhitehorn			NAME=$PACKAGE.h
192220749Snwhitehorn			;;
193220749Snwhitehorn		esac
194220749Snwhitehorn	fi
195220749Snwhitehorn
196217309Snwhitehorn	eval $PRG $TMPSRC $DST/$NAME
197251843Sbapt	rm -f $TMPEDT $TMPTMP $TMPSRC
198217309Snwhitehornfi
199220749Snwhitehorn# vile:ts=4 sw=4
200