# (C) Andrea Giammarchi @WebReflection
# this is the esiest way to install `jsgtk` via terminal

pkgname=jsgtk

# OSX / MacPorts users
# there is a bug in MacPorts taglib
# https://trac.macports.org/ticket/51318
# https://trac.macports.org/ticket/51319

# Homebrew Users
# WebKitGKT will most likely fail :-(

# if you want to have pure quartz install:
# PURE_QUARTZ=true

if [ "$TMPDIR" = "" ]; then
  TMPDIR=/tmp
fi

# if specified, use $JSGTK_INSTALL_PATH folder
# e.g. export JSGTK_INSTALL_PATH=/opt/local
if [ "$JSGTK_INSTALL_PATH" != "" ]; then
  destdir="$JSGTK_INSTALL_PATH"
else
  # verify there is a usable /bin and /lib folder
  if [ -d /usr/local/bin ]; then
    destdir=/usr/local
  else
    if [ -d /opt/local/bin ]; then
      destdir=/opt/local
    else
      echo ""
      if [ "$(which brew)" = "" ] && [ "$(which port)" = "" ]; then
        echo "  $(tput bold)Dependencies issue:$(tput sgr0)"
        echo '    please install either homebrew or macports to have GJS'
        echo '      http://brew.sh/'
        echo '      https://www.macports.org/'
      else
        echo " $(tput bold)[WARNING]$(tput sgr0): unable to locate a folder where to install jsgtk."
        echo "   Please run again this script after the following command:"
        echo " export JSGTK_INSTALL_PATH=~/.bin"
      fi
      echo ""
      exit 1
    fi
  fi
fi

# verify that target lib folder will be reachable
if [ ! -d "${destdir}/lib" ]; then
  mkdir -p "${destdir}/lib"
  if [ $? -ne 0 ]; then
    echo ""
    echo " $(tput bold)[WARNING]$(tput sgr0): unable to install jsgtk."
    echo "   Please be sure ${destdir}/bin and ${destdir}/lib exists."
    echo ""
    exit 1
  fi
fi

destbin="${destdir}/bin/${pkgname}"
destlib="${destdir}/lib/${pkgname}"

