#!/bin/bash

if [ -f /usr/share/lib/geth-utils/geth-utils.include.sh ]; then source /usr/share/lib/geth-utils/geth-utils.include.sh; fi
if [ -f /etc/geth-utils/geth.conf ]; then source /etc/geth-utils/geth.conf; fi

# DEFAULTMODE=auto
DEFAULTMODE=$GETHINSTALLMODE
source /usr/share/scripts/singlestep

_usage () {
   echo "Usage: `basename $0`"
   echo "Usage: `basename $0` [ -h | --help ]"
   echo "Usage: `basename $0` [ -v | --version ]"
}

_version () {
   echo "them-geth-utils Version $GVERSION"
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
   _usage
   exit 17
fi

if [ "$1" = "-v" -o "$1" == "--version" ]; then
   _version
   exit 17
fi

echo "Doing `basename $0`"

if [ `whoami` != root ]; then
  echo "must be called as root."
  exit 17
fi

_prepare() {
    singleline apt-get update
    # singleline apt-get -y -t buster install build-essential
    singleline apt-get -y -t sid install build-essential cmake g++ gcc libboost-all-dev libcvc4-dev
    # singleline apt-get -y -t buster install g++
    # singleline apt-get -y -t buster install gcc
    ## singleline apt-get -y -t buster install git
    # singleline apt-get -y -t buster install libboost-all-dev
    ## singleline apt-get -y -t buster install libboost-tools-dev
    ## singleline apt-get -y -t buster install libz3-dev
    # singleline apt-get -y -t buster install libcvc4-dev
    # libevent-pthreads-2.1-6
    # singleline ./scripts/install_deps.sh
}

_compile () {
    export SVERSION=""
    _set_git_version $SOLVERSION
    singleline git checkout $SVERSION
    singleline ./scripts/build.sh Release -DUSE_CVC4=OFF -DUSE_Z3=OFF
    singleline git checkout develop
}


echo "=========== solC installation:"
if [ -d /usr/src/solidity ]; then
    echo "solC installation seems to exist in /usr/src/solidity; just updating..."
    _prepare
    doline cd /usr/src/solidity
    singleline git fetch
    singleline git pull
    _compile
else
    echo "solC installation seems not to exist in /usr/src/solidity; doing fresh install"
    _prepare
    doline cd /usr/src
    singleline git clone --recursive https://github.com/ethereum/solidity.git
    doline cd /usr/src/solidity
    _compile
    # creates /usr/local/bin/solc itself
fi


#echo "=========== porosidity installation:"
#if [ -d /usr/src/porosity ]; then
#    echo "porosity installation seems to exist in /usr/src/porosity; use gethUpdate instead"
#else
#    doline cd /usr/src
#    singleline git clone --recursive https://github.com/comaeio/porosity.git
#    doline cd /usr/src/porosity/porosity/porosity
#    singleline make
#    
#    if [ \! -L /usr/local/bin/porosity -a -f /usr/src/porosity/porosity/porosity/porosity ]; then
#       singleline ln -s /usr/src/porosity/porosity/porosity/porosity /usr/local/bin/porosity
#    fi
#fi

echo "=========== ... finished"

# echo "You may call now: gstart ..."
