#  This script only works in the ./djgpp directory.

export HOME=.
export PATH_SEPARATOR=:
export PATH_EXPAND=y
export TEST_FINDS_EXE=y
export LD=ld
export NM=nm
export LN_S="cp -p"
export ac_cv_prog_LN_S="cp -p"
export ac_cv_func_fork=no
export ac_cv_func_mkfifo=no
export ac_cv_func_mknod=no
export ac_cv_func_mmap=no
export ac_cv_func_pipe=no
export ac_cv_func_vfork=no

# Do not allow that the BFD's configure script determine the
# host dependant file_ptr a.k.a. off_t type as BFD_HOST_64_BIT.
# This is the case if ftello64 and fseeko64 are found.  But DJGPP
# does not provide the full set of 64 bit file functions required
# for a working 64 bit BFD.
export ac_cv_func_fseeko64=${ac_cv_func_fseeko64=no}
export ac_cv_func_ftello64=${ac_cv_func_ftello64=no}
export ac_cv_have_decl_fseeko64=${ac_cv_have_decl_fseeko64=no}
export ac_cv_have_decl_ftello64=${ac_cv_have_decl_ftello64=no}



#
#  1: Adjust the configure scripts.
#

cat > script.sed << EOF
# For some reason the function does not work with bash 2.05b.
/^func_lalib_p/,/^}$/ {
  /test/ i\\
    case \$1 in\\
    *.la | *.lo)
  /GREP/ {
    s/$/;;/
    a\\
    *) false;;\\
    esac
  }
}

# The CR test for awk does not work for DJGPP.
/^ac_cs_awk_cr=/,/^$/ {
  /^fi$/ a\\
ac_cs_awk_cr=\$ac_cr
}

# AWK program above erroneously prepends '/' to C:/dos/paths
/# AWK program above erroneously prepends/,/esac/ {
  s/mingw/*djgpp* | mingw/
}

# Force the test for 'ln -s' to report 'cp -pf'.
/as_ln_s='ln -s'/ s/ln -s/cp -pf/
EOF


for file in ../ltmain.sh ../configure ../bfd/configure ../binutils/configure ../gas/configure ../gprof/configure ../ld/configure ../opcodes/configure; do
  if test ! -f ${file}.orig; then
    cp -vf ${file} ${file}.orig
    touch ${file}.orig -r ${file}
  fi
  sed -f ./script.sed ${file}.orig > ./file.out
  update ./file.out ${file}
  touch ${file} -r ${file}.orig
done
rm -vf ./file.out ./script.sed



#
#  2: Adjust the Makefile.ins and other files.
#

cat > script.sed << EOF
# Fix for ./bfd/Makefile.in
# Force the regenaration of libcoff.h
# to define obj_go32
/^\\\$(BFD_H):/ s/:/: stmp-lcoff-h/

# For html docs.
/^MAKEINFOHTML[ 	]*=/ s/$/ --no-split/

# Fixes for ./libiberty/Makefile.in
# ps support for libiberty docs.
/dvi-subdir[ 	]\\{1,\\}pdf-subdir/ s/dvi-subdir[ 	]\\{1,\\}pdf-subdir/& ps-subdir/

/^LIBIBERTY_PDFFILES[ 	]*=/ i\\
LIBIBERTY_PSFILES = libiberty.ps\\
\\
ps: \\\$(LIBIBERTY_PSFILES) ps-subdir\\


/^libiberty.pdf[ 	]*:/ i\\
libiberty.ps : ./libiberty.dvi \\\$(srcdir)/libiberty.texi \\\$(TEXISRC)\\
	dvips -o ./libiberty.ps ./libiberty.dvi\\

# Fixes for ./etc/Makefile.in.
/^HTMLFILES =.*configure.html$/ i\\
PSFILES = standards.ps configure.ps
/epstopdf/ s/[ 	]\\{1,\\}-outfile/ --outfile/
EOF


for file in ./../bfd/Makefile.in ./../bfd/doc/Makefile.in ./../binutils/doc/Makefile.in ./../etc/Makefile.in ./../gas/doc/Makefile.in ./../gprof/Makefile.in ./../ld/Makefile.in ./../libiberty/Makefile.in; do
  if test ! -f ${file}.orig; then
    cp -vf ${file} ${file}.orig
    touch ${file}.orig -r ${file}
  fi
  sed -f ./script.sed ${file}.orig > ./file.out
  update ./file.out ${file}
  touch ${file} -r ${file}.orig
done

rm -vf ./file.out ./script.sed



#
#  3: Adjust the config.in headers.
#

cat > script.sed << EOF
# DJGPP 2.03 needs the snprintf prototype.
/^#undef HAVE_DECL_SNPRINTF$/ c\\
#include <sys/version.h>\\
#if __DJGPP__ == 2 && __DJGPP_MINOR__ < 4\\
# define HAVE_DECL_SNPRINTF 0\\
#else\\
#undef HAVE_DECL_SNPRINTF\\
#endif
EOF


for file in ../bfd/config.in ../binutils/config.in ../gold/config.in ../libiberty/config.in; do
  if test ! -f ${file}.orig; then
    cp -vf ${file} ${file}.orig
    touch ${file}.orig -r ${file}
  fi
  sed -f ./script.sed ${file}.orig > ./file.out
  update ./file.out ${file}
  touch ${file} -r ${file}.orig
done
rm -vf ./file.out ./script.sed


cat > script.sed << EOF
# DJGPP 2.03 needs the snprintf prototype.
$ a\\
\\
#include <sys/version.h>\\
#if __DJGPP__ == 2 && __DJGPP_MINOR__ < 4\\
/* Define to 1 if you have the declaration of snprintf, and to 0 if you\\
   do not. */\\
