#!/bin/sh

####
#### File : Config
#### Package : eCrypt
####

root="Package"

## Checking if the current directory is the root directory of the package
curr=`pwd`
files=`ls $curr`
IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}/"
prevdir=""
for dir in $curr; do
  #echo $dir
  prevdir=$dir
done
IFS="$saveifs"

if test $prevdir != $root ; then
  echo "You must run config only from the main directory - $root"
  echo
  exit 1
fi

export root_of=$curr

clear

for arg
do 
  case $arg in
  	-silent) silent=yes ;;
	*) ;;
  esac
done

# Checking for the C++ compiler
if test  -z "$CPP"; then
  # Extract the first word of `g++', so it can be a program name with args.
  set dummy g++; word=$2
  set dummy c++; word1=$2
  echo "Checking for C++ Compiler......"
  
  # Setting field seperator
  IFS="${IFS=   }"; saveifs="$IFS"; IFS="${IFS}:"
  
  for dir in $PATH; do
    test -z "$dir" && dir=.
    if test -f $dir/$word; then
	  echo Found $word in $dir
	  CPP="g++"
      break
    fi
    if test -f $dir/$word1; then
	  echo Found $word1 in $dir
	  CPP="c++"
      break
    fi
  done
  
  # Resetting field seperator
  IFS="$saveifs"
fi

#test -z "$CC" && CC="gcc"
#test -n "$CC" -a "$silent" && echo "    setting CC to $CC"

if test -z "$CPP"; then
   echo "Could not find either c++ or g++. Make sure at least one of them is installed and that the path 
     variable is correct";
   exit 1
fi

echo 

## Checking for header files
echo "Checking for header files......"
## Header file 1 :-
echo "Looking for <stdio.h>"
if test -z "$CPPP"; then
  CPPP='$CPP -E'
  cat > conftest.c <<EOF
#include <stdio.h>
EOF
err=`eval "($CPPP conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
  :
else
  echo "Could not find header <stdio.h>. Make sure it's present in one of your directories your compiler looks for."
  exit 1
fi
rm -f conftest*
fi

## Header file 2 :-
echo "Looking for <iostream.h>"
if test -z "$CPPP"; then
  CPPP='$CPP -E'
  cat > conftest.c <<EOF
#include <iostream.h>
EOF
err=`eval "($CPPP conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
  :
else
  echo "Could not find header <iostream.h>. Make sure it's present in one of your directories your compiler looks for."
  exit 1
fi
rm -f conftest*
fi

## Header file 3 :-
echo "Looking for <math.h>"
if test -z "$CPPP"; then
  CPPP='$CPP -E'
  cat > conftest.c <<EOF
#include <math.h>
EOF
err=`eval "($CPPP conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
  :
else
  echo "Could not find header <math.h>. Make sure it's present in one of your directories your compiler looks for."
  exit 1
fi
rm -f conftest*
fi

## Header file 4 :-
echo "Looking for <stdlib.h>"
if test -z "$CPPP"; then
  CPPP='$CPP -E'
  cat > conftest.c <<EOF
#include <stdlib.h>
EOF
err=`eval "($CPPP conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
  :
else
  echo "Could not find header <stdlib.h>. Make sure it's present in one of your directories your compiler looks for."
  exit 1
fi
rm -f conftest*
fi

## Header file 5 :-
echo "Looking for <time.h>"
if test -z "$CPPP"; then
  CPPP='$CPP -E'
  cat > conftest.c <<EOF
#include <time.h>
EOF
err=`eval "($CPPP conftest.c >/dev/null) 2>&1"`
if test -z "$err"; then
  :
else
  echo "Could not find header <time.h>. Make sure it's present in one of your directories your compiler looks for."
  exit 1
fi
rm -f conftest*
fi

echo
echo "Your system contains all the requisite header files." Y

echo 
echo "Enter the platform you are using (eg. linux, solaris) -"
read plat
echo

## Generate the makefile
echo
echo "Creating Makefile......"
echo
cat > makefile <<EOF
####
#### File : makefile
#### Package : eCrypt
####

## This Makefile has been generated by the programme config
## during the process of installation

SRC=./src
BIN=./bin

INC=/usr/include/g++-3
CPP=$CPP -I\$(INC)
CPP_FLAGS=-O3 -g

PLAT=$plat

all : genOWF runOWF genPRF genKEY encrypt decrypt

