#!/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 and/or trace my steps in 
# compiling Coinye. 

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

# 3f3be57d1922322166c98139dd9509089f6d7f6d0bc1aa835f9a09e59cafb27a  coinyecoind
# d13e1da041c47fcb83ff14571fed60bafca387244ee81a70720ee79236de3d05  coinyecoind_unstripped
# dea4bae4e91f12e19a41125e44368bd7ddff7ff813d762b04fcc0438897cd4ca  coinyecoin-qt
# 3143dcb947be01beb028478fdece25a3ae12a2bc12ea01f981dbf4c84dae77c5  coinyecoin-qt_unstripped

# tl;dr:
# - get a clean Ubuntu 14.04 (i used Virtual Box, don't update/upgrade!)
# wget http://www.coinye.net/downloads/ubuntu_14_64/build_coinye_on_ubuntu14.sh && chmod +x build_coinye_on_ubuntu14.sh && ./build_coinye_on_ubuntu14.sh |& tee coinye_build_log.txt
# - 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


# I did not get past this in Debian 10 though...! )-:
echo -e "\n\n**************** update repositories ********************\n"

add-apt-repository ppa:bitcoin/bitcoin -y


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 \
               #libdb++-dev \



#               libqrencode-dev \
# QR Codes don't seem to be working anyway!

echo -e "\n\n**************** Get Coinye Source ********************\n"
sudo -u $BUILDER git clone https://github.com/realcoinyecoin/coinyecoin


echo -e "\n\n**************** Build headless daemon:  ********************\n"
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/




echo -e "\n\n**************** Build QT Wallet:  ********************\n"
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



echo -e "\n\n**************** Generate extra files  ********************\n"
# Make a config file for test/run, disable this if you only want to build! ###

DATADIR=/home/$BUILDER/build_coinye/sample_datadir/
CONF=$DATADIR/coinyecoin.conf

echo "Creating dir $DATADIR for $BUILDER"
sudo -u $BUILDER mkdir $DATADIR
sudo -u $BUILDER chmod 700 $DATADIR

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

## Make a test/run script:
STARTFILE=test_coinye-qt.sh
sudo -u $BUILDER touch $STARTFILE
sudo -u $BUILDER chmod +x $STARTFILE
sudo -u $BUILDER echo "#!/bin/bash" > $STARTFILE
sudo -u $BUILDER echo "/home/$BUILDER/build_coinye/coinyecoin-qt --datadir=$DATADIR" >> $STARTFILE

echo "DONE. "
echo "output files in: /home/$BUILDER/build_coinye/"
echo "You can test your coinye build by running build_coine/$STARTFILE"