#! /bin/sh
# Easily install fSD packages

scriptversion="0.1.1"

#
# 
# Copyright (C) 2026 fSD
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

prefix=/usr/local/bin/
me="$(basename "$0")"
pkgs_descs=$(cat <<EOF
yait C and SH project init tool.
fes  An embedded Lua microwebframework.
fp   A package manager for the Free Software Distributions Project
EOF
)

http_download() {
	url="$1"
	outfile="$2"
	
	if command -v curl >/dev/null 2>&1; then
		curl -fSL "$url" -o "$outfile"
		return $?
	elif command -v wget >/dev/null 2>&1; then
		wget --retry-connrefused --waitretry=1 --read-timeout=20 "$url" -O "$outfile"
		return $?
	elif command -v fetch >/dev/null 2>&1; then
		fetch -o "$outfile" "$url"
		return $?
	else
		echo "$me: error: No HTTP client (curl, wget, fetch) found." >&2
		return 1
	fi
}

search() {
	pkg="$1"
	echo "$pkgs_descs" | GREP_COLORS='mt=0;92' grep -Fi --color=always "$pkg"
}

install() {
	pkg="$1"

	if [ "$(id -u)" -ne 0 ]; then
		echo "$me: error: please run as root."
		exit 1
	fi

	echo "starting install for $pkg"
	http_download "https://dl.fsdproject.org/$pkg" "$prefix$pkg" && chmod +x "$prefix$pkg"
	echo "done"
}

upgrade() {
	echo "upgrade is an alias to 'fp install $1'"
	install "$1"
}

update() {
	if [ "$(id -u)" -ne 0 ]; then
		echo "$me: error: please run as root."
		exit 1
	fi

	echo "updating $pkg"
	http_download "https://dl.fsdproject.org/fp" "${prefix}fp"
	echo "done"
}

remove() {
	pkg="$1"

	if [ "$(id -u)" -ne 0 ]; then
		echo "$me: error: please run as root."
		exit 1
	fi

	echo "removing $pkg"
	rm -f "$prefix$pkg"
	echo "done"
}

web() {
cat <<EOF
  ___            ___ _        
 | __| _ ___ ___| _ \ |____ _ 
 | _| '_/ -_) -_)  _/ / / _\` |
 |_||_| \___\___|_| |_\_\__, |
                        |___/

FreePkg (fp) is the offical package manager of the Free Software Distribtuions Project.

You have invoked the web version of this script. Remove the --web-ver flag and
you can see further options.
EOF
}

version="$me/fSD v$scriptversion

Copyright (C) 2026 fSD.
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law."

usage="Usage: $me [OPTION]... <arg1> <arg2>
Easily install fSD packages for x86_64

Options:
   --help         print this help and exit
   --version      output version information

   search         search for a package
   install        install a package
   remove         remove a package
   update         update FreePkg"

if ! [ $# -gt 0 ]; then
	echo "$usage"
	exit 0
fi

while [ $# -gt 0 ]; do
  case $1 in
    --help) echo "$usage"; exit 0 ;;
    --version) echo "$version"; exit 0 ;;
    --web-ver) web; exit 0;;
    -*) echo "$me: Unknown option '$1'." >&2; exit 1 ;;
    search) shift; search "$*"; exit 0 ;;
    install) shift; install "$1"; shift ;;
    upgrade) shift; upgrade "$1"; shift ;;
    remove) shift; remove "$1"; shift ;;
    update) update; exit 0 ;;
    *) echo "$me: Unknown option '$1'." >&2; exit 1 ;;
  esac
done