genOWF : genOWF.o 
	  @\$(CPP) \$(CPP_FLAGS) -o \$(BIN)/genOWF \$(BIN)/genOWF.o
	  
genOWF.o : \$(SRC)/GenerateOWF.cpp \$(SRC)/Generate.hpp \$(SRC)/RandomGraph.hpp \$(SRC)/Randomizer.hpp \$(SRC)/PredicateGenerator.hpp \
		\$(SRC)/primes.hpp \$(SRC)/Geometric.hpp \$(SRC)/ArrayFunctions.hpp \$(SRC)/MathFunctions.hpp	  
	  @echo "Compiling GenerateOWF.cpp..."
	  @echo
	  @\$(CPP) \$(CPP_FLAGS) -c \$(SRC)/GenerateOWF.cpp -o \$(BIN)/genOWF.o
	  
runOWF : runOWF.o
	  @\$(CPP) \$(CPP_FLAGS) -o \$(BIN)/runOWF \$(BIN)/runOWF.o

runOWF.o : \$(SRC)/RunOWF.cpp \$(SRC)/Execute.hpp \$(SRC)/ArrayFunctions.hpp \$(SRC)/MathFunctions.hpp
	  @echo "Compiling RunOWF.cpp..."
	  @echo
	  @\$(CPP) \$(CPP_FLAGS) -c \$(SRC)/RunOWF.cpp -o \$(BIN)/runOWF.o

genPRF : genPRF.o 
	  @\$(CPP) \$(CPP_FLAGS) -o \$(BIN)/genPRF \$(BIN)/genPRF.o
	  
genPRF.o : \$(SRC)/GeneratePRF.cpp \$(SRC)/Generate.hpp \$(SRC)/RandomGraph.hpp \$(SRC)/Randomizer.hpp \$(SRC)/PredicateGenerator.hpp \
		\$(SRC)/primes.hpp \$(SRC)/Geometric.hpp \$(SRC)/ArrayFunctions.hpp \$(SRC)/MathFunctions.hpp	  
	  @echo "Compiling GeneratePRF.cpp..."
	  @echo
	  @\$(CPP) \$(CPP_FLAGS) -c \$(SRC)/GeneratePRF.cpp -o \$(BIN)/genPRF.o 
	  
genKEY : genKEY.o 
	  @\$(CPP) \$(CPP_FLAGS) -o \$(BIN)/genKEY \$(BIN)/genKEY.o
	  
genKEY.o : \$(SRC)/GenerateKey.cpp \$(SRC)/Randomizer.hpp   
	  @echo "Compiling GenerateKey.cpp..."
	  @echo
	  @\$(CPP) \$(CPP_FLAGS) -c \$(SRC)/GenerateKey.cpp -o \$(BIN)/genKEY.o 	
	  
encrypt : encrypt.o 
	  @\$(CPP) \$(CPP_FLAGS) -o \$(BIN)/encrypt \$(BIN)/encrypt.o
	  
encrypt.o : \$(SRC)/Encrypt.cpp \$(SRC)/ExecutePRF.hpp \$(SRC)/Execute.hpp \$(SRC)/BreakBlox.hpp \$(SRC)/Randomizer.hpp \$(SRC)/XORFunction.hpp 
		@echo "Compiling Encrypt.cpp..."
	  @echo
	  @\$(CPP) \$(CPP_FLAGS) -c \$(SRC)/Encrypt.cpp -o \$(BIN)/encrypt.o 

decrypt : decrypt.o 
	  @\$(CPP) \$(CPP_FLAGS) -o \$(BIN)/decrypt \$(BIN)/decrypt.o
	  
decrypt.o : \$(SRC)/Decrypt.cpp \$(SRC)/ExecutePRF.hpp \$(SRC)/BreakBlox.hpp \$(SRC)/Execute.hpp \$(SRC)/XORFunction.hpp 
		@echo "Compiling Decrypt.cpp..."
	  @echo
	  @\$(CPP) \$(CPP_FLAGS) -c \$(SRC)/Decrypt.cpp -o \$(BIN)/decrypt.o 
	

clean : 
	  @rm -f \$(BIN)/*
	  @echo "Cleaned up all binaries"
	  @echo

EOF

echo "You may now proceed to install the binaries."
echo 
echo "For doing this, run \"make\" on the command prompt."
echo

