#! /bin/bash

# Parabola GNU/linux-libre helper script to set/unset/check/fix the enabled Java environment
# This program may be freely redistributed under the terms of the GNU General Public License
#
# Author: Guillaume ALAUX <guillaume@archlinux.org>
# Parabola Rebranding Author: André Silva <emulatorman@parabola.nu>

JVM_DIR=/usr/lib/jvm
DEFAULT_NAME=default
DEFAULT_PATH=${JVM_DIR}/${DEFAULT_NAME}
DEFAULT_NAME_JRE=default-runtime
DEFAULT_PATH_JRE=${JVM_DIR}/${DEFAULT_NAME_JRE}


# Utility functions

check_root() {
  if [ $(id -u) -ne 0 ]; then
    echo 'This script must be run as root'
    exit 1
  fi
}

# $1: parameter count given to this script for this option
# $2: expected parameter count for this option
check_param_count() {
  if [ $1 -ne $2 ]; then
    echo 'Wrong parameter count'
    exit 2
  fi
}

# Second level functions

get_default_java() {
  path=$(readlink -e ${DEFAULT_PATH})
  if [ "x${path}" != "x/dev/null" ]; then
    echo ${path/${JVM_DIR}\/}
  else
    echo ""
  fi
}

get_installed_javas() {
  if [ -d ${JVM_DIR} ]; then
    for dir in $(find ${JVM_DIR} -mindepth 1 -maxdepth 1 -type d | sort); do
      if [ -x ${dir}/bin/javac ]; then
        javas+=(${dir/${JVM_DIR}\/})
      else
        if [ -x ${dir}/jre/bin/java ]; then
        javas+=(${dir/${JVM_DIR}\/}/jre)
        fi
      fi
    done
  fi
  echo ${javas[@]}
}

# $1: Java environment name to test
is_java_valid() {
  test "x$1" != "x${DEFAULT_NAME}" && test -x ${JVM_DIR}/$1/bin/java
}

# $1: Java environment name to set as default
set_default_link_to() {
  new_default=$1
  unlink ${DEFAULT_PATH} 2>/dev/null
  ln -sf ${new_default} ${DEFAULT_PATH}

  unlink ${DEFAULT_PATH_JRE} 2>/dev/null
  if [[ ${new_default} == */jre ]]; then
    ln -sf ${new_default} ${DEFAULT_PATH_JRE}
  else
    ln -sf ${new_default}/jre ${DEFAULT_PATH_JRE}
  fi
}

unset_default_link() {
  unlink ${DEFAULT_PATH} 2>/dev/null
  unlink ${DEFAULT_PATH_JRE} 2>/dev/null
}

# First level functions

do_status() {
  installed_java=($(get_installed_javas))
  if [ ${#installed_java[@]} -eq 0 ]; then
    echo 'No compatible Java environment installed'
  else
    default_java=$(get_default_java)
    echo 'Available Java environments:'
    for java in ${installed_java[@]}; do
      if [ "${java}/jre" = "${default_java}" ]; then
        echo -e "  ${java} (${java}/jre default)"
      elif [ ${java} = "${default_java}" ]; then
        echo -e "  ${java} (default)"
      else
        echo "  ${java}"
      fi
    done
    if [ -z ${default_java} ]; then
      echo "No Java environment set as default"
    fi
  fi
}

do_get() {
  get_default_java
}

# $1: Java environment name to set as default
do_set() {
  if ! is_java_valid $1; then
    echo "'${JVM_DIR}/$1' is not a valid Java environment path"
    exit 1
  fi
  default=$(get_default_java)
  if [ "x$1" != "x${default}" ] || ! is_java_valid ${default}; then
    unset_default_link
    set_default_link_to $1
  fi

  #parent_dir=$(dirname $1)
  #if is_java_valid ${parent_dir}; then
  #  echo "Warning: '${parent_dir}' looks like a valid JDK whereas '$1' is set as default"
  #  echo "Fix this with 'parabola-java set ${parent_dir}'"
  #fi
}

# $1: Java environment name to unset
do_unset() {
  unset_default_link
}

do_fix() {
  default=$(get_default_java)
  if is_java_valid ${default}; then
    if is_java_valid $(dirname ${default}); then
      unset_default_link
      set_default_link_to $(dirname ${default})
    fi
  else
    prev=$(readlink ${DEFAULT_PATH})
    unset_default_link
    prev_fix=("${prev/\/jre}" "${prev}/jre")
    openjdk7=('java-7-openjdk' 'java-7-openjdk/jre')
    to_check=(${prev_fix[@]} ${openjdk7[@]} $(get_installed_javas))
    for java in ${to_check[@]}; do
      if ! is_java_valid $(get_default_java) && is_java_valid ${java}; then
        set_default_link_to ${java}
      fi
    done
  fi
  if ! is_java_valid $(get_default_java); then
    echo 'No valid Java environment found'
  fi
}

usage() {
  echo "$(basename $0) <COMMAND>"
  echo -e "\nCOMMAND:"
  echo -e '\tstatus\t\tList installed Java environments and enabled one'
  echo -e '\tget\t\tReturn the short name of the Java environment set as default'
  echo -e '\tset <JAVA_ENV>\tForce <JAVA_ENV> as default'
  echo -e '\tunset\t\tUnset current default Java environment'
  echo -e '\tfix\t\tFix an invalid/broken default Java environment configuration'
}

## Main
case $1 in
  'status') do_status;;
  'get')    do_get;;
  'set')    check_root; check_param_count $# 2; do_set $2;;
  'unset')  check_root; do_unset;;
  'fix')    check_root; do_fix;;
  'help' | '--help' | '-h' | '') usage;;
  *)           echo "$(basename $0): unknown option '$@'"; exit 1;;
esac