#! /bin/bash
#
# (C) 1997 under GPL, Hans Lermen <lermen@fgan.de>
#
# NOTE: you normally do not need this, its just a tool for the developers:
# Compile and install a binary distribution of DOSEMU under
#
#   ~/.dosemu/tmp/dosemu-<version>/*
#
# To pack this into a tar file do something like
#
#   tar -cf- -C ~/.dosemu/tmp/dosemu-<version> . | gzip -9 > dosemu-<version>-bin.tgz
#

function usage {
  echo -e "USAGE:
  mkbindist [-m] [-s] [-t targdir] [-d docdir] [-f csfile] [-- configopts ... ]

  -m          do a 'make pristine; make' before generation
  -c          do just a clean compile, but no generation
  -s          produce static binaries
  -f csfile   take csfile as compiletime-settings
  -t targdir  use 'targdir' instead of ~/.dosemu/tmp/dosemu-<version>/*
  -d docdir   include documentation in 'docdir'
  configopts  all valid options for ./configure, don't forget to
              put an empty '--' in front of the first option

  NOTE: this is intended for 'releasers' use mostly,
        you normally don't need it!
"
  exit 1
}


if [ "`basename $PWD`" = "tools" ]; then
  cd ../../
fi

TAKESETUP="1"

TMPDIR=~/.dosemu/tmp
mkdir -p $TMPDIR
VER=`basename $PWD`
DIR="$TMPDIR/${VER}"
TMP="$TMPDIR/settings.$$"
SETTINGS="./compiletime-settings"

SHIFT=""
while getopts "mcst:d:f:" OPT; do
  case $OPT in
    m) DOMAKE="1"; SHIFT="$SHIFT x";;
    c) DOMAKE="1"; COMPILE="1"; SHIFT="$SHIFT x";;
    s) STATIC="1"; SHIFT="$SHIFT x";;
    t) DIR=$OPTARG; SHIFT="$SHIFT x x";;
    d) DOCDIR_=$OPTARG; SHIFT="$SHIFT x x";;
    f) SETTINGS=$OPTARG; SHIFT="$SHIFT x x";;
    ?) usage ;;
  esac
done

for i in $SHIFT; do
  shift
done

SUFFIX=""
while [ "$1" != "" ]; do
  if [ "$1" != "--" ]; then
    SUFFIX="$SUFFIX $1"
  fi
  shift
done

if [ "$COMPILE" != "1" ]; then
  MANDIR=${DIR}/usr/man/man1

  if [ "$DOCDIR_" = "" ]; then
    DOCDIR_="usr/doc/dosemu"
  fi
  DOCDIR="$DOCDIR_"
  if [ "${DOCDIR#/}" = "$DOCDIR" ]; then
    DOCDIR="${DIR}/${DOCDIR}"
  fi

  install -d  ${DIR}/etc
  install -d  ${DIR}/usr/bin
  install -d  ${DIR}/var/lib/dosemu
  install -d  ${DIR}/usr/X11R6/lib/X11/fonts/misc
fi

if [ "$DOMAKE" = "1" ]; then
  if [ "$STATIC" = "1" ]; then
    sed -e "s/linkstatic off/linkstatic on/" -e "s/slangforce off/slangforce on/" $SETTINGS > ${TMP}
  else
    sed -e "s/linkstatic on/linkstatic off/" -e "s/slangforce off/slangforce on/" $SETTINGS > ${TMP}
  fi
  if [ -f Makefile.conf ]; then
    make pristine
  fi
  ./default-configure ${TMP} ${SUFFIX}
  make 'WAIT='
  if [ "$TAKESETUP" = "1" ]; then
     (cd setup/parser; make 'STATIC=-static')
     strip setup/parser/parser
  fi
  strip ./bin/dos
  strip ./bin/dosdebug
  strip ./src/tools/periph/dexeconfig
  strip ./src/tools/periph/mkfatimage16
  strip ./src/tools/periph/scsicheck
  strip ./src/tools/periph/dosctrl
  rm -f ${TMP}
fi

if [ "$COMPILE" = "1" ]; then
  exit 0
fi

if [ ! -x ./bin/dos ]; then
  echo "there was nothing compiled to install"
  echo " use -m or -c before"
  echo
  usage
fi

if [ "$DIR" = "/" ]; then
  make install
else
  make "INSTROOT=$DIR" install
  rm -rf ${DIR}/etc
fi
install -m 0755 src/tools/periph/mkfatimage16 ${DIR}/usr/bin
install -m 0755 src/tools/periph/dexeconfig ${DIR}/usr/bin
install -m 0755 src/tools/periph/scsicheck ${DIR}/usr/bin
install -m 0755 src/tools/periph/dosctrl ${DIR}/usr/bin

if [ -d ${DIR}/var/lib/dosemu/dexe ]; then cp -fp ./dexe/* ${DIR}/var/lib/dosemu/dexe
else cp -ap ./dexe  ${DIR}/var/lib/dosemu; fi
sed -e "s/ISBINRELEASE/1/" ./dexe/mkdexe >${DIR}/var/lib/dosemu/dexe/mkdexe
chmod 755 ${DIR}/var/lib/dosemu/dexe/mkdexe
sed -e "s/ISBINRELEASE/1/" ./setup-hdimage >${DIR}/var/lib/dosemu/setup-hdimage
chmod 755 ${DIR}/var/lib/dosemu/setup-hdimage
sed -e "s/ISBINRELEASE/1/" ./setup-bootdir >${DIR}/var/lib/dosemu/setup-bootdir
chmod 755 ${DIR}/var/lib/dosemu/setup-bootdir


if [ -d ${DIR}/var/lib/dosemu/dosC ]; then cp -fp ./contrib/dosC/dist/* ${DIR}/var/lib/dosemu/dosC
else cp -ap ./contrib/dosC/dist ${DIR}/var/lib/dosemu/dosC; fi
cp -fp ./contrib/dosC/readme.*  ${DIR}/var/lib/dosemu/dosC

if [ ! -d ${DIR}/var/lib/dosemu/commands ]; then install -d ${DIR}/var/lib/dosemu/commands;fi
cp -ap ./commands/* ${DIR}/var/lib/dosemu/commands

install -d ${DIR}/var/lib/dosemu/etc
( cd ./etc; cp -fap config.* dosemu.* keymap ${DIR}/var/lib/dosemu/etc )

pushd ${DIR}/var/lib/dosemu >/dev/null
cat > doinstall.sh <<'EOF'
#! /bin/bash
( cd /usr/X11R6/lib/X11/fonts/misc; mkfontdir . )
( cd /var/lib/dosemu; mkfatimage16 -b dosC/boot.bin -p -f hdimage.test \
	-l DOSEMU dosC/ipl.sys dosC/kernel.exe dosC/command.com \
	commands/* )
if [ -f /etc/dosemu.conf ]; then
  if grep '$_hd.image' /etc/dosemu.conf>/dev/null; then
    echo "you seem to have a new style dosemu.conf, but better you check"
    echo "against the one in /var/lib/dosemu/etc"
  else
    echo "you seem not have an old style dosemu.conf,"
    echo "please read /usr/doc/dosemu/QuickStart on how to proceed."
  fi
else
  cp -p /var/lib/dosemu/etc/dosemu.conf /etc
  if [ ! -f /etc/dosemu.users ]; then
    cp -p /var/lib/dosemu/etc/dosemu.users.easy /etc
  fi
fi
EOF
popd >/dev/null

if [ "$DOCDIR" = "" ]; then
  DOCDIR=/usr/doc/dosemu
fi
install -d $DOCDIR
cp -fp ./doc/* $DOCDIR
rm -f ${DOCDIR}/Makefile.*
cp -fp ChangeLog* ${DOCDIR}

_DOCDIR=""
for i in `echo -n $DOCDIR_ |tr '/' ' '`; do
  _DOCDIR="${_DOCDIR}\/${i}"
done


sed -e "s/DOCDIR/${_DOCDIR}/g" ./src/tools/QuickStart.bindist >${DOCDIR}/QuickStart
cp -fp COPYING ${DOCDIR}

if [ "$TAKESETUP" = "1" ]; then
  install -d  ${DIR}/var/lib/dosemu/setup/parser
  (cd setup; cp -fp parse-* runtime* select* write* *.tk ${DIR}/var/lib/dosemu/setup)
  sed -e "s|\.\./doc|${_DOCDIR}|" setup/browse.tk >${DIR}/var/lib/dosemu/setup/browse.tk
  install  setup/parser/parser ${DIR}/var/lib/dosemu/setup/parser
  cat > ${DIR}/var/lib/dosemu/setup-dosemu <<'EOF'
#!/bin/sh
#
# Front end to the runtime & compile time scripts.
#

if hash wish >/dev/null 2>&1; then
  if [ "$DISPLAY" != "" ]; then
    echo "
*** NOTE: setup-dosemu currently is only able to handle setup
***       via TclTk. Fortunately you have that installed ;-)
"
    mkdir -p ~/.dosemu/tmp
    cd setup
    exec ./runtime_setup.tk
  fi
fi

echo "
*** NOTE: setup-dosemu needs TclTk, you don't have it.
"
exit 1
EOF

  chmod 755 ${DIR}/var/lib/dosemu/setup-dosemu
fi