# fetch latest version from gh-pages
pkgver=$(curl -sL https://webreflection.github.io/jsgtk/version)
pkgdir="${TMPDIR}/${pkgname}-${pkgver}"
source="https://webreflection.github.io/jsgtk/archive/${pkgname}-${pkgver}.tar.gz"
macported='false'

# info message and ask for sudo
echo "

  $(tput bold)jsgtk ${pkgver}$(tput sgr0)

    jsgtk runs via $(tput bold)${destbin}$(tput sgr0)
    and uses its libraries in $(tput bold)${destlib}$(tput sgr0)

  sudo privilege is needed to install dependencies:

"

# be sure the sudo password is known
authorized=$(sudo sh -c "echo '${pkgver}'")
if [ $? -ne 0 ] || [ "${authorized}" != "${pkgver}" ]; then
  echo ''
  echo "  $(tput bold)sudo password is necessary$(tput sgr0) to install lib and bin"
  echo ''
  exit 1
fi

# verify dependencies
if [ "$(which gjs 2> /dev/null)" = "" ]; then
  if [ "$(uname)" = "Darwin" ]; then
    if [ "$(which port)" = "" ]; then
      if [ "$(which brew)" = "" ]; then
        echo "  $(tput bold)Dependencies issue:$(tput sgr0)"
        echo '    please install either homebrew or macports to have GJS'
        echo '      http://brew.sh/'
        echo '      https://www.macports.org/'
        exit 1
      else
        echo "  Installing $(tput bold)GJS$(tput sgr0) via $(tput bold)Homebrew$(tput sgr0)"
        sleep 2
        brew install gjs adwaita-icon-theme libnotify
        if [ "$(which gjs 2> /dev/null)" = "" ]; then
          brew link --overwrite gjs
        fi
        if [ "$WEBKIT" != "" ]; then
          brew install webkitgtk
        fi
      fi
    else
      macported='true'
      echo "  Installing $(tput bold)GJS$(tput sgr0) via $(tput bold)MacPorts$(tput sgr0)"
      sleep 2
      # if X11 is specified, use this variant
      if [ "$X11" = "true" ]; then
        TODO='
          port install gtk3 +x11
          port install xorg-server xorg-xinit
          port install gjs adwaita-icon-theme libnotify
        '
        if [ "$WEBKIT" != "" ]; then
          TODO="${TODO}
          port install webkit2-gtk +x11 -gtk2
          "
        fi
      else
        if [ "$PURE_QUARTZ" = "true" ]; then
          # remove gst-plugins-bad from dependencies
          # forces quartz only variants
          TODO='
            sed -i.bak -e '"'"':a'"'"' -e '"'"'N'"'"' -e '"'"'$!ba'"'"' -e '"'"'s/[ ]*port:gstreamer1-gst-plugins-bad[ ]*\\\n//'"'"' $(port file webkit2-gtk)
            cp /opt/local/etc/macports/variants.conf /opt/local/etc/macports/variants.conf.bak
            echo "-x11 +no_x11 +quartz +gtk3 -gtk2 +no_gtk2" > /opt/local/etc/macports/variants.conf
            port install gtk3 +quartz
            port install gjs adwaita-icon-theme libnotify
          '
          if [ "$WEBKIT" != "" ]; then
            TODO="${TODO}
            port install webkit2-gtk +quartz
            "
          fi
          TODO="${TODO}
            mv /opt/local/etc/macports/variants.conf.bak /opt/local/etc/macports/variants.conf
          "
        else
          TODO='
            port install gtk3 +quartz
            port install gjs adwaita-icon-theme
          '
          if [ "$WEBKIT" != "" ]; then
            TODO="${TODO}
            port install webkit2-gtk +quartz
            "
          fi
        fi
      fi
      if [ "$WEBKIT" != "" ]; then
        echo "  installing with $(tput bold)WebKit2GTK$(tput sgr0)"
      fi
      echo "  $(tput bold)this might take long time$(tput sgr0) ..."
    fi
  else
    if [ "$(uname)" = "Linux" ]; then
      if [ "$(which pacman 2> /dev/null)" = "" ]; then
        if [ "$(which apt-get 2> /dev/null)" = "" ]; then
          if [ "$(which yum 2> /dev/null)" = "" ]; then
            echo ''
            echo " $(tput bold)[WARNING]$(tput sgr0): dunno how to install dependencies:"
            echo '   gjs, gobject-introspection, libgirepository-1.0, libwebkit2gtk-3.0'
            echo ''
            exit 1
          else
            TODO='yum -y install gjs'
            if [ "$WEBKIT" != "" ]; then
              TODO="${TODO} webkitgtk4"
            fi
          fi
        else
          TODO='apt-get --assume-yes install gjs'
          if [ "$WEBKIT" != "" ]; then
            TODO="${TODO} libwebkit2gtk-4.0"
          fi
        fi
      else
        TODO='pacman -S --needed --noconfirm gjs'
        if [ "$WEBKIT" != "" ]; then
          TODO="${TODO} webkit2-gtk"
        fi
      fi
    else
      echo ''
      echo " $(tput bold)[WARNING]$(tput sgr0): this platform is not supported: $(uname)"
      echo ''
      exit 1
    fi
  fi
else
  if [ "$WEBKIT" != "" ]; then
    echo ''
    echo " $(tput bold)[WARNING]$(tput sgr0): gjs already installed!"
    echo '   Installing WebKit2GTK on top might cause torubles.'
    echo '   Please remove gjs first and then try again.'
    echo ''
    exit 1
  fi
fi

TODO="${TODO}
if [ '$(which yaourt 2> /dev/null)' = '' ]; then
  # unpack and install the package
  sudo -u $USER mkdir -p $pkgdir
  cd $pkgdir
  echo 'installing from $(pwd)'
  sudo -u $USER curl -sLO $source
  sudo -u $USER tar -xzvf '${pkgname}-${pkgver}.tar.gz'
  mkdir -p '${destlib}'
  cp -r '${pkgname}/jsgtk_modules' '${destlib}'
  cp '${pkgname}/${pkgname}.sh' '${destbin}'
  chmod +x '${destbin}'
  sudo -u $USER rm -rf $pkgdir

  # notify about MacPorts X11
  if [ '${macported}' = 'true' ] && [ '$X11' != '' ]; then
    echo ''
    echo '  in order to use X11 please $(tput bold)log out$(tput sgr0)'
    echo '  and then $(tput bold)log in$(tput sgr0) again'
    sleep 3
    echo '  ... anyway ...'
  fi
else
  # ArchLinux AUR users would surely prefer the AUR way
  sudo -u $USER yaourt -S --noconfirm jsgtk
fi
"

sudo sh -c "$TODO"

echo ''
echo "  $(tput bold)Thank you$(tput sgr0) for trying $(tput bold)jsgtk$(tput sgr0)!"
echo ''
echo "    Visit the following page to try some example:"
echo "    https://github.com/WebReflection/jsgtk/#gtkui-examples"
echo ''
