#!/bin/sh
#set -x
# Hi!
# this shell script to grind an installed cross compiler into a
# shape suitable for running on a pc.
#
# First you've got to build the cross compiler, probably with a configure
# like
# configure --target=h8300-coff-hms --host=go32 --build=sun4
#
# then you make all info install install-info
#
# then you've also got a dos tree, since install runs this script


option=$1
exec_prefix=$2
target_alias=$3
prefix=$4
srcdir=$5
srcdemodir=$6
multilib=$7

RELEASE="frv-050408-1"

objdir=`pwd`

PATH="${objdir}:$PATH"
export PATH

# if srcdir is relative, make it absolute
if [ "`echo $srcdir | sed -n '/^\.\./p'`" != "" ]; then
	srcdir=`pwd`/$srcdir
fi

# first build the directory structure
dosverytop=${prefix}/H-dos
dostop=${dosverytop}/${target_alias}
bin=$dostop/bin
lib=$dostop/lib
info=$dostop/info
demo=$dostop/demo
readme=$dostop/readme
include=$dostop/include




if [ $option = "A" ] ; then
rm -rf $dostop

for dir in $dosverytop $dostop $bin $lib $info $include $readme $demo ; do
	if [ ! -d $dir ] ; then	
		mkdir $dir
	fi
done


# and now dig around and find cc1, cc1plus and cpp
# which in my release went into
# /usr/cygnus/comp-tools-940204/H-i386-go32/lib/gcc-lib/h8300-coff-hms/cygnus-2.5.90-940415/
# to get the 2.5.90-940415 we have to look inside version.c in the gcc directory
# with this hairy sed thing.  

mainversion=`cat ${srcdir}/../gcc/version.c | sed -e "s:.*= \"::" | sed -e "s:\";::"`

cc1dirname=${exec_prefix}/lib/gcc-lib/${target_alias}/${mainversion}
for i in cc1 cc1plus cpp ; do
	cp $cc1dirname/$i $bin
	i386-go32-strip $bin/$i
	./coff2exe ${bin}/$i
	rm -rf ${bin}/$i
done

# now copy in the other standard programs needed to run.  This includes
# go32 and asynctsr, a make perhaps ?

for i in asynctsr.com go32.exe make.exe emu387 info.exe  ; do	
	cp $srcdir/exe/$i $bin
done

srclibpath=${exec_prefix}/${target_alias}/lib
# if there's a libg++ then copy the libiosteam headers

