#!/bin/sh
if [ "x$1" = "x" ]
then echo fixincludes: no output directory specified
exit 1
fi
LIB=${1}
shift
if [ ! -d $LIB ]; then
mkdir $LIB || {
echo fixincludes: output dir '`'$LIB"' cannot be created"
exit 1
}
else
( cd $LIB && touch DONE && rm DONE ) || {
echo fixincludes: output dir '`'$LIB"' is an invalid directory"
exit 1
}
fi
if test -r ./Makefile; then
target_canonical="`sed -n -e 's,^target[ ]*=[ ]*\(.*\)$,\1,p' < Makefile`"
fi
if test -z "${target_canonical}" ; then
if test -x ./config.guess ; then
target_canonical="`config.guess`" ; fi
test -z "${target_canonical}" && target_canonical=unknown
fi
export target_canonical
PWDCMD=pwd
case "`$PWDCMD`" in
//*)
PWDCMD="eval pwd | sed -e 's,.*/usr/,/usr/,'"
;;
esac
ORIGDIR=`${PWDCMD}`
FIXINCL=${ORIGDIR}/fixincl
export FIXINCL
case $LIB in
/*)
;;
*)
cd $LIB; LIB=`${PWDCMD}`
;;
esac
echo Fixing headers into ${LIB} for ${target_canonical} target
if ln -s X $LIB/ShouldNotExist 2>/dev/null; then
rm -f $LIB/ShouldNotExist
LINKS=true
elif ln -s X /tmp/ShouldNotExist 2>/dev/null; then
rm -f /tmp/ShouldNotExist
LINKS=true
else
LINKS=false
fi
if test $then
INPUTLIST="/usr/include"
else
INPUTLIST="$@"
fi
for INPUT in ${INPUTLIST} ; do
cd ${ORIGDIR}
cd ${INPUT} || continue
INPUT=`${PWDCMD}`
echo Finding directories and links to directories
dirs="."
levels=2
all_dirs=""
search_dirs=""
while [ -n "$dirs" ] && [ $levels -gt 0 ]
do
levels=`expr $levels - 1`
newdirs=
for d in $dirs
do
echo " Searching $INPUT/$d"
all_dirs="$all_dirs `find $d/. -type d -print | \
sed -e '/\/\.$/d' -e 's@/./@/@g'`"
theselinks=
$LINKS && \
theselinks=`find $d/. -type l -print | sed -e 's@/./@/@g'`
for d1 in $theselinks --dummy--
do
if [ -d $d1 ]
then
all_dirs="$all_dirs $d1"
if [ "`ls -ld $d1 | sed -n 's/.*-> //p'`" != "." ]
then
newdirs="$newdirs $d1"
search_dirs="$search_dirs $d1"
fi
fi
done
done
dirs="$newdirs"
done
dirs=
echo "All directories (including links to directories):"
echo $all_dirs
for file in $all_dirs; do
rm -rf $LIB/$file
if [ ! -d $LIB/$file ]
then mkdir $LIB/$file
fi
done
mkdir $LIB/root
treetops=". ${LIB}"
if $LINKS; then
echo 'Making symbolic directory links'
cwd=`${PWDCMD}`
for sym_link in $search_dirs; do
cd ${INPUT}
dest=`ls -ld ${sym_link} | sed -n 's/.*-> //p'`
cd ./`echo ${sym_link} | sed 's;/[^/]*$;;'`
(cd $dest) > /dev/null 2>&1
if [ $? = 0 ]; then
cd $dest
full_dest_dir=`${PWDCMD}`
cinput=`cd ${INPUT}; ${PWDCMD}`
if [ ${full_dest_dir} = ${cinput} ]; then
echo ${sym_link} '->' . ': Making self link'
rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
ln -s . ${LIB}/${sym_link} > /dev/null 2>&1
elif expr ${full_dest_dir} : "${cinput}/.*" > /dev/null; then
y=`echo ${full_dest_dir} | sed -n "s&${cinput}/&&p"`
dots=`echo "${sym_link}" |
sed -e 's@^./@@' -e 's@/./@/@g' -e 's@[^/][^/]*@..@g' -e 's@..$@@'`
echo ${sym_link} '->' $dots$y ': Making local link'
rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
ln -s $dots$y ${LIB}/${sym_link} > /dev/null 2>&1
else
echo ${sym_link} '->' root${full_dest_dir} ': Making rooted link'
if [ -d $LIB/root${full_dest_dir} ]
then true
else
dirname=root${full_dest_dir}/
dirmade=.
cd $LIB
while [ x$dirname != x ]; do
component=`echo $dirname | sed -e 's|/.*$||'`
mkdir $component >/dev/null 2>&1
cd $component
dirmade=$dirmade/$component
dirname=`echo $dirname | sed -e 's|[^/]*/||'`
done
fi
for file2 in $all_dirs; do
case $file2 in
${sym_link}/*)
dupdir=${LIB}/root${full_dest_dir}/`echo $file2 |
sed -n "s|^${sym_link}/||p"`
echo "Duplicating ${sym_link}'s ${dupdir}"
if [ -d ${dupdir} ]
then true
else
mkdir ${dupdir}
fi
;;
*)
;;
esac
done
parent=`echo "${sym_link}" | sed -e 's@/[^/]*$@@'`
libabs=`cd ${LIB}; ${PWDCMD}`
file2=`cd ${LIB}; cd $parent; ${PWDCMD} | sed -e "s@^${libabs}@@"`
dots=`echo "$file2" | sed -e 's@/[^/]*@../@g'`
rm -fr ${LIB}/${sym_link} > /dev/null 2>&1
ln -s ${dots}root${full_dest_dir} ${LIB}/${sym_link} > /dev/null 2>&1
treetops="$treetops ${sym_link} ${LIB}/root${full_dest_dir}"
fi
fi
done
fi
required=
set x $treetops
shift
while [ $ SRCDIR=`cd ${INPUT} ; cd $1 ; ${PWDCMD}`
export SRCDIR
FIND_BASE=$1
export FIND_BASE
shift
DESTDIR=`cd $1;${PWDCMD}`
export DESTDIR
shift
if [ -f ${DESTDIR}/DONE ]
then continue ; fi
touch ${DESTDIR}/DONE
echo Fixing directory ${SRCDIR} into ${DESTDIR}
cd ${INPUT}
required="$required `if $LINKS; then
find ${FIND_BASE}/. -name '*.h' \( -type f -o -type l \) -print
else
find ${FIND_BASE}/. -name '*.h' -type f -print
fi | \
sed -e 's;/\./;/;g' -e 's;//*;/;g' | \
${FIXINCL}`"
done
set x `echo $required`
shift
while [ $ newreq=
while [ $ cd ${INPUT}
cd $1
if [ -f $2 ] ; then
if [ -r $2 ] && [ ! -r $3 ]; then
cp $2 $3 >/dev/null 2>&1 || echo "Can't copy $2" >&2
chmod +w $3 2>/dev/null
chmod a+r $3 2>/dev/null
echo Copied $2
for include in `egrep '^[ ]* sed -e 's/^[ ]* do
dir=`echo $2 | sed -e s'|/[^/]*$||'`
dir2=`echo $3 | sed -e s'|/[^/]*$||'`
newreq="$newreq $1 $dir/$include $dir2/$include"
done
fi
fi
shift; shift; shift
done
set x $newreq
shift
done
echo 'Cleaning up DONE files.'
cd $LIB
find . -name DONE -exec rm -f '{}' ';'
echo 'Removing unneeded directories:'
cd $LIB
all_dirs=`find . -type d \! -name '.' -print | sort -r`
for file in $all_dirs; do
rmdir $LIB/$file > /dev/null 2>&1
done
done
if [ x${INSTALL_ASSERT_H} != x ] && [ -f ${srcdir}/assert.h ]
then
cd $ORIGDIR
rm -f include/assert.h
cp ${srcdir}/assert.h include/assert.h || exit 1
chmod a+r include/assert.h
fi