#!/bin/bash
#   gast_make : wrapper script for gmake command to execute with -j1 option
#
#   Copyright (C) 2009 Tadashi Koike
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
#   02110-1301, USA.
#
#----------------------------------------------------------------------
# (I am weak in English. Please modify English comment more suitable. :T.K)

VERSION=0.1.0
ORIGINAL_NAME="gast_make"
CWD=`pwd`
SCRIPT_NAME=${0##*/}
SCRIPT_PATH=${0%/*}

# check wheter perl can use or not.
HAS_PERL=`which perl 2>/dev/null`

# When this script is executed as 'gast', this script creates symbolic link
# file name as compiler alias name and exit.
if [ "${SCRIPT_NAME}" = "${ORIGINAL_NAME}" ]; then
    for arg in "$@"
    do
        case "${arg}" in
        '--version'|'-v')
            echo "${ORIGINAL_NAME} ${VERSION}"
            exit 0
            ;;
        '--help'|'-h')
            cat <<EOL
usage : ${ORIGINAL_NAME} [-v|--version] [-h|--help]

  -h, --help              display this help and exit
  -v, --version           display version information and exit

  When this script is executed as '${ORIGINAL_NAME}' with no option, this script 
  creates symbolic link toward itself which name is from make-command (e.g. 
  make/gmake).

  When this script is executed as make command, this script executes original
  command with additional -j1 option (to avoid compiling parallel) .
EOL
            exit 0
            ;;
        *)
            echo "usage : ${ORIGINAL_NAME} [--version|-v] [--help|-h]"
            exit 1
            ;;
        esac
    done

    cd "${SCRIPT_PATH}"
    for cmd in make gmake
    do
        if [ -e "${cmd}" ]; then
            file "${cmd}" 2>/dev/null | egrep 'symbolic link' >/dev/null 2>&1
            if [ "$?" -ne 0 ]; then
                echo "${ORIGINAL_NAME} : '${cmd}' exists as normal file or directory. Could not create symbolic link." >&2
                continue
            fi
        fi
        ln -sf "${ORIGINAL_NAME}" "${cmd}"
        if [ "$?" -eq 0 ]; then
            echo "${ORIGINAL_NAME} : Create symbolic link '${cmd}'" >&2
        else
            echo "${ORIGINAL_NAME} : Couldn't create symbolic link '${cmd}'" >&2
        fi
    done
    exit 0
fi

# get a directory path (full-path) of this script
case "${SCRIPT_PATH}" in
    '.')	SCRIPT_PATH="${CWD}" ;;
    \./*)	SCRIPT_PATH="${CWD}/${SCRIPT_PATH#*/}" ;;
    '~')	SCRIPT_PATH="${HOME}" ;;
    \~/*)	SCRIPT_PATH="${HOME}/${SCRIPT_PATH#*/}" ;;
    \/*)		:	;;
    *)		SCRIPT_PATH="${CWD}/${SCRIPT_PATH}" ;;
esac

# Get an original make command.
path_save="${PATH}"
path_tmp=
has_tool_path=

for dir in ${PATH//:/ }
do
    if [ "${dir}" = "${SCRIPT_PATH}" ]; then
        has_tool_path="yes"
        continue
    fi
    [ "${dir}" = '.' ] && dir="${CWD}"

    if [ -z "${path_tmp}" ]; then
        path_tmp="${dir}"
    else
        path_tmp="${path_tmp}:${dir}"
    fi
done
export PATH=${path_tmp}
unset path_tmp

MAKE_BIN=`which "${SCRIPT_NAME}" 2>/dev/null`
if [ -z "${MAKE_BIN}" ]; then
    echo "${ORIGINAL_NAME} : Couldn't find original ${SCRIPT_NAME} command. exit." >&2
    exit 1
fi

# recover PATH environment variable
if [ -n "${has_tool_path}" ]; then
    export PATH="${path_save}"
else
    export PATH="${SCRIPT_PATH}:${path_save}"
fi
unset path_save has_tool_path

# Check arguments to modify -j option
next_is_jobs_flg=
has_j_option=
idx=0
for arg in "$@"
do
    new_args[$idx]="${arg}"

    if [ -n "${next_is_jobs_flg}" ]; then
        next_is_jobs_flg=
        case "${arg}" in
            -*)	:
                ;;
            *)	new_args[$idx]="1"
                ;;
        esac
    else
        # modify -j/--jobs option
        case "${arg}" in
            '-j')	next_is_jobs_flg="yes"
                        has_j_option="yes"
                ;;
            \-j*)	new_args[$idx]="-j1"
                        has_j_option="yes"
                ;;
            \-\-jobs*)	new_args[$idx]="--jobs=1"
                        has_j_option="yes"
                ;;
        esac
    fi
    let idx+=1
done

# execute make command with -j1 option
if [ -n "${has_j_option}" ]; then
    #echo "${MAKE_BIN} ${new_args[@]}" >&2
    exec "${MAKE_BIN}" "${new_args[@]}"
else
    #echo "${MAKE_BIN} -j1 $@" >&2
    exec "${MAKE_BIN}" -j1 "$@"
fi
