#!/bin/bash

# Set version number
VERSION=1.1.0

# Set temporary and installation directories
TMP_DIR=/tmp/nvidia_gpu_exporter_install
INSTALL_DIR=/usr/bin

# Create temporary directory
mkdir -p $TMP_DIR

# Download and extract nvidia_gpu_exporter
wget -P $TMP_DIR https://github.com/utkuozdemir/nvidia_gpu_exporter/releases/download/v${VERSION}/nvidia_gpu_exporter_${VERSION}_linux_x86_64.tar.gz

tar -xvzf $TMP_DIR/nvidia_gpu_exporter_${VERSION}_linux_x86_64.tar.gz -C $TMP_DIR

# Create installation directory and move binary
mkdir -p $INSTALL_DIR
mv $TMP_DIR/nvidia_gpu_exporter $INSTALL_DIR/
chmod +x $INSTALL_DIR/nvidia_gpu_exporter

# Create system user
useradd --system --no-create-home --shell /usr/sbin/nologin nvidia_gpu_exporter

# Create systemd service file
cat <<EOF | tee /etc/systemd/system/nvidia_gpu_exporter.service
[Unit]
Description=Nvidia GPU Exporter
After=network-online.target

[Service]
Type=simple
User=nvidia_gpu_exporter
Group=nvidia_gpu_exporter
ExecStart=$INSTALL_DIR/nvidia_gpu_exporter
SyslogIdentifier=nvidia_gpu_exporter
Restart=always
RestartSec=1

[Install]
WantedBy=multi-user.target
EOF

# Reload systemd and enable service
systemctl daemon-reload
systemctl enable --now nvidia_gpu_exporter

# Check service status
systemctl status nvidia_gpu_exporter --no-pager

