#!/bin/bash


###########################################
###
### !!!!!!! WARNING !!!!!!!
###
### Writing Progress is 70% of total ######
###
### From cloning hoshinova git repository, you should have to do manually now.
### (Msg updated at 2023-08-20 14:54:30 UTC+09:00 (KST)
###
###########################################

export DL_GO_VERSION='1.22.5'
export DL_GO_OS='linux'
export DL_GO_ARCH='amd64'
export DL_GO_TAR_GZ="go${DL_GO_VERSION}.${DL_GO_OS}-${DL_GO_ARCH}.tar.gz"

export HOSTNAME=$(hostname)
export NGINX_HOME='/etc/nginx'
export NGINX_CONF='nginx.conf'
export NGINX_LOG_ROOT='/var/log/nginx'
export NGINX_LOG_USER='www-data'
export NGINX_LOG_GROUP='adm'
export NGINX_SITES_AVAILABLE="${NGINX_HOME}/sites-available"
export NGINX_SITES_ENABLED="${NGINX_HOME}/sites-enabled"
export NGINX_DEFAULT_CONF_NAME='001-hoshinova-nginx-vhost.conf'
export NGINX_RENAME_CONF_PREFIX=$(echo "${HOSTNAME}" | sed "s|\.|_|g")
export NGINX_RENAME_CONF_NAME="001-${NGINX_RENAME_CONF_PREFIX}.conf"


cd ~ && \
echo '' && \
echo '#########################' && \
echo "# Downloading Go binary... -> ver: ${DL_GO_VERSION}, os: ${DL_GO_OS}, arch: ${DL_GO_ARCH} #" && \
wget --inet4-only "https://go.dev/dl/${DL_GO_TAR_GZ}" && \
echo '' && \
echo "# Installing Go binary... -> /usr/local/go/${DL_GO_VERSION} #" && \
mkdir -p /usr/local/go/ && \
tar -C /usr/local/go/ -xzf "${DL_GO_TAR_GZ}" && \
mv /usr/local/go/go "/usr/local/go/${DL_GO_VERSION}" && \
echo '' && \
echo '# Setting up GOROOT Globally in `/etc/profile`... #' && \
echo '' >> /etc/profile && \
echo '' >> /etc/profile && \
echo '' >> /etc/profile && \
echo '##### GO Settings #####' >> /etc/profile && \
echo "export GOROOT='/usr/local/go/${DL_GO_VERSION}'" >> /etc/profile && \
echo 'export PATH="${PATH}:${GOROOT}/bin"' >> /etc/profile && \
echo '' >> /etc/profile && \
echo '' >> /etc/profile && \
echo '' >> /etc/profile && \
echo '# Setting up GOPATH for current users... #' && \
echo '' && \
echo '' >> "${HOME}/.bashrc" && \
echo '' >> "${HOME}/.bashrc" && \
echo '##### For GO Bin Execute #####' >> "${HOME}/.bashrc" && \
echo 'export GOPATH="${HOME}/go"' >> "${HOME}/.bashrc" && \
echo 'export PATH="${PATH}:${GOPATH}/bin"' >> "${HOME}/.bashrc" && \
echo '##########################@######' >> "${HOME}/.bashrc" && \
echo '' >> "${HOME}/.bashrc" && \
echo '' >> "${HOME}/.bashrc" && \
echo '' && \
echo '--------------------------------------' && \
echo '| Now, Starting to setup programs... |' && \
echo '--------------------------------------' && \
echo '' && \
echo '# Installing dependencies for `ytarchive`... #' && \
curl -fsSL https://kr2.mirrors.naho.moe/naho/tools/ytdlp_dependency_install_tiny.sh | bash - && \
echo '' && \
. "/etc/profile" && \
. "${HOME}/.bashrc" && \
export GOROOT="/usr/local/go/${DL_GO_VERSION}" && export GOPATH="${HOME}/go" && export PATH="${PATH}:${GOROOT}/bin:${GOPATH}/bin" &&  echo '# Installing ytarchive ... #' && \
go install github.com/Kethsar/ytarchive@master && \
echo '' && \
echo '# Installing updating util for `go install` binary files... #' && \
go install github.com/Gelio/go-global-update@latest && \
echo '' && \
echo '' && \
echo '--------------------------------------' && \
echo '| Installing Rustup ...              |' && \
echo '--------------------------------------' && \
echo '' && \
echo '' >> "${HOME}/.bashrc" && \
echo '' >> "${HOME}/.bashrc" && \
echo '##### For Rustup #####' >> "${HOME}/.bashrc" && \
cd "${HOME}" && \
wget https://sh.rustup.rs -O rustup-init.sh && \
chmod +x rustup-init.sh && \
./rustup-init.sh -y && \
. "${HOME}/.cargo/env" && \
export CARGO_HOME="${HOME}/.cargo" && export PATH="${PATH}:${CARGO_HOME}/bin" && \
echo '' && \
echo '' && \
echo '--------------------------------------------------' && \
echo '| Installing `n` the Node.js Version Manager ... |' && \
echo '--------------------------------------------------' && \
echo '' && \
wget https://git.io/n-install -O n-install.sh && \
chmod +x n-install.sh && \
./n-install.sh -y && \
. "${HOME}/.bashrc" && \
echo '' && \
echo '' && \
export N_PREFIX="$HOME/n" && [[ :$PATH: == *":$N_PREFIX/bin:"* ]] || PATH+=":$N_PREFIX/bin" && \
npm i -g npm yarn node-gyp && \
echo '' && \
echo '' && \
echo '--------------------------------------------------' && \
echo '| Installing `hoshinova` from HoloArchivists ... |' && \
echo '--------------------------------------------------' && \
echo '' && \
git clone -b main --single-branch https://github.com/HoloArchivists/hoshinova.git && \
echo '' >> "${HOME}/.bashrc "&& \
echo 'export HOSHINOVA_ROOT="${HOME}/hoshinova"' >> "${HOME}/.bashrc" && \
echo 'export HOSHINOVA_BIN="${HOSHINOVA_ROOT}/bin"' >> "${HOME}/.bashrc" && \
echo 'export HOSHINOVA_TARGET="${HOSHINOVA_ROOT}/target/debug"' >> "${HOME}/.bashrc" && \
echo 'export HOSHINOVA_WEB="${HOSHINOVA_ROOT}/web"' >> "${HOME}/.bashrc" && \
echo 'export HOSHINOVA_BASE_NAME="hoshinova"' >> "${HOME}/.bashrc" && \
echo 'export HOSHINOVA_BUILT_PATH="${HOSHINOVA_TARGET}/${HOSHINOVA_BASE_NAME}"' >> "${HOME}/.bashrc" && \
echo 'export HOSHINOVA_COOKIES="${HOSHINOVA_TARGET}/cookies"' >> "${HOME}/.bashrc" && \
echo '' && \
echo '' && \
export HOSHINOVA_ROOT="${HOME}/hoshinova" && \
export HOSHINOVA_BIN="${HOSHINOVA_ROOT}/bin" && \
export HOSHINOVA_TARGET="${HOSHINOVA_ROOT}/target/debug" && \
export HOSHINOVA_WEB="${HOSHINOVA_ROOT}/web" && \
export HOSHINOVA_BASE_NAME="hoshinova" && \
export HOSHINOVA_BUILT_PATH="${HOSHINOVA_TARGET}/${HOSHINOVA_BASE_NAME}" && \
export HOSHINOVA_COOKIES="${HOSHINOVA_TARGET}/cookies" && \
cd "${HOSHINOVA_ROOT}" && \
echo '(1/4) Generating Typescript bindings ...' && \
cargo test && \
echo '' && \
echo '(2/4) Build the web UI ...' && \
cd "${HOSHINOVA_WEB}" && yarn install && yarn build && \
echo '' && \
echo '(3/4) Build the Hoshinova Excutable Binary file ...' && \
cd "${HOSHINOVA_ROOT}" && mkdir "${HOSHINOVA_BIN}" "${HOSHINOVA_COOKIES}" && \
cargo build && \
echo '' && \
echo '(4/4) Prepare systemctl executable' && \
TS=$(date +20%y%m%d-%H%M%S) && \
BUILT_NAME="${HOSHINOVA_BASE_NAME}_${TS}" && \
mv "${HOSHINOVA_BUILT_PATH}" "${HOSHINOVA_BIN}/${BUILT_NAME}" && \
cd "${HOSHINOVA_BIN}" && \
ln -s "${BUILT_NAME}" "${HOSHINOVA_BASE_NAME}" && \
echo '' && \
echo '' && \
echo '' && \
wget https://kr2.mirrors.naho.moe/naho/tools/hoshinova.service && \
mv hoshinova.service /etc/systemd/system/ && \
echo '' && \
echo '(Finalize)Starting hoshinova service' && \
systemctl enable --now hoshinova.service && \
echo '' && \
echo '####### Finished #######' && \
echo '' && \
cd "${NGINX_SITES_AVAILABLE}" && \
wget https://kr2.mirrors.naho.moe/naho/tools/001-hoshinova-nginx-vhost.conf && \
sed -i "s|____________________|${HOSTNAME}|g" "${NGINX_DEFAULT_CONF_NAME}" && \
mv "${NGINX_DEFAULT_CONF_NAME}" "${NGINX_RENAME_CONF_NAME}" && \
echo '' && \
mkdir -p "${NGINX_LOG_ROOT}/${HOSTNAME}" && \
chown -R ${NGINX_LOG_USER}:${NGINX_LOG_GROUP} "${NGINX_LOG_ROOT}" && \
echo '' && \
echo '' && \
mkdir -p "${HOME}/sbin" 2>/dev/null && \
cd "${HOME}/sbin" && \
wget https://kr2.mirrors.naho.moe/naho/tools/renew_and_enable_certbot_http2.sh && \
sed -i "s|____________________|${NGINX_RENAME_CONF_NAME}|g" 'renew_and_enable_certbot_http2.sh' && \
chmod +x 'renew_and_enable_certbot_http2.sh' && \
echo '' && \
echo '' && \
echo '' && \
echo '----- Cleaning garbages ... -----' && \
rm -f "${HOME}/${DL_GO_TAR_GZ}" 2>/dev/null && \
rm -f "${HOME}/n-install.sh" 2>/dev/null && \
rm -f "${HOME}/PKG_NAME" 2>/dev/null && \
rm -f "${HOME}/rustup-init.sh" 2>/dev/null && \
echo '' && \
echo '' && \
echo '===== Finished =====' && \
echo '' && \
echo ''
