2023-09-06 17:09:19 +00:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-09-10 08:53:27 +00:00
|
|
|
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"
|
2023-09-06 17:09:19 +00:00
|
|
|
ENV_SAMPLE="sample.env"
|
|
|
|
ENV_TARGET=".env"
|
|
|
|
|
2023-09-10 08:53:27 +00:00
|
|
|
# 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
|
|
|
|
|
2023-09-06 17:09:19 +00:00
|
|
|
# Check if root
|
|
|
|
if [[ $EUID -ne 0 ]]; then
|
|
|
|
echo "This script must be run as root."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
|
2023-09-10 08:53:27 +00:00
|
|
|
# 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."
|
2023-09-06 17:09:19 +00:00
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2023-09-10 08:53:27 +00:00
|
|
|
echo "Service file is up-to-date. Skipping..."
|
2023-09-06 17:09:19 +00:00
|
|
|
fi
|
|
|
|
|
2023-09-10 08:53:27 +00:00
|
|
|
# Check and copy .env file
|
2023-09-06 17:09:19 +00:00
|
|
|
if [ ! -e "$ENV_TARGET" ]; then
|
|
|
|
if [ -e "$ENV_SAMPLE" ]; then
|
|
|
|
cp "$ENV_SAMPLE" "$ENV_TARGET"
|
2023-09-10 08:53:27 +00:00
|
|
|
if [ $? -ne 0 ]; then
|
2023-09-06 17:09:19 +00:00
|
|
|
echo "Failed to create .env file."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "Sample env file $ENV_SAMPLE does not exist."
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
else
|
2023-09-10 08:53:27 +00:00
|
|
|
echo ".env file already exists. Skipping..."
|
2023-09-06 17:09:19 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# Script completed successfully
|
|
|
|
exit 0
|
|
|
|
|