#!/bin/bash

echo "Creating zip"
touch /tmp/mlisp.zip
rm /tmp/mlisp.zip
zip -r /tmp/mlisp.zip mlisp/*



echo "Copying zip"
ssh -p 5333 root@46.28.109.184 "rm /tmp/mlisp.zip"
ssh -p 5333 root@46.28.109.184 "rm -rf /tmp/mlisp/"
scp -P 5333 /tmp/mlisp.zip root@46.28.109.184:/tmp


echo "Unziping zip"
ssh -p 5333 vaclavt@46.28.109.184 "cd /tmp/; unzip mlisp.zip"


if [ "$#" -eq 1 ]; then
    echo "Copy only requested, exiting."
    exit 0;
fi

echo "Building ml"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; gcc -std=c99 -c -O2 -o linenoise.o clib/linenoise.c"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -c -O2  -I/usr/local/opt/openssl/include -Iclib --std=c++17  ml.cpp ml_io.cpp ml_date.cpp ml_string.cpp ml_util.cpp ml_profiler.cpp ml_usql.cpp  clib/json11.cpp clib/csvparser.cpp clib/sslclient.cpp clib/printf.cpp usql/exception.cpp usql/lexer.cpp usql/parser.cpp usql/usql.cpp usql/table.cpp usql/table.h usql/row.cpp usql/csvreader.cpp usql/usql.cpp"
ssh -p 5333 root@46.28.109.184 "cd /tmp/mlisp; c++ -o ml -O2 -L/usr/local/lib -L/usr/local/opt/openssl/lib -lm -lstdc++ -lcrypto -lssl *.o"


echo "Installing new version"
ssh -p 5333 root@46.28.109.184 "mkdir -p /usr/local/var/mlisp"
ssh -p 5333 root@46.28.109.184 "chown vaclavt: /usr/local/var/mlisp"

ssh -p 5333 root@46.28.109.184 "cp /tmp/mlisp/ml /usr/local/bin"
ssh -p 5333 root@46.28.109.184 "cp /tmp/mlisp/stdlib/*.lsp /usr/local/var/mlisp"

ssh -p 5333 root@46.28.109.184 "chmod +x /usr/local/bin/ml"
ssh -p 5333 root@46.28.109.184 "chown vaclavt: /usr/local/bin/ml"
ssh -p 5333 root@46.28.109.184 "chown vaclavt: /usr/local/var/mlisp"


echo "Completed"
