#!/bin/bash -e

# Copyright (C) 2018 Gunter Miegel coinboot.io
#
# This file is part of Coinboot.
# This software may be modified and distributed under the terms
# of the MIT license.  See the LICENSE file for details.
#
# Please notice even while this script is licensed with the
# MIT license the software packaged by this script may be licensed by
# an other license with different terms and conditions.
# You have to agree to the license of the packaged software to use it.


# Download and install Claymore's Miner

CLAYMORES_RELEASE=v11.9
CLAYMORES_NAME="Claymore""'""s Dual Ethereum+Decred_Siacoin_Lbry_Pascal_Blake2s_Keccak AMD+NVIDIA GPU Miner $CLAYMORES_RELEASE - LINUX"
CLAYMORES_ARCHIVE="$CLAYMORES_NAME.tar.gz"
CLAYMORES_URL='https://mega.nz/#F!O4YA2JgD!n2b4iSHQDruEsYUvTQP5_w'
CLAYMORES_CACHE_PATH=/mnt/cache/claymores
MEGATOOLS='megatools'
LIBCURL='libcurl3'

sudo apt-get update

create_plugin.py start

sudo apt-get install --yes $MEGATOOLS $LIBCURL --no-install-recommends

if ! [ -d "$CLAYMORES_CACHE_PATH"/"$CLAYMORES_NAME" ]; then
  if  [ TRAVIS=true ]; then 
    megadl --no-progress $CLAYMORES_URL --path=/tmp/ &
    while ps aux | grep -q megadl; do
      echo "Download of Claymore's miner still in progress ..."
      sleep 30
      du -sh /tmp/Claymore* || true
    done
  else 
    megadl $CLAYMORES_URL --path=/tmp/ &
  fi
else
  echo "$CLAYMORES_ARCHIVE is already in build cache."
fi

if ! [ -d "$CLAYMORES_CACHE_PATH"/"$CLAYMORES_NAME" ]; then
  if ! [ -d $"$CLAYMORES_CACHE_PATH"/"$CLAYMORES_NAME" ]; then
    mkdir -p "$CLAYMORES_CACHE_PATH"/"$CLAYMORES_NAME"
  fi
  tar -zxvf /tmp/"$CLAYMORES_ARCHIVE" --strip-components=1 -C "$CLAYMORES_CACHE_PATH"/"$CLAYMORES_NAME"
else
  echo "$CLAYMORES_ARCHIVE is already decompressed."
fi

if ! [ -d "$DEBIRF_ROOT/usr/local/bin/claymores" ]; then
  #if ! [ -d "$DEBIRF_ROOT"/usr/local/bin/claymores ]; then
   # mkdir "$DEBIRF_ROOT"/usr/local/bin/claymores
  #fi
  sudo cp -vr "$CLAYMORES_CACHE_PATH"/"$CLAYMORES_NAME" "$DEBIRF_ROOT"/usr/local/bin/claymores
else
  echo "Claymore's Miner $CLAYMORES_RELEASE binary is already copied to the build environment."
fi

sudo tee /etc/systemd/system/claymores.service << 'EOF'
[Unit]
Description=Claymore's Miner Ethereum Minining Software
After=network-online.target

[Service]
EnvironmentFile=/etc/environment
WorkingDirectory=/usr/local/bin/claymores
ExecStart=/usr/local/bin/claymores/ethdcrminer64 -epool eu1.ethermine.org:4444 -ewal ${ETHEREUM_ADDRESS}.%H -epsw x
Restart=on-failure

[Install]
WantedBy=multi-user.target

EOF

# Disable variable expansion by quoting the label
# of the here-doc.
sudo tee /usr/local/bin/claymoresctl <<'EOF'
#!/bin/bash
# Small helper script for interacting with the claymores
# Systemd unit.

# Reset terminal colors on exit.
trap 'tput sgr0' SIGINT

case "$1" in
  start)
    sudo systemctl start claymores
    ;;
  stop)
    sudo systemctl stop claymores
    ;;
  status)
    sudo systemctl status claymores
    ;;
  log)
    # '--output cat' is used to restore colors.
    sudo journalctl -fu claymores --output cat
    ;;
esac

EOF

chmod a+x /usr/local/bin/claymoresctl

# Get rid of tools for mega.nz cause we don't need them for mining.
sudo apt purge --yes $MEGATOOLS

create_plugin.py finish coinboot_$(basename $0)_${CLAYMORES_RELEASE}_$(date +%Y%m%d.%H%M)
