#!/bin/bash

set -euo pipefail   # safety – stop on errors, unset vars, etc.

if [ ! -t 0 ]; then
    notify-send -t 10000 "Error" "This script must be run from such as a terminal emulator (not a file manager)."
    exit 1
fi

read -p "Warning: Do not run this if you moved the UPDATE script outside of its directory. This will go up two directories and copy shule to /opt/shule (and completely overwrite anything there). Do you wish to proceed? (y/n): " answer

# Check if the answer starts with Y or y
if [[ "$answer" =~ ^[Yy](es)?$ ]]; then
    echo "Proceeding..."
    # Insert your 'yes' code here
    
    #Get the absolute path of this update script:
    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
    #echo $DIR
    
    #Go there.
    cd "$DIR"
    
    #Go up into the shule directory.
    cd ..
    
    #Get the name shule
    PROGRAM_NAME=`basename "$PWD"`
    
    #Go up into whatever directory contains the shule directory
    cd ..
    #echo $PROGRAM_NAME
    
    #Overwrite /opt/shule with the shule directory (or copy it there if absent). This will delete any custom content you have in it (but it helps to ensure a clean installation that works)!
    sudo cp -rT "$PROGRAM_NAME" /opt/shule
    
    #Install Shule's dependencies.
    echo "Shule has been installed. Updating apt, installing dependencies, and clearing font cache."
    "$DIR/DEPEND"
elif [[ "$answer" =~ ^[Nn](o)?$ ]]; then
    echo "Canceled."
else
    echo "Canceled."
fi