#!/bin/bash
set -e

HOSHINOVA_BASE_NAME="hoshinova"
HOSHINOVA_HOME="${HOME}/${HOSHINOVA_BASE_NAME}"
HOSHINOVA_BIN="${HOSHINOVA_HOME}/bin"
HOSHINOVA_TARGET="${HOSHINOVA_HOME}/target/debug"
HOSHINOVA_WEB="${HOSHINOVA_HOME}/web"
HOSHINOVA_BUILT_PATH="${HOSHINOVA_TARGET}/${HOSHINOVA_BASE_NAME}"

TS=$(date +20%y%m%d-%H%M%S)
HOSHINOVA_BIN_NEW="${HOSHINOVA_BASE_NAME}_${TS}"

rustup update && \
n lts && \
npm i -g npm yarn node-gyp && \
cd "${HOSHINOVA_HOME}" && \
cargo test && \
cd "${HOSHINOVA_WEB}" && \
yarn && \
yarn build && \
cd ${HOSHINOVA_HOME} && \
cargo build && \
mv "${HOSHINOVA_BUILT_PATH}" "${HOSHINOVA_BIN}/${HOSHINOVA_BIN_NEW}" && \
cd "${HOSHINOVA_BIN}" && \
rm -f "${HOSHINOVA_BIN}/${HOSHINOVA_BASE_NAME}" && \
ln -s "${HOSHINOVA_BIN_NEW}" "${HOSHINOVA_BASE_NAME}" && \
systemctl daemon-reload
