#!/usr/bin/env sh

VER=0.5

PROJECT_NAME="ASDWebSigner"

PROJECT_PACKAGE="$PROJECT_NAME.tar.gz"

PROJECT_PACKAGE_URL="http://cedr-fm.mfcr.cz/CEDRNFFM3/ASDWebSigner/Resources/Installer/Linux/gz"

DEFAULT_INSTALL_HOME="$HOME/.$PROJECT_NAME"

_WGET=`which wget`
_TAR=`which tar`
_MKDIR=`which mkdir`
_RM=`which rm`
_MIME_UPD=`which update-mime-database`

DEFAULT_OUT="out.txt"

PROTOCOL_NAME="asdwebsigner"
PROTOCOL_RUNTIME="asdsoft-websignerclientruntime"

#=========== fce ===============

__green() {
    printf '\033[1;31;32m'
    printf -- "%b" "$1"
    printf '\033[0m'
}

__red() {
    printf '\033[1;31;40m'
    printf -- "%b" "$1"
    printf '\033[0m'
}

_printdateline() {
  if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
    printf -- "%s" "[$(date)] "
  fi
  if [ -z "$2" ]; then
    printf -- "%s" "$1"
  else
    printf -- "%s" "$1='$2'"
  fi
}


_printargs() {
  if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
    printf -- "%s" "[$(date)] "
  fi
  if [ -z "$2" ]; then
    printf -- "%s" "$1"
  else
    printf -- "%s" "$1='$2'"
  fi
  printf "\n"
}

_usage() {
  __red "$@" >&2
  printf "\n" >&2
}

_exists() {
  cmd="$1"
  if [ -z "$cmd" ]; then
    _usage "Usage: _exists cmd"
    return 1
  fi

  if eval type type >/dev/null 2>&1; then
    eval type "$cmd" >/dev/null 2>&1
  elif command >/dev/null 2>&1; then
    command -v "$cmd" >/dev/null 2>&1
  else
    which "$cmd" >/dev/null 2>&1
  fi
  ret="$?"
  #_debug3 "$cmd exists=$ret"
  #_printargs "$cmd exists=$ret" >&2
  return $ret
}

_info() {
  _printargs "$@"
}

_err() {
  if [ -z "$NO_TIMESTAMP" ] || [ "$NO_TIMESTAMP" = "0" ]; then
    printf -- "%s" "[$(date)] " >&2
  fi
  if [ -z "$2" ]; then
    __red "$1" >&2
  else
    __red "$1='$2'" >&2
  fi
  printf "\n" >&2
  return 1
}

_precheck() {

  if ! _exists "wget" && ! _exists "curl"; then
    _err "Please install curl or wget first, we need to access http resources."
    return 1
  fi
  return 0
}

_download() {
   _printdateline "$(__green "downloading ...")"
   response="$($_WGET -q "$PROJECT_PACKAGE_URL" -O $PROJECT_PACKAGE)"
   _ret="$?"
   if [ "$_ret" != "0" ]; then
     __red " false!"
      printf "\n" >&2
     _err "Error downloading, please refer to https://www.gnu.org/software/wget/manual/html_node/Exit-Status.html for error code: $_ret"
     return 1
   fi
   __green " ok!"
   printf "\n" >&2
   return $_ret
   
}

_extract() {
   _printdateline "$(__green "unpacking ...")"
   response="$($_TAR xf "$PROJECT_PACKAGE" -C $HOME)"
   _ret="$?"
   $_RM -f $PROJECT_PACKAGE
   if [ "$_ret" != "0" ]; then
     __red " false!"
      printf "\n" >&2
     _err "Error unpacking : $_ret"
     return 1
   fi
   __green " ok!"
  printf "\n" >&2
  return $_ret
}


_seturlprotocol() {
 _printdateline "$(__green "set protocol ...")"
 
 $_MKDIR -p ~/.local/share/applications
 $_MKDIR -p ~/.local/share/mime/packages
 
 echo "[Added Associations]
text/html=firefox.desktop;chromium-browser.desktop;
x-scheme-handler/$PROTOCOL_NAME=$PROTOCOL_NAME.desktop;

[Default Applications]
text/html=firefox.desktop
x-scheme-handler/$PROTOCOL_NAME=$PROTOCOL_NAME.desktop
" > ~/.local/share/applications/mimeapps.list

 echo "[Desktop Entry]
Encoding=UTF-8
Version=3
Type=Application
Terminal=false
Name=ASDSoft.WebSigner
Exec=$DEFAULT_INSTALL_HOME/$PROTOCOL_RUNTIME %u
Path=$DEFAULT_INSTALL_HOME/
Icon=
MimeType=x-scheme-handler/$PROTOCOL_NAME
" > ~/.local/share/applications/$PROTOCOL_NAME.desktop

$_MIME_UPD ~/.local/share/mime

  __green " ok!"
  printf "\n" >&2
}


version() {
  echo "$PROJECT_PACKAGE_URL"
  echo "v$VER"
 
}


#=========== main ========


main() {
  if ! _precheck ; then
	return 1
  fi
  if ! _download ; then
	return 1
  fi
  if ! _extract  ; then
	return 1
  fi
  _seturlprotocol
  
}

main "$@"

