#!/bin/bash

# One-time initialization of this repository, to be run after cloning.


cd "$( dirname "${BASH_SOURCE[0]}" )"
mkdir -p log
LOG=log/init.log
echo "Logging stdout and stderr to $LOG"

(

# Install submodules.
git submodule update --init --recursive


# Install required packages


# Platform-specific installs.
echo "Installing linux packages."
mkdir -p local
COMMON_PACKAGES='make inotify-tools bc remmina jq perl unzip python3-tornado'
DEBUG_PACKAGES='vim htop tmux'
if [[ -n "$(which apt-get 2> /dev/null)" ]]
then
  # Ubuntu
  sudo apt-get update
  sudo apt-get -y install curl g++ python3 python3-pip python3-pil $COMMON_PACKAGES
  sudo apt -y install $DEBUG_PACKAGES
  #sudo python3 -m pip install Pillow
elif [[ -n "$(which yum 2> /dev/null)" ]]
then
  # CentOS

  # For Python:
  sudo yum makecache
  sudo yum -y install yum-utils curl
  sudo yum -y install https://repo.ius.io/ius-release-el7.rpm https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm # new URLs by IUS "https://ius.io/setup"
  sudo yum makecache
  sudo yum -y install python3 python3-pillow # this will install python3.6 version
  # To look like Ubuntu, we need python3 and pip3.

  sudo yum -y install $COMMON_PACKAGES  #  python-tornado python-imaging redundant w/ pip install?
  sudo yum -y install $DEBUG_PACKAGES 

  ## bash lives at /usr/bin/bash in CentOS, but in /bin/bash for Ubuntu. Create /bin/bash.
  #sudo ln -s /bin/bash /usr/bin/bash    # TODO: So far, untested. Test and delete this comment.
fi


# Install python libraries for user only so as not to affect system installs.
echo "Installing Python packages for user $USER."
sudo python3 -m pip install --upgrade pip
USER_PYTHON_PACKAGES='Pillow tornado awscli boto3 sandpiper-saas'
python3 -m pip install --user --upgrade $USER_PYTHON_PACKAGES
#sudo python3 -m pip install --user --upgrade $USER_PYTHON_PACKAGES


# Configure remmina to disble the tray icon (so, it isn't left running when launched via Makefile).
if [[ -e "$HOME/.remmina/remmina.pref" ]]
then
  sed -i 's/^disable_tray_icon=false$/disable_tray_icon=true/' "$HOME/.remmina/remmina.pref"
else
  mkdir -p "$HOME/.remmina"
  echo [remmina_pref] > "$HOME/.remmina/remmina.pref"
  echo 'disable_tray_icon=true' >> "$HOME/.remmina/remmina.pref"
fi
# Protect remmina.pref as this contains an encryption key. (No idea why remmina leaves this open.)
# Passwords stored in remmina can be decrypted by anyone with access to this file.
chmod 400 "$HOME/.remmina/remmina.pref"


# Make sure ~/.local/bin is in path for python installs.
#
echo "Making sure Python installs are in \$PATH."
if [[ $( which aws > /dev/null 2>&1 )$? != 0 ]]
then
  # Need to add ~/.local/bin to $PATH.
  echo
  if [[ "$SHELL" == "/bin/bash" ]]
  then
    if [[ -e "$HOME/.bashrc" ]]
    then
      if ( grep fpga-webserver "$HOME/.bashrc" > /dev/null 2>&1 ) ||
         ( grep 1st-CLaaS "$HOME/.bashrc" > /dev/null 2>&1 )
      then
        echo "INFO: Strange, it looks like ~/.bashrc has already been modified, but 'aws' command is not currently in \$PATH. Might need to add ~/.local/bin to your \$PATH manually."
      else
        echo >> "$HOME/.bashrc"
        echo "export PATH=\$PATH:$HOME/.local/bin  # Added by 1st-CLaaS repository." >> ~/.bashrc
        echo "==================================="
        echo "Modified ~/.bashrc to add ~/.local/bin to path, and sourcing ~/.bashrc."
        echo "==================================="
        source "$HOME/.bashrc"
        if ( ! which aws > /dev/null 2>&1 )
        then
          echo "WARNING: Still cannot find 'aws' command."
        fi
      fi
    else
      echo "WARNING: Cannot find ~/.bashrc. You must add ~/.local/bin to your \$PATH manually."
    fi
  else
    echo "NOTE: You must add ~/.local/bin to your \$PATH manually."
  fi
  echo
fi


# Download Terraform binary.
# TODO: Move this into /local.
if [[ ! -d terraform ]]
then
  echo "Downloading Terraform"
  mkdir terraform
  ( cd terraform && \
    curl https://releases.hashicorp.com/terraform/1.5.2/terraform_1.5.2_linux_amd64.zip > terraform.zip \
    && unzip terraform.zip \
    && rm terraform.zip \
    && chmod +x terraform )
fi
# Note that framework/Makefile and bin/regress use the existence of terraform/terraform to indicate that this script was run.


# Check Verilator installation.
# Need Verilator 4+ (ish).
# Installation runs long, so here, we just report what should be done.
echo -e "\e[1m\e[33m"  # Report in bold yellow.
# if [[ no local install ]] && ( no verilator || verilator is not version 4+ )
if [[ ! -n "$(ls "local/verilator/bin/verilator" 2> /dev/null)" ]] && ( ! which verilator &> /dev/null || ! (( $(verilator --version | sed 's/^Verilator \([[:digit:]]\+\)\..*$/\1/') > 3 )) > /dev/null )
then
  echo "Could not find Verilator 4+."
  if which verilator &> /dev/null
  then
    echo "You have: $(verilator --version)"
    if which verilator &> /dev/null
    then
       # Ubuntu
       echo "Uninstall first (sudo apt-get remove verilator?), then:"
    else
       echo "Uninstall first, then:"
    fi
  fi
  echo "Install using ./bin/install_verilator, or visit veripool.org for information about the latest version."
fi
echo -e "\e[0m"  # Back to normal font.
) |& tee $LOG
