#!/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. It works exactly the same on
# Ubuntu 16, so these scripts are identical except for some
# comments and the checksums (so the builds are different)

# 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:

# 67d528f525a746be8eb2b5b3bbf9b9eda4be67f674d2ced6860a1d96bc508184  coinyecoind
# 4bfcfddcfc9908b23fefc7e549a59a509e83bffcc6838a4f0f90b96b3027e507  coinyecoind_unstripped
# 80ec0664461e676e9eb1255c6d0c72884ee699acfdb9c986139a18ac04414bc2  coinyecoin-qt
# 90f53533452ca3359f2ea354d7cdaabe9d5ff07cbcde80686479db392e1234ca  coinyecoin-qt_unstripped

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

# Might neet to install add-apt-repository command:
apt install software-properties-common -y

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"