if [ -f $srclibpath/libg++.a ] ; then
	# Copy only the relevant bits of the include directory
	mkdir $include/cxx
	cp -r ${prefix}/include/g++/* $include/cxx


# move some of the conflicting names

	mv $include/cxx/Complex.h $include/cxx/_complex.h
	mv $include/cxx/iostreamP.h $include/cxx/iostreap.h
	mv $include/cxx/Regex.h $include/cxx/_regex.h

# and make the header.gcc file
	echo Complex.h _complex.h >$include/cxx/header.gcc
	echo iostreamP.h iostreap.h >>$include/cxx/header.gcc
	echo Regex.h  _regex.h >>$include/cxx/header.gcc

fi

# now all the library files

cp -r ${srclibpath}/* $lib

# but don't copy libiberty 

find $lib -name "libiberty.a"  -exec rm {} \;

# kill the libg's they're just copies of libc.
find $lib -name "libg.a" -exec rm {} \;
cp $cc1dirname/specs $lib

# copy the libgccs too, they're kept in the cc1 directory and 
# below

# this doesn't always work, so we won't use it
#( cd $cc1dirname; find .  -name "*.a" -exec cp {} $lib/{} \; )

# the subshell makes things nice, so we'll keep it
(
  cd $cc1dirname
  for foo in `find . -name "*.[ao]" -print`; do
      cp $foo $lib/$foo
  done
)
     


# copy the binaries out from the installed directories into bin
# cut off the target_alias from the front and change any illegal chars.


# first all the ones in what could look like
# /usr/cygnus/comp-tools-940204/H-i386-go32/bin/h8300-coff-hms-*


for i in ${exec_prefix}/bin/${target_alias}* ; do
	newname=`echo $i | sed -e "s:${exec_prefix}/bin/${target_alias}-::" | sed -e "s:\+\+:xx:"`
	cp $i ${bin}/${newname}
	i386-go32-strip ${bin}/${newname}
	./coff2exe ${bin}/${newname}
	rm -f ${bin}/${newname}
done



# and the info stuff. This was lovingly built with -mnosplit in our object tree
# but the install directory will be polluted with the info-n*  from various other
# builds, so we grab from the build directory. - also the hitachi releases have
# slightly different doc.

# there is a problem, some needed files like license.info aren't built
# for cross - license.info insn't made because it is generated by texinfo, and
# we don't build texinfo. We take this from the installed directory.  Gross but 
# true.

for i in \
	../binutils/binutils.info	\
	../etc/intro.info		\
	../gas/doc/as.info		\
	../gas/doc/gasp.info		\
	../gcc/cpp.info			\
	../gcc/gcc.info			\
	../gcc/reno.info		\
	../gdb/doc/gdb.info		\
	../ld/ld.info			\
	../newlib/libc/libc.info	\
	../newlib/libm/libm.info	\
	../libg++/libg++.info           \
	../libgloss/doc/porting.info    \
	../libio/iostream.info          \
	../readline/doc/history.info	\
	../readline/doc/readline.info	\
	; do
	
	cp $i $info/`echo $i | sed -e "s:.info:.inf:" | sed -e "s:g\+\+:xx:" | sed -e s:.*/::`
done

cp ./license.info $info/license.inf

# What's even more gross is getting the make doc...

makeinfo --no-split -I${srcdir}/../make ${srcdir}/../make/make.texinfo -o $info/make.inf


# build the dir entry with the new names, we copy the original gen-info-dir prog
# and run from here because our file name extension has changed
echo P1
cat ${srcdir}/../texinfo/gen-info-dir | sed -e s:\*.info:\*.inf: >gen-info-dir
sh ./gen-info-dir ${info} ${srcdir}/../texinfo/dir.info-template >tmp
cat tmp | sed -e s:ibg\+\+:ibgxx: > $info/dir.inf

# Now the includes

cp -r ${exec_prefix}/${target_alias}/include/* ${include}

# and the gcc includes

cp -r ${cc1dirname}/include/*.h ${include}

# dosify the files in the include directory
for file in `find $include -type f -print`; do
    ./unix2dos $file /tmp/conv$$
    cp /tmp/conv$$ $file
done

# a nice demo, this is very target dependent so we run the script in the 
# demo directory to build the release.

# something that is common is the syscalls layout
mkdir ${dostop}/src
mkdir ${dostop}/src/syscalls
./unix2dos ${srcdir}/../newlib/stub/shared/glue.c ${dostop}/src/syscalls/glue.c

echo cd ${srcdir}/demo/${srcdemodir} sh ./RELEASE ${dostop} ${srcdir}/demo/${srcdemodir}
(cd ${srcdir}/demo/${srcdemodir}; sh ./RELEASE ${dostop} ${srcdir}/demo/${srcdemodir})
echo P2

# copy the install program and the readme file

cp ${srcdir}/exe/install.exe ${dostop}

./unix2dos  ${srcdir}/readme/readme ${dostop}/readme/readme
./unix2dos  ${srcdir}/readme/cygnus ${dostop}/readme/cygnus
./unix2dos  ${srcdir}/readme/copying ${dostop}/readme/copying
./unix2dos  ${srcdir}/readme/problems.txt ${dostop}/readme/problems.txt
./unix2dos  ${srcdir}/readme/send-pr.txt ${dostop}/readme/sendpr.txt

# Mark the type and release number

echo ${target_alias} >${dostop}/rel
echo "$RELEASE" >>${dostop}/rel
./unix2dos ${dostop}/rel /tmp/conv$$
cp /tmp/conv$$ ${dostop}/rel


fi
# above matches if [ $option = "A" ]

# change the names of the directories and files to work in a DOS environment
du -a ${dostop}/* | ./doscheck
./dosifytree ${dostop} >./script
cat script
sh ./script
# and a final sanity check

du -a ${dostop}/* | ./doscheck

# stuff into a disk structure

disking=${dostop}/../disks/${target_alias}
rm -rf ${disking}
mkdir -p ${disking}
./diskit ${dostop} /tmp/disking$$ ${disking}
rm -rf /tmp/disking$$
