#!/bin/sh
if [ "`whoami`" != "root" ] ; then
    echo "These need to be run as root..."
    exit 0
fi
echo "Cleaning up previous installations"
make clean
echo "Setting up /usr/local/bin/i77 to point to `pwd`/i77"
mkdir -p /usr/local/bin
chmod o+rx /usr/local/bin
if [ -f /usr/local/bin/i77 ] || [ -L /usr/local/bin/i77 ]; then
    rm -f /usr/local/bin/i77
fi
ln -s `pwd`/i77 /usr/local/bin/i77
echo "Setting up /usr/local/bin/i2c to point to `pwd`/i2c"
if [ -f /usr/local/bin/i2c ] || [ -L /usr/local/bin/i2c ] ; then
    rm -f `pwd`/i2c /usr/local/bin/i2c
fi
ln -s `pwd`/i2c /usr/local/bin/i2c
echo "Creating /usr/local/include/i2c"
mkdir -p /usr/local/include/i2c
chmod o+rx /usr/local/include/i2c
echo "Installing a copy of perms.h and perms.inc in /usr/local/include/i2c/"
if [ -f /usr/local/include/i2c/perms.h ] || [ -L /usr/local/include/i2c/perms.h ] ; then
    rm -f /usr/local/include/i2c/perms.h
fi
##cp perms.h /usr/local/include/i2c/
ln -s `pwd`/perms.h /usr/local/include/i2c/perms.h
if [ -f /usr/local/include/i2c/perms.inc ] || [ -L /usr/local/include/i2c/perms.inc ] ; then
    rm -f /usr/local/include/i2c/perms.inc
fi
##cp perms.inc /usr/local/include/i2c/
ln -s `pwd`/perms.inc /usr/local/include/i2c/perms.inc
echo "Creating /usr/local/lib/i2c"
mkdir -p /usr/local/lib/i2c/m32
chmod o+rx /usr/local/lib/i2c /usr/local/lib/i2c/m32
echo "Building compiler."
make
echo "Building perms"
i77 -m32 -o perms-m32.o -c perms.c || i77 -o perms-m32.o -c perms.c
i77      -o perms.o     -c perms.c
echo "Installing perms in /usr/local/lib/i2c/"
if [ -f /usr/local/lib/i2c/m32/perms.o ] || [ -L /usr/local/lib/i2c/m32/perms.o ] ; then
    rm -f /usr/local/lib/i2c/m32/perms.o
fi
ln  -s `pwd`/perms-m32.o /usr/local/lib/i2c/m32/perms.o
if [ -f /usr/local/lib/i2c/perms.o ] || [ -L /usr/local/lib/i2c/perms.o ] ; then
    rm -f /usr/local/lib/i2c/perms.o
fi
ln  -s `pwd`/perms.o /usr/local/lib/i2c/perms.o
echo "i77 installed.  Quick test with ./REGRESSION-RUN.sh ; ./REGRESSION-ACCEPT-RUN.sh ; ./REGRESSION-RUN.sh"
echo "Full test with ./REGRESSION.sh ; ./REGRESSION-ACCEPT.sh ; time ./REGRESSION.sh"
exit 0