# define HAVE_DECL_SNPRINTF 0\\
#endif
EOF


for file in ../gas/config.in ../gprof/gconfig.in ../ld/config.in ../opcodes/config.in; do
  if test ! -f ${file}.orig; then
    cp -vf ${file} ${file}.orig
    touch ${file}.orig -r ${file}
  fi
  sed -f ./script.sed ${file}.orig > ./file.out
  update ./file.out ${file}
  touch ${file} -r ${file}.orig
done
rm -vf ./file.out ./script.sed



#
#  4: Configure and build the libs and programs.
#

touch start_build.txt

echo
echo Configuring the progs and libs.
echo See build_log.txt file for output.


echo Using: > build_log.txt
gcc --version >> build_log.txt
as --version >> build_log.txt
echo >> build_log.txt
ld --version >> build_log.txt
echo >> build_log.txt
echo >> build_log.txt
echo >> build_log.txt

echo Configuring the progs and libs for DJGPP. >> build_log.txt
echo >>build_log.txt

../configure  --disable-dependency-tracking --disable-nls \
              --enable-install-bfd --enable-install-libiberty \
              --with-mpc='/dev/env/DJDIR' --with-mpfr='/dev/env/DJDIR' --with-gmp='/dev/env/DJDIR' \
              --enable-build-warnings=-Wimplicit,-Wcomment,-Wformat,-Wparentheses,-Wpointer-arith >> build_log.txt  2>&1

echo >> build_log.txt
echo ################################################################################ >> build_log.txt
echo >> build_log.txt


# Remove target alias from tooldir and scriptdir paths so
# that the linker scripts and binaries are installed in
# their DJGPP specific canonical places.
sed "/^.*tooldir = /s|/.*$||" ./Makefile > file.out
mv -vf ./file.out ./Makefile


echo
echo Building the progs and libs.
echo See build_log.txt file for output.
echo Building the progs and libs for DJGPP. >> build_log.txt
echo >> build_log.txt
make >> build_log.txt  2>&1

touch stop_build.txt
