#!/usr/bin/env bash
set -e

apt update
apt install -y openjdk-8-jre-headless

echo "nexus - nofile 65536" > /etc/security/limits.d/nexus.conf
rm -f nexus-3.41.1-01-unix.tar.gz
wget https://download.sonatype.com/nexus/3/nexus-3.41.1-01-unix.tar.gz
tar xzf nexus-3.41.1-01-unix.tar.gz
mv nexus-3.41.1-01 /opt/nexus
mv sonatype-work /opt/
chown -R nexus:nexus /opt/nexus /opt/sonatype-work
echo "run_as_user=\"nexus\"" > /opt/nexus/bin/nexus.rc
mkdir -p /opt/sonatype-work/nexus3/etc
echo "application-port=2001" > /opt/sonatype-work/nexus3/etc/nexus.properties

cat > /etc/systemd/system/nexus.service << EOF
[Unit]
Description=nexus service
After=network.target

[Service]
Type=forking
LimitNOFILE=65536
ExecStart=/opt/nexus/bin/nexus start
ExecStop=/opt/nexus/bin/nexus stop
User=nexus
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

systemctl daemon-reload
systemctl enable nexus.service
