#!/bin/bash

# I'm trying to compile Coinye on Ubuntu 20.
# This script is a work of progress. 

# NOTE: THIS IS NOT WORKING YET!!

# It needs root access for a few tasks, so the whole
# script must be ran as root. But because we don't 
# want everything to be done by Root, and we want to 
# keep our system clean, we'll ask which username
# be used for the more mundane stuff. This users name
# will be kept in $COINYEUSER. If /home/$COINYEUSER does 
# not exist, we'll assume it's a new user and create it
if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root" 

   # -k force password, despite timestamp
   sudo -k /bin/bash $0 $USER
   exit
fi

set -e # Quit on any errors

COINYEUSER=$1
echo install coinye for $COINYEUSER, using sudo/root only where needed.



# For qt4 (old version)
add-apt-repository ppa:gezakovacs/ppa -y

# On Ubuntu 20:
#Build-essentials because we're going to build berkeley db 4.8 for backwards compatibility


apt update
apt install -y \
    build-essential \
    g++ \
    python-dev \
    autotools-dev \
    libicu-dev \
    libbz2-dev \
    qt4-default \
    libminiupnpc17 \



sudo -u $COINYEUSER mkdir /home/$COINYEUSER/coinye
cd /home/$COINYEUSER/coinye

sudo -u $COINYEUSER wget http://www.coinye.net/downloads/ubuntu_14_64/coinyecoin-qt
sudo -u $COINYEUSER chmod +x coinyecoin-qt


sudo -u $COINYEUSER wget 'http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz'
sudo -u $COINYEUSER tar -xzvf db-4.8.30.NC.tar.gz
# Blatant steal from https://www.fsanmartin.co/compiling-berkeley-db-4-8-30-in-ubuntu-19/ :
sudo -u $COINYEUSER sed -i 's/__atomic_compare_exchange/__atomic_compare_exchange_db/g' db-4.8.30.NC/dbinc/atomic.h
cd db-4.8.30.NC/build_unix/
sudo -u $COINYEUSER ../dist/configure --enable-cxx
sudo -u $COINYEUSER make
make install

# Tell your system where to find db4.8
export BDB_INCLUDE_PATH="/usr/local/BerkeleyDB.4.8/include"
export BDB_LIB_PATH="/usr/local/BerkeleyDB.4.8/lib"
# ln -s /usr/local/BerkeleyDB.4.8/lib/libdb-4.8.so /usr/lib/libdb-4.8.so
ln -s /usr/local/BerkeleyDB.4.8/lib/libdb_cxx-4.8.so /usr/lib/libdb_cxx-4.8.so



# Get Boost 1.54:
cd /home/$COINYEUSER/coinye
sudo -u $COINYEUSER wget http://downloads.sourceforge.net/project/boost/boost/1.54.0/boost_1_54_0.tar.gz
sudo -u $COINYEUSER tar -zxvf boost_1_54_0.tar.gz
cd boost_1_54_0
echo "****** Bootstrap that bitch! ******"

set +e
sudo -u $COINYEUSER ./bootstrap.sh  # this will generate ./b2

echo "****** install using b2 ******"
./b2 --with=all install

set -e



cd /usr/lib/x86_64-linux-gnu
# ln -s libssl.so.
echo "****** Link to libminiupnpc ******"
ln -s libminiupnpc.so.17 libminiupnpc.so.8

echo "****** libCrypto ******"
ln -s libcrypto.so.1.1 libcrypto.so.1.0.0

echo "****** link ssl (this doesn't work) ******"
ln -s libssl.so.1.1 libssl.so.1.0.0

