Name: hyperspace
Version: 5.0.237
Release: 1%{?dist}
Summary: WhiteStar HyperSpace Service
License: GPL

Source0: https://whitestar.io/repo/hyperspace/5.0.055/5.0.237/hyperspace.tar.xz
BuildArch: noarch
ExclusiveArch: %{nodejs_arches} noarch
BuildRequires: make

Requires: telnet

%description
WhiteStar HyperSpace Service

%prep
%autosetup -n %{name}

%build
ESLINT=0 NODE_ENV=production make

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

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

# Script to be run after package is installed
%post
chown -R root /home/whitestar/hyperspace
chown -R root /home/whitestar/hyperspace/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/hyperspace.service ]; then
  echo "##"
  echo "## Creating the HyperSpace Service"
  echo "##"
  cat > /etc/systemd/system/hyperspace.service <<EOF
  [Unit]
  Description = WhiteStar HyperSpace Service
  After=network-online.target iscsid.service mnt.mount
  [Service]
  WorkingDirectory=/home/whitestar/hyperspace/dist
  ExecStart=/opt/jdk-21.0.1/bin/java -XX:+UseStringDeduplication -Dpty=true -jar hyperSpace.jar 
  User=root
  Type=simple
  Restart=on-failure
  RestartSec=10
  [Install]
  WantedBy=multi-user.target
EOF
fi

if [ ! -e /usr/lib/firewalld/services/hyperSpace.xml ]; then
  echo "##"
  echo "## Creating the HyperSpace FirewallD rule file"
  echo "##"
  cat > /usr/lib/firewalld/services/hyperSpace.xml <<EOF
<?xml version="1.0" encoding="utf-8"?>
<service>
  <short>HyperSpace</short>
  <description>WhiteStar Communications, Inc. HyperSpace Hybrid Quantum Networking</description>
  <port protocol="tcp" port="42580-42599"/>
</service>
EOF

firewall-cmd --reload
fi
 
#
# Enable the HyperSpace Service - if we choose to start verify it is running
#
echo "##"
echo "## Enable the HyperSpace Service"
echo "##"
systemctl daemon-reload
systemctl enable hyperspace.service
systemctl restart hyperspace.service

echo "## WhiteStar HyperSpace 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 hyperspace service
if [ $1 -eq 0 ] ; then
  rm -f /etc/systemd/system/hyperspace.service;
  systemctl stop hyperspace.service;
  systemctl disable hyperspace.service;
  systemctl daemon-reload;
fi

%changelog