anki-sync-server/install.sh

65 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
SERVICE_NAME=anki-sync-server
# Constants
DIR="${1:-$(pwd)}"
SERVICE_FILE_TEMPLATE="$DIR/service/anki-sync-server.service.template"
SYSTEMD_SERVICE_PATH="/etc/systemd/system/${SERVICE_NAME}.service"
ENV_SAMPLE="sample.env"
ENV_TARGET=".env"
# Check essential tools
if ! command -v systemctl &> /dev/null || ! command -v sed &> /dev/null; then
echo "Essential tools (systemctl or sed) are missing."
exit 1
fi
# Check if root
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root."
exit 1
fi
# Check if service file template exists
if [ ! -e "$SERVICE_FILE_TEMPLATE" ]; then
echo "Service file $SERVICE_FILE_TEMPLATE does not exist."
exit 1
fi
# Check and copy service file
if ! cmp -s <(sed "s|{{DIR}}|$DIR|g" "$SERVICE_FILE_TEMPLATE") "$SYSTEMD_SERVICE_PATH"; then
cp "$SERVICE_FILE_TEMPLATE" "$SYSTEMD_SERVICE_PATH" && sed -i "s|{{DIR}}|$DIR|g" "$SYSTEMD_SERVICE_PATH"
if [ $? -ne 0 ]; then
echo "Failed to copy and modify the service file."
exit 1
fi
systemctl daemon-reload
if [ $? -ne 0 ]; then
echo "Failed to reload the daemon."
exit 1
fi
else
echo "Service file is up-to-date. Skipping..."
fi
# Check and copy .env file
if [ ! -e "$ENV_TARGET" ]; then
if [ -e "$ENV_SAMPLE" ]; then
cp "$ENV_SAMPLE" "$ENV_TARGET"
if [ $? -ne 0 ]; then
echo "Failed to create .env file."
exit 1
fi
else
echo "Sample env file $ENV_SAMPLE does not exist."
exit 1
fi
else
echo ".env file already exists. Skipping..."
fi
# Script completed successfully
exit 0