#!/bin/bash
# fixboot: fixboot starter.
# Copyright (C) [2016/12], Akinori Furuta (afuruta@m7.dion.ne.jp).
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer
#    in the documentation and/or other materials provided with
#    the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
# BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
# THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES  (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

how_run_me=(`history 2 | tail -1`)

me_sourced=0
if [[ ( "X${how_run_me[1]}" == "Xsource" ) || ( "X${how_run_me[1]}" == "X." ) ]]
then
	me_sourced=1
	script_path=`echo "${how_run_me[2]}" | sed "s/^'//" | sed "s/'$//"`
fi

function FixBootExit() {
	if (( ${me_sourced} == 0 ))
	then
		exit $1
	fi
}

LsbRelease=/etc/lsb-release

if [[ -f "${LsbRelease}" ]]
then
	source "${LsbRelease}"
fi

machine=`uname -m`

live_cd="no"
if mount | grep -q -e '/cow[[:space:]]\+on[[:space:]]\+/[[:space:]]\+' \
		   -e '/dev/mapper/live-rw[[:space:]]\+on[[:space:]]\+/[[:space:]]\+'
then
	live_cd="yes"
fi

if (( ${me_sourced} == 0 ))
then
	which_path=`which "$0"`
else
	which_path="${script_path}"
fi

full_path=`readlink -f "${which_path}"`
full_dir=`dirname "${full_path}"`
mount_point=`stat -c "%m" "${full_path}"`
if (( $? != 0 ))
then
	mount_point=""
fi

echo "${0}: INFO: machine=${machine}"
echo "${0}: INFO: me_sourced=${me_sourced}"
echo "${0}: INFO: which_path=${which_path}"
echo "${0}: INFO: full_path=${full_path}"
echo "${0}: INFO: full_dir=${full_dir}"
echo "${0}: INFO: mount_point=${mount_point}"

case "${machine}" in
	(i386|i486|i586|pentium-mmx|c3|c3-2|winchip|winchip2|winchip-c6)
		echo "${0}: WARNING: It may hang or abort by SIGILL (illegal instruction), Processor is too old."
		machine=i686
	;;
	(i686|k6|athlon|k8)
		machine=i686
	;;
	(x86_64)
		# keep machine
		# do nothing.
	;;
	(*)
		echo "${0}: ERROR: Unsupported Processor."
		FixBootExit 2
	;;
esac

fixboot_exe_file="fixboot-${machine}"
fixboot_exe="${full_dir}/${fixboot_exe_file}"

echo "${fixboot_exe}: INFO: Start."
sudo "${fixboot_exe}" $*
FixBootExit 0
