#!/bin/bash
source /sbin/functions.sh
# Used to update the global result.
update_result() {
local new_result=${1}
# Only update if result was good so far
[[ ${RESULT} == 0 ]] && RESULT=${new_result}
}
#
# some wrapper functions, so that we don't always print the output
#
# quiet echo
qecho() {
[[ -z ${QUIET} ]] && echo ${@}
}
# quiet einfo
qeinfo() {
[[ -z ${QUIET} ]] && einfo ${@}
}
# quiet ewarn
qewarn() {
[[ -z ${QUIET} ]] && ewarn ${@}
}
# quiet eerror
qeerror() {
[[ -z ${QUIET} ]] && eerror ${@}
}
# quiet ebegin
qebegin() {
[[ -z ${QUIET} ]] && ebegin ${@}
}
# quiet eend
qeend() {
[[ -z ${QUIET} ]] && eend ${@}
}
# Here are our checks. The names should prefixed with 'check_'
# Check environment files in /etc/env.d/java:
# Generation-2 have GENERATION="2" in them.
# Generation-1 don't have anything.
check_vm_environment_files() {
local this_result=0
local packages_to_merge old_configs
# We need to unset GENERATION, because this ends up being in
# the environment.
local OLD_GENERATION=${GENERATION}
unset GENERATION
# Check /etc/env.d/java for old env files
for env in $(ls /etc/env.d/java/20* 2>/dev/null); do
generation=$(source ${env}; echo $GENERATION)
version=$(source ${env}; echo $VERSION)
handle=${env#*20}
if [[ "${generation}z" != "2z" ]]; then
local ATOM
if [[ ${handle} =~ "^kaffe" ]]; then
ATOM="kaffe"
elif [[ ${handle} =~ "^(\w+-)+[0-9]+\.[0-9]+" ]]; then
ATOM="=${BASH_REMATCH}*"
fi
packages_to_merge="${packages_to_merge} ${ATOM}"
old_configs="${old_configs} ${env}"
this_result=1
fi
done
if [[ -n ${old_configs} ]]; then
qeerror "Generation-1 configuration files were found in /etc/env.d/java"
qeerror "Please run the following to remove them:"
qecho "# rm ${old_configs}"
qecho
qeerror "You must then update your VMs to Generation-2 by running:"
qecho "# emerge -atv ${packages_to_merge}"
qecho
fi
GENERATION=${OLD_GENERATION}
update_result ${this_result}
return ${this_result}
}
# Checks environment files in current user's home directory:
# For Generation-1, these files sat in ~/.gentoo
check_user_settings() {
local this_result=0
# Check .gentoo/ for old env files for non-root users
if [[ ${UID} != 0 ]]; then
# this equates to java, java.csh, java-env, java-env.csh
local user_configs=$(ls ~/.gentoo/java{,.csh,-env{,.csh}} 2>/dev/null)
if [[ -n ${user_configs} ]]; then
qeerror "Generation-1 configurations found in ~/.gentoo"
qeerror "Please run the following to remove them:"
qecho \$ rm ${user_configs}
qecho
qewarn "You may also need update your ~/.bash_profile "
qewarn "or ~/.bashrc if they reference these file"
qewarn "If you choose, you can select a new user VM."
qewarn "Otherwise, the system-vm will be used."
qewarn "Run 'java-config -L' to see a list of available VMs"
qewarn "Then run 'java-config -s <VM choice>'"
this_result=1
fi
else
qeinfo "Running as root. Don't need to check user settings."
fi
update_result ${this_result}
return ${this_result}
}
# Check if the system-vm for generation-1 is 1.5
# Having a 1.5 system-vm causes much pain and anguish.
check_generation_1_system_15_vm() {
local this_result=$(check_generation_1_system_vm)
if [[ ${this_result} != 0 ]]; then
return ${this_result}
fi
# TODO make this check slightly better. It would match kaffe-1.1.5 for example.
if grep 1.5 /etc/env.d/20java >/dev/null 1>/dev/null; then
qecho
qeerror "The System VM set to a 1.5 JDK for Generation-1"
qeerror "Run 'java-config-1 -L' to see a list of available VMs"
qeerror "Then run 'java-config-1 -S <VM choice>'"
qecho
qeerror "There is a chance that your system has packages targetted for 1.5."
qeerror "This is a bad thing. To fix this, try running the following:"
# TODO should point this to migration-overlay probably
portdir=$(source /etc/make.conf; echo ${PORTDIR})
portdir=${portdir:="/usr/portage"}
qecho "# ${portdir}/dev-java/java-config-wrapper/files/java-1.5-fixer"
this_result=1
fi
update_result ${this_result}
return ${this_result}
}
# Checks that a system-vm for generation-1 is set
check_generation_1_system_vm() {
local this_result=0
# Make sure a generation-1 VM is set
if [[ ! -f /etc/env.d/20java ]]; then
qeerror "No Generation-1 System VM is set!"
qeerror "Run 'java-config-1 -L' to see a list of available VMs"
qeerror "Then run 'java-config-1 -S <VM choice>'"
this_result=1
elif [[ -z $(java-config-1 -f) ]]; then
qeerror "No Generation-1 System VM is set!"
qeerror "Run 'java-config-1 -L' to see a list of available VMs"
qeerror "Then run 'java-config-1 -S <VM choice>'"
this_result=1
fi
update_result ${this_result}
return ${this_result}
}
#if [[ ${UID} != 0 ]]; then
# eerror "Must be root to run"
# exit 1
#fi
qeinfo "=== Java Environment Checker ==="
qeinfo "The purpose of this script is to check the sanity of your Java Environment."
qeinfo "We have significantly changed and improved the way Java is handled in many respects."
qeinfo "Please refer to https://projects.gentooexperimental.org/expj/wiki/ for details"
qecho
RESULT=0
# Get the list of checks to perform from the command line
checks=$@
# or use our default
checks=${checks:="vm_environment_files user_settings generation_1_system_vm"}
for check in ${checks}; do
if [ "$(type -t check_${checks})" == "function" ]; then
qebegin "Checking ${check}"
check_${check}
result=$?
qeend ${result}
else
eerror "Could not find function 'check_${check}'"
exit 1
fi
if [[ ${result} != 0 ]]; then
qeerror "Some problems were found. Please follow the instructions above, and rerun java-check-environment"
exit ${result}
fi
done
if [[ ${RESULT} == 0 ]]; then
qeinfo "Java environment is sane. Congratulations!"
#touch "/etc/java-config-2/environment-ok"
fi
exit ${RESULT}