#!/bin/bash

# Define paths
CHECK_SCRIPT="$HOME/.local/bin/rclone_mount_check.sh"
SERVICE_FILE="$HOME/.config/systemd/user/rclone_ftp_mount.service"

# Ensure the check script is executable
if [ -f "$CHECK_SCRIPT" ] && [ ! -x "$CHECK_SCRIPT" ]; then
    echo "Making the check script executable: $CHECK_SCRIPT"
    chmod +x "$CHECK_SCRIPT" || { echo "Failed to make the script executable"; exit 1; }
else
    echo "Check script already executable or doesn't exist."
fi

# Reload user systemd to recognize the new unit
systemctl --user daemon-reload

# Enable the service if not already enabled
if [ -f "$SERVICE_FILE" ]; then
    if ! systemctl --user is-enabled --quiet rclone_ftp_mount.service; then
        echo "Enabling the rclone service."
        systemctl --user enable rclone_ftp_mount.service || { echo "Failed to enable the service"; exit 1; }
    else
        echo "Service already enabled."
    fi
else
    echo "Service file does not exist at $SERVICE_FILE."
    exit 1
fi

# Start the service if not already running
if systemctl --user is-active --quiet rclone_ftp_mount.service; then
    echo "Rclone service is already running."
else
    echo "Starting the rclone service."
    systemctl --user start rclone_ftp_mount.service || { echo "Failed to start the service"; exit 1; }
fi

echo "Post-installation steps for rclone completed successfully."
