Name: wsh
Version: 5.0.211
Release: 1%{?dist}
Summary: Whitestar Shell Service
License: GPL

Source0: https://whitestar.io/repo/wsh/5.0.024/5.0.211/wsh.tar.xz
BuildArch: noarch
ExclusiveArch: %{nodejs_arches} noarch
BuildRequires: make

Requires: telnet

%description
Whitestar Shell Service

%prep
%autosetup -n %{name}

%build
ESLINT=0 NODE_ENV=production make

%install
make install-service DESTDIR=%{buildroot} PREFIX=/home/whitestar/wsh

%files
%doc README.md
%attr(0744, whitestar, whitestar) /home/whitestar/wsh/*
%attr(0744, whitestar, whitestar) /home/whitestar/wsh/dist/*
%attr(0755, whitestar, whitestar) /home/whitestar/wsh/dist/libs/*

# Script to be run after package is installed
%post
#
# Create the user: whitestar with password whitestar and admin privileges
# password is encrypted below using crypt() command outside this routine
#
# perl -e 'print crypt("whitestar", "salt"),"\n"'
#
echo "##"
echo "## Creating whitestar userid...and giving it admin authority"
echo "##"
id -u whitestar || (
  echo "Creating new User: whitestar"
  useradd -m -p saVcBwwVb0fMY whitestar
)
usermod -aG wheel whitestar
chown -R whitestar /home/whitestar/wsh
chown -R whitestar /home/whitestar/wsh/dist

# if java 21 folder is not installed, install it
if [ ! -d /opt/jdk-21.0.1 ]; then
  echo "##"
  echo "## Installing Java 21.0.1"
  echo "##"
  cd /tmp
  wget -c https://cdn.azul.com/zulu/bin/zulu21.30.15-ca-fx-jdk21.0.1-linux_x64.tar.gz
  tar xzf zulu21.30.15-ca-fx-jdk21.0.1-linux_x64.tar.gz
  mv zulu21.30.15-ca-fx-jdk21.0.1-linux_x64  /opt/jdk-21.0.1
fi

if [ ! -e /etc/systemd/system/wsh.service ]; then
  echo "##"
  echo "## Creating the WSH Service"
  echo "##"
  cat > /etc/systemd/system/wsh.service <<EOF
  [Unit]
  Description = WhiteStar Shell
  After=network-online.target iscsid.service mnt.mount
  [Service]
  WorkingDirectory=/home/whitestar/wsh/dist
  ExecStart=/opt/jdk-21.0.1/bin/java -XX:+UseStringDeduplication -Dpty=true -jar wsh.jar 
  User=whitestar
  Type=simple
  Restart=on-failure
  RestartSec=10
  [Install]
  WantedBy=multi-user.target
EOF
fi
 
#
# Enable the WSH Service - if we choose to start verify it is running
#
echo "##"
echo "## Enable the WSH Service"
echo "##"
systemctl daemon-reload
systemctl enable wsh.service
systemctl restart wsh.service

echo "## WhiteStar Shell Service Started!";

# Script to be run after package is un-installed
%postun
# $1 variable determines whether this is an upgrade ($1 == 1) or uninstall ($1 == 0)
# if set to uninstall, remove the wsh service
if [ $1 -eq 0 ] ; then
  rm -f /etc/systemd/system/wsh.service;
  systemctl stop wsh.service;
  systemctl disable wsh.service;
  systemctl daemon-reload;
fi

%changelog