# Script for Setting Up Medplum on Debian-Based Systems

## Check if Running as Root  
if [ "$(id -u)" -ne 0 ]; then  
  echo "This script must be run as root (or with sudo)"  
  exit 1  
fi

## Make Sure the Script is Running on a Debian-Based System  
if ! [ -f "/etc/debian_version" ]; then  
  echo "Error: This script is only supported on Debian-based systems."  
  exit 1  
fi

## Check if Node Exists  
if ! command -v node &> /dev/null; then  
  echo "Node.js is required but not installed"  
  echo "To install Node.js, visit: https://deb.nodesource.com/"  
  exit 1  
fi

## Cleanup Old Sources  
rm -f /usr/share/keyrings/apt.medplum.com.asc || true  
rm -f /etc/apt/sources.list.d/apt.medplum.com.list || true

## Download GPG Key  
if ! wget -O /usr/share/keyrings/apt.medplum.com.asc https://apt.medplum.com/apt.medplum.com.asc; then  
  echo "Failed to download Medplum signing key"  
  exit 1  
fi

## Set Permissions  
if ! chmod 644 /usr/share/keyrings/apt.medplum.com.asc; then  
  echo "Failed to set correct permissions on /usr/share/keyrings/apt.medplum.com.asc"  
  exit 1  
fi

## Add Sources  
if ! echo "deb [signed-by=/usr/share/keyrings/apt.medplum.com.asc] https://apt.medplum.com/debian stable main" | tee /etc/apt/sources.list.d/apt.medplum.com.list > /dev/null; then  
  echo "Failed to add Medplum repository to sources list"  
  exit 1  
fi

echo "Medplum repository has been configured successfully."  
echo ""  
echo "To install Medplum, run:"  
echo "  sudo apt-get update"  
echo "  sudo apt-get install medplum"
