# Copyright 2013 Samsung Information Systems America, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Author: Koushik Sen

lowercase(){
    echo "$1" | sed "y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/"
}

OS=`lowercase \`uname\``

echo $OS


npm install uglify-js@1 || { echo "npm install uglify-js@1 failed!"; exit 1; }
npm install node-ffi || { echo "npm install node-ffi failed!"; exit 1; }
npm install cover || { echo "npm install cover failed!"; exit 1; }
npm install websocket || { echo "npm install websocket failed!"; exit 1; }
npm install source-map || { echo "npm install source-map failed!"; exit 1; }
npm install esprima || { echo "npm install esprima failed!"; exit 1; }
npm install estraverse || { echo "npm install estraverse failed!"; exit 1; }
npm install escodegen || { echo "npm install escodegen failed!"; exit 1; }
npm install dryice || { echo "npm install dryice failed!"; exit 1; }
npm install execSync || {echo "npm install execSync failed!"; exit 1; }


rm -rf thirdparty
mkdir thirdparty
git clone --recursive git://github.com/Trenker/Browser-UglifyJS.git thirdparty/browser-uglifyjs || { echo "git clone --recursive git://github.com/Trenker/Browser-UglifyJS.git thirdparty/browser-uglifyjs failed!"; exit 1; }
cd thirdparty/browser-uglifyjs/lib
rm -rf UglifyJS
git clone git://github.com/mishoo/UglifyJS.git || { echo "git clone git://github.com/mishoo/UglifyJS.git failed!"; exit 1; }
cd UglifyJS
rm -rf .git
cd ..
cd ..
rm -rf .git
node build.js || { echo "node build.js for browserify of UglifyJs1 failed!"; exit 1; }
mv build/uglifyjs.1.2.5.js ../
cd ..

rm -rf esprima
git clone git://github.com/ariya/esprima.git || { echo "git clone git://github.com/ariya/esprima.git failed!"; exit 1; }
cd esprima
rm -rf .git
cd ..

rm -rf escodegen
git clone git://github.com/Constellation/escodegen.git || { echo "git clone git://github.com/Constellation/escodegen.git failed!"; exit 1; }
cd escodegen
rm -rf .git
cd ..

rm -rf estraverse
git clone git://github.com/Constellation/estraverse.git || { echo "git clone git://github.com/Constellation/estraverse.git failed!"; exit 1; }
cd estraverse
rm -rf .git
cd ..

rm -rf source-map
git clone git://github.com/mozilla/source-map.git || { echo "git clone git://github.com/mozilla/source-map.git failed!"; exit 1; }
cd source-map/
npm install amdefine
node Makefile.dryice.js
rm -rf .git
cd ..


echo "Downloading cvc3 ...."
if [ "${OS}" == "darwin" ]
then
    node ../src/js/commands/curl.js cvc3-2.4.1-macosx-optimized-static.tar.gz http://www.cs.nyu.edu/acsys/cvc3/releases/2.4.1/macosx/cvc3-2.4.1-macosx-optimized-static.tar.gz || { echo "node ../src/js/commands/curl.js cvc3-2.4.1-macosx-optimized-static.tar.gz http://www.cs.nyu.edu/acsys/cvc3/releases/2.4.1/macosx/cvc3-2.4.1-macosx-optimized-static.tar.gz failed!"; exit 1; }
    tar zvxf cvc3-2.4.1-macosx-optimized-static.tar.gz  || { echo "tar zvxf cvc3-2.4.1-macosx-optimized-static.tar.gz failed!"; exit 1; }
    mv cvc3-2.4.1-macosx-optimized-static cvc3
elif [ "${OS}" == "linux" ]
then
    node ../src/js/commands/curl.js cvc3-2.4.1-optimized-static.tar.gz http://www.cs.nyu.edu/acsys/cvc3/releases/2.4.1/linux32/cvc3-2.4.1-optimized-static.tar.gz || { echo "node ../src/js/commands/curl.js cvc3-2.4.1-optimized-static.tar.gz http://www.cs.nyu.edu/acsys/cvc3/releases/2.4.1/linux64/cvc3-2.4.1-optimized-static.tar.gz failed!"; exit 1; }
    tar zvxf cvc3-2.4.1-optimized-static.tar.gz  || { echo "tar zvxf cvc3-2.4.1-optimized-static.tar.gz failed!"; exit 1; }
    mv cvc3-2.4.1-optimized-static cvc3
else
    echo "You need Mac OS X or Linux to run Jalangi"
    exit 1;
fi

./cvc3/bin/cvc3 < ../scripts/formula.cvc3 || { echo "cvc3 installation failed! Make sure that you have libgmp installed in your machine.  This is a common cause for the failure."; exit 1; }

mkdir javalib
cd javalib
echo "Downloading http://www.brics.dk/automaton/automaton.jar ...."
node ../../src/js/commands/curl.js automaton.jar http://www.brics.dk/automaton/automaton.jar || { echo "node ../../src/js/commands/curl.js automaton.jar http://www.brics.dk/automaton/automaton.jar failed!"; exit 1; }
cd ../..

#ant -f jalangijava.xml || { echo "ant -f jalangijava.xml failed!"; exit 1; }
rm -rf jout
mkdir jout
mkdir jout/production
mkdir jout/production/jalangijava

javac -cp ./thirdparty/javalib/automaton.jar -d jout/production/jalangijava src/java/RegexpEncoder.java || { echo "javac -cp ./thirdparty/javalib/automaton.jar src/java/RegexpEncoder.java."; exit 1; }
java -cp ./jout/production/jalangijava/:./thirdparty/javalib/automaton.jar RegexpEncoder length ".*www\..*" y38 false || { echo "Installation of Java part of Jalangi failed!  Make sure that you have Sun's JDK 1.6 or higher."; exit 1; }

node src/js/instrument/esnstrument.js src/js/analyses/concolic/SymbolicFunctions.js
node src/js/instrument/esnstrument.js src/js/analyses/puresymbolic/SymbolicFunctions2.js
node src/js/instrument/esnstrument.js src/js/analyses/puresymbolic/SymbolicFunctions3.js

echo "****************************"
echo "Installation successful."
echo "Run ./scripts/testsym to make sure that all tests pass."
echo "****************************"





