#!/bin/bash

###############################################################################
## ISC LICENSE:
##
## Permission to use, copy, modify, and/or distribute this software for
## any purpose with or without fee is hereby granted, provided that the above
## copyright notice and this permission notice appear in all copies.
##
## THE SOFTWARE IS PROVIDED “AS IS” AND ISC DISCLAIMS ALL WARRANTIES WITH
## REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
## AND FITNESS.
## IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
## OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
## USE, DATA OR PROFITS,
## WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
## ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
################################################################################
## [META Info]
##
## Title: geuphoria.sh
## Version: 0.03
## Description: manage installation and removal of Euphoria Programming Language
## Author: Mollusk
## #############################################################################


name="euphoria"
version="4.1.0"
state="beta2"
arch="x64"
fext="tar.gz"
extcmd="tar"
deps="curl"
target_dir="/usr/local"
url="https://svwh.dl.sourceforge.net/project/rapideuphoria/Euphoria/${version}-${state}/${name}-${version}-Linux-${arch}-57179171dbed.${fext}"
tmpdir="/tmp"

check_deps(){

  get_path=$(which curl)

  if [ ! ${get_path} ];then
    printf "Please install: ${deps}"
    exit 0;
  else
    printf "Deps passed checks..moving on\n"
  fi

}

check_root(){

  if [[ $EUID -ne 0 ]]; then
    printf "${LRED}This script must be run as root${NC}\n" 1>&2
     exit 1
  fi
}

check_install(){

  if [ ! -d ${target_dir}/${name}-${version}-Linux-${arch} ];then
    printf "Install check passed...moving on...\n"
  else
    printf "${0}: ${name}-${version} is already installed\n"
    exit 0;
  fi
}

download_pkgs(){

  if [ ! -f ${tmpdir}/${name}-${version}.${fext} ];then
    curl -o ${tmpdir}/${name}-${version}.${fext} ${url}
  else
    printf "The archive already exists..moving to extraction phase\n"
  fi
}

extract_files(){
  cd ${tmpdir}
  if [ ! -f ${name}-${version}.${fext} ];then
    printf "${0}: ${tmpdir}/${name}-${version}.${fext}: no such file\n"
    exit 0;
  else
    ${extcmd} xvzf ${name}-${version}.${fext}
  fi
}

install_build(){
  cd ${tmpdir}
  if [ ! -d ${name}-${version}-Linux-${arch} ];then
    printf "${0}: ${name}-${version}-Linux-${arch}: No such firectory\n"
  else
    cp -r ${tmpdir}/${name}-${version}-Linux-${arch} ${target_dir}/
  fi
}

post_install(){
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eui /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/euc /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eutest /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eudist /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eudoc /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eushroud /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/euloc /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eudis /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eubind /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eub /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/echoversion /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/creole /usr/bin
  ln -v -s ${target_dir}/${name}-${version}-Linux-${arch}/bin/eucoverage /usr/bin
}

symlinks=$(which {eui,euc,eutest,eudist,eudoc,eushroud,euloc,eudis,eubind,eub,echoversion,creole,eucoverage})

uninstall_pkg(){
  if [ ! -d ${target_dir}/${name}-${version}-Linux-${arch} ];then
    printf "${name}-${version} is not installed\n"

    if [[ $symlinks ]];then
      rm $symlinks
    fi

  else

    rm -v -r ${target_dir}/${name}-${version}-Linux-${arch}

    if [[ $symlinks ]];then
      rm $symlinks
    fi

      if [ ! -d ${target_dir}/${name}-${version}-Linux-${arch} ];then
        echo
        printf "${name}-${version} is uninstalled\n"
      else
        printf "${name}-${version} still exists on the system\n"
        printf "Location: ${target_dir}/${name}-${version}-Linux-${arch}\n"
      fi
    fi
}

remove_tmp(){
  echo
  printf "Do you want to remove ${tmpdir}/${name}-${version}-Linux-${arch} and ${tmpdir}/${name}-${version}.${fext}"
  printf "[y/n]: "
  read choice

  if [ "${choice}" = "y" ];then
    rm -v -r ${tmpdir}/${name}-${version}-Linux-${arch}
    rm -v -r ${tmpdir}/${name}-${version}.${fext}
    if [ ! -d ${tmpdir}/${name}-${version}-Linux-${arch} ] &&  [ ! -f ${tmpdir}/${name}-${version}.${fext} ];then
      printf "${tmpdir} cleared\n"
    else
      printf "Failed to remove files\n"
    fi
  elif [ "${choice}" = "n" ];then
    printf "Exiting...\n"
    exit 0;
  else
    printf "Skipping and exiting...\n"
    exit 0;
  fi
}


case $1 in
  install | i)
    check_root
    check_deps
    check_install
    download_pkgs
    extract_files
    install_build
    post_install
  ;;

  remove | r)
    check_root
    uninstall_pkg
  ;;

  clean | c)
    remove_tmp
  ;;

  *)
    printf """
    [SYNOPSIS]
    ${0} <option>

    [OPTIONS]

    install | i     Install ${name}-${version}
    remove  | r     Uninstall ${name}-${version}
    clean   | c     Clear out ${name} downloads in ${tmpdir}

    """
  ;;
esac

