#!/bin/bash

# This script is made to build the Coinye daemon and wallet, 
# including handling all dependences etcetera.
# I ran it in a fresh install in Ubuntu 14.04 in a Virtual 
# Machine, and tested it.
# In the end, it will also create a data dir in case you 
# want to run/test it. Comment that section out if you 
# Only want to build. Obviouly ymmv and this is here mainly
# to help people get started. 

# Without the QR Lib (see below), these are the Shasums of 
# the resulting files:

# d97b33a8a2e1dacb89955409477a1224d20adfe0499abdb5b874006065cdac73  coinyecoind
# aec8283ec535abebc2a77e1ad61375dc370b8bf35a3fe8eb3e62d6adf183b95c  coinyecoind_unstripped
# dea4bae4e91f12e19a41125e44368bd7ddff7ff813d762b04fcc0438897cd4ca  coinyecoin-qt
# 3143dcb947be01beb028478fdece25a3ae12a2bc12ea01f981dbf4c84dae77c5  coinyecoin-qt_unstripped

# tl;dr:
# - get a clean Ubuntu 14.04 (i used Virtual Box)
# - wget http://www.coinye.net/build_coinye_on_ubuntu14.sh
# - chmod +x build_coinye_on_ubuntu14.sh
# - sudo ./build_coinye_on_ubuntu14.sh
# - drink coffyee / profit



# lets get started:

# 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 $BUILDER. If /home/$BUILDER 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
   exit
fi

set -e # Exit on all kinds of errors!

# # Now you're root
echo We are not going to do everything as root, who is the user who is going to build coinye?
read BUILDER

if [ -d "/home/$BUILDER" ]; then
    echo using existing home dir: "/home/$BUILDER"
else 
    useradd -m $BUILDER
    echo Made new user $BUILDER, don\'t forget to sudo passwd $BUILDER to set his password later on!
fi

cd /home/$BUILDER
sudo -u $BUILDER mkdir build_coinye
cd build_coinye

# libdb4.8-dev and libdb4.8++-dev are found in ppa:bitcoin/bitcoin
add-apt-repository ppa:bitcoin/bitcoin -y
# I did not get past this in Debian 10 though...! )-:

apt update
# Don't... apt upgrade!!!

apt install -y vim \
               build-essential \
               git \
               libssl-dev \
               libboost-all-dev \
               qt4-qmake \
               libqt4-dev \
               libboost-dev \
               libboost-system-dev \
               libboost-filesystem-dev \
               libboost-program-options-dev \
               libboost-thread-dev \
               libminiupnpc-dev \
               libdb4.8-dev \
               libdb4.8++-dev \
#               libqrencode-dev \
# QR Codes don't seem to be working anyway!

sudo -u $BUILDER git clone https://github.com/realcoinyecoin/coinyecoin



# Berkeley DB: 
sudo -u $BUILDER wget 'http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz'
sudo -u $BUILDER tar -xzvf db-4.8.30.NC.tar.gz

cd db-4.8.30.NC/build_unix/
sudo -u $BUILDER ../dist/configure --enable-cxx
sudo -u $BUILDER make

make install 



# Build headless daemon: 
cd /home/$BUILDER/build_coinye/coinyecoin/src
sudo -u $BUILDER make -f makefile.unix USE_UPNP=1

sudo -u $BUILDER mv coinyecoind /home/$BUILDER/build_coinye/




# Next the QT interface:
cd /home/$BUILDER/build_coinye/coinyecoin
# QR Code option shows up in context menu, but it doesn't seem to work.
# sudo -u $BUILDER qmake "USES_QRCODE=1" "USE_DBUS=1" 
# So for now: 
sudo -u $BUILDER qmake "USES_QRCODE=0" "USE_DBUS=1" 

sudo -u $BUILDER make

sudo -u $BUILDER mv coinyecoin-qt /home/$BUILDER/build_coinye/

cd /home/$BUILDER/build_coinye
sudo -u $BUILDER cp coinyecoind coinyecoind_unstripped
sudo -u $BUILDER strip coinyecoind

sudo -u $BUILDER cp coinyecoin-qt coinyecoin-qt_unstripped
sudo -u $BUILDER strip coinyecoin-qt

# Check all this, generate some checksums
cd /home/$BUILDER/build_coinye
sudo -u $BUILDER touch checksums.sha256 # So it is owned by $BUILDER
sudo -u $BUILDER sha256sum coinye* | tee -a checksums.sha256



### Make a config file for first run, disable this if you only want to build! ###
if [ -d "/home/$BUILDER/.coinyecoin" ]; then
    echo $BUILDER already has a .coinyecoin dir
else
    CONF=/home/$BUILDER/.coinyecoin/coinyecoin.conf
    echo "Creating /home/$BUILDER/.coinyecoin dir for $BUILDER"

    sudo -u $BUILDER mkdir /home/$BUILDER/.coinyecoin
    sudo -u $BUILDER chmod 700 /home/$BUILDER/.coinyecoin
    sudo -u $BUILDER touch $CONF # To make it owned by $BUILDER

    sudo -u $BUILDER echo rpcuser=coinyecoinrpc >> $CONF
    RND=$(tr -dc A-Za-z0-9 </dev/urandom | head -c 6 ; echo '')
    sudo -u $BUILDER echo rpcpassword=ChangeThisIfYouWantToUseRpc_$RND >> $CONF
    sudo -u $BUILDER echo "upnp=1" >> $CONF

    sudo -u $BUILDER echo "" >> $CONF

    sudo -u $BUILDER wget http://www.coinye.net/schmapi/nodes.php -O nodes.txt
    sudo -u $BUILDER cat nodes.txt | grep addnode >> $CONF
fi