#!/bin/bash

#
#  fenris - program execution path analysis tool
#  ---------------------------------------------
#
#  Copyright (C) 2001, 2002 by Bindview Corporation
#  Portions Copyright (C) 2001, 2002 by their respective contributors
#  Developed and maintained by Michal Zalewski <lcamtuf@coredump.cx>
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#  05/16/2002: Modified by Marcin Gozdalik to use libfnprints and such.


test "$BASH" = "" && echo "Bash shell is required to execute this script."
test "$BASH" = "" && exit 1

PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH

rm -f core core.* *~ .testerr *.o a.out .tmp 
rm -f nasm/insnsd.c nasm/insnsi.h nasm/insnsn.c

test "$1" = "clean" && exit 0


CFLAGS="-O9 -fomit-frame-pointer -funroll-loops -fexpensive-optimizations \
        -ffast-math -Wall"

CFLAGS2="-O9 -fomit-frame-pointer -funroll-loops -fexpensive-optimizations \
        -ffast-math "
	
if [ "$1" = "minimal" ]; then
  CFLAGS="$CFLAGS -DMINIMAL=1"
fi

LDFLAGS="-ldl -lbfd -liberty -rdynamic"

if [ "$1" = "debug" ]; then
  CFLAGS="-Wall -ggdb -DDEBUG=1"
  CFLAGS2="-ggdb -DDEBUG=1"
  NOSTRIP=1
fi


if [ "$1" = "heavy" ]; then
  CFLAGS="-Wall -ggdb -DDEBUG=1 -DHEAVY_DEBUG=1"
  CFLAGS2="-ggdb -DDEBUG=1  -DHEAVY_DEBUG=1"
  NOSTRIP=1
fi


if [ "$1" = "prof" ]; then
  CFLAGS="-Wall -DDEBUG=1 -fno-inline -ggdb -pg -DPROFILE=1"
  CFLAGS2="-DDEBUG=1 -fno-inline -ggdb -pg -DPROFILE=1"
  NOSTRIP=1
fi

cd ./nasm

echo -n "[*] Generating insnsd.c: "
perl ./insns.pl -d ./insns.dat 2>/dev/null
echo "done"

echo -n "[*] Generating insnsi.h: "
perl ./insns.pl -i ./insns.dat 2>/dev/null
echo "done"

echo -n "[*] Generating insnsn.c: "
perl ./insns.pl -n ./insns.dat 2>/dev/null
echo "done"

cd ..

CFLAGS3="-g -O2 -Wall -ansi -pedantic -I./nasm"

###

echo -n "[*] Compiling disasm: "

gcc -c $CFLAGS3 $CFLAGS2 nasm/disasm.c 2>.testerr 

if [ ! -s disasm.o ]; then
  echo "failed!"
  echo "[-] Sorry it didn't work out. Please consult the documentation."
  echo "[-] Note: if this is because of no memory left, try './build minimal',"
  echo "[-] or, better, temporarily enable some swap memory."
  echo
  echo "[-] Error messages:"
  cat .testerr
  echo
  rm -f .testerr
  exit 1
fi

echo "done"

if [ -s .testerr ]; then
  echo "[!] Warning messages: "
  cat .testerr
fi

rm -f .testerr

###

echo -n "[*] Compiling sync: "

gcc -c $CFLAGS3 $CFLAGS2 nasm/sync.c 2>.testerr 

if [ ! -s sync.o ]; then
  echo "failed!"
  echo "[-] Sorry it didn't work out. Please consult the documentation."
  echo "[-] Note: if this is because of no memory left, try './build minimal',"
  echo "[-] or, better, temporarily enable some swap memory."
  echo
  echo "[-] Error messages:"
  cat .testerr
  echo
  rm -f .testerr
  exit 1
fi

echo "done"

if [ -s .testerr ]; then
  echo "[!] Warning messages: "
  cat .testerr
fi

rm -f .testerr

###

echo -n "[*] Compiling nasmlib: "

gcc -c $CFLAGS3 $CFLAGS2 nasm/nasmlib.c 2>.testerr 

if [ ! -s nasmlib.o ]; then
  echo "failed!"
  echo "[-] Sorry it didn't work out. Please consult the documentation."
  echo "[-] Note: if this is because of no memory left, try './build minimal',"
  echo "[-] or, better, temporarily enable some swap memory."
  echo
  echo "[-] Error messages:"
  cat .testerr
  echo
  rm -f .testerr
  exit 1
fi

echo "done"

if [ -s .testerr ]; then
  echo "[!] Warning messages: "
  cat .testerr
fi

rm -f .testerr

###

echo -n "[*] Compiling insnsd: "

gcc -c $CFLAGS3 $CFLAGS2 nasm/insnsd.c 2>.testerr 

if [ ! -s insnsd.o ]; then
  echo "failed!"
  echo "[-] Sorry it didn't work out. Please consult the documentation."
  echo "[-] Note: if this is because of no memory left, try './build minimal',"
  echo "[-] or, better, temporarily enable some swap memory."
  echo
  echo "[-] Error messages:"
  cat .testerr
  echo
  rm -f .testerr
  exit 1
fi

echo "done"

if [ -s .testerr ]; then
  echo "[!] Warning messages: "
  cat .testerr
fi

rm -f .testerr

###



exit 0
