#!/bin/bash

# Install a new release of XyScript.  Be sure to edit paths and file
# names in the code below.  For Linux.  Tested only on Arch Linux.

# New version number must be entered as x.x.x for instance 0.1.6
# This relies on a fixed form of the name of the archive file, and a
# fixed source for the archive.  If Carl changes these, the script
# will need to be modified.  See below in the code.

# IF THE SCRIPT FAILS DON'T RUN AGAIN UNTIL YOU FIX STUFF.  I AM NOT
# RESPONSIBLE FOR DATA LOSS.  USE AT YOUR OWN RISK.

# If the script fails, you may have to copy xyscript.bak over xyscript
# to recover your previous installation.  Be sure to make an
# extra backup of your xyscript directory before starting this script.
# Don't end up wishing you had a backup but you don't.

# NO WARRANTEES OF ANY KIND, INCLUDING WARRANTEES OF MERCHANTABILITY OR
# FITNESS FOR INTENDED PURPOSE, ARE PROVIDED.

# NO SUPPORT PROVIDED.

# I AM NOT RESPONSIBLE FOR DATA LOSS OR DAMAGES OF ANY KIND UNDER ANY
# CIRCUMSTANCES OR UNDER ANY LEGAL THEORY WHATSOEVER, EVEN IF I HAVE
# BEEN APPRISED OF THE POSSIBILITY OF DAMAGES.  USE AT YOUR OWN RISK.

# Latest: 2026-09-23
# Released into the public domain.
# Bob Newell
# Honolulu, Hawai`i, USA
# bobnewell@bobnewell.net

##################
# EDIT TO SUIT:
##################
# Where to download the archive.
downloaddir="/home/bnewell/Downloads"
# Where to install the XyScript directory.
installdir="/usr/local/share"
# Files to preserve from previous installation.
files_to_keep="settingz.xpl startup.xpl xyscript-rjn.kbd emacs*"
##################

# Change these if Carl changes stuff.
archive_base_name="xyscript-linux-x86_64-v"
extracted_directory_name="xyscript-linux-x86_64"
archive_suffix="zip"
archive_base_url="https://ammaze.net/xyscript/binaries"
#

# Get version to install.
clear
echo " "
echo "****************************************"
echo "Ctrl-C to abort if you do not have"
echo "a safe backup of your xyscript files!"
echo " "
echo "Seriously.  Do this first; avoid regrets."
echo "****************************************"
echo " "
read -p "Version to install: " vinst
if [[ "$vinst" == "" ]]
then
  echo "ERROR Need version number"
  exit 1
fi

# Build variables.
archive_full_name="$archive_base_name$vinst.$archive_suffix"

if [[ -d "$downloaddir" ]]
then
    cd "$downloaddir"
    # Fetch the archive.
    archive_full_url="$archive_base_url/$archive_full_name"
    echo "Fetching $archive_full_url ... may take a little while"
    curl --output "$archive_full_name" "$archive_full_url" >/dev/null 2>&1
    if [[ $? -ne 0 ]]
    then
	echo "ERROR fetching $archive_full_url failed"
	exit 1
    fi
else
    echo "ERROR $downloaddir not found"
    exit 1
fi

# Unpack the archive
zipper="$downloaddir/$archive_full_name"
if [[ -f "$zipper" && -r "$zipper" ]]
then
   echo "Installing from $zipper"
   # Go to install dir.
   if [[ -d "$installdir" ]]
   then
      cd "$installdir"
   else
       echo "ERROR $installdir not found"
       exit 1
   fi
   # See about doing a temporary backup.
   if [[ -d xyscript ]]
   then
      if [[ -d xyscript.bak ]]
      then
	 echo "Deleting old backup"
         rm -Rf xyscript.bak
      fi
      echo "Backing up xyscript"
      cp -a xyscript xyscript.bak
      echo "Deleting xyscript"
      rm -Rf xyscript
   fi
    echo "Extracting from archive"
    unzip -o $zipper >/dev/null
    mv "$extracted_directory_name" xyscript
    if [[ -d xyscript.bak ]]
    then
	echo "Copying over user files"
	cd xyscript.bak
        cp $files_to_keep ../xyscript
    fi
    cd ../xyscript
    # Optional:
    command -v upx >/dev/null 2>&1
    if [[ $? -eq 0 ]]
    then
    	echo "Packing the executable ... takes a long while ... do not interupt"
    	upx -9 xyscript >/dev/null 2>&1
    fi
    echo "Install successful but please verify manually!"
else
   echo "Can't find or read $zipper"
fi
exit 0
