#!/bin/bash
#   gast_rpmbuild : wrapper script for rpmbuild command to set environment using gast
#
#   Copyright (C) 2009-2010 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.3
ORIGINAL_NAME="gast_rpmbuild"
CWD=`pwd`
SCRIPT_NAME=${0##*/}
if [[ "$0" =~ ".+/[^/]+" ]]; then
    SCRIPT_PATH=${0%/*}
else
    SCRIPT_PATH=${0%${SCRIPT_NAME}}
fi
# get a directory path (full-path) of directory 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

# When this script is executed as 'gast_rpmbuild', this script creates symbolic link
# file name as 'rpmbuild' 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 
  display usage.

  When this script is executed as 'rpmbuild', this script behaves just as
  rpmbuild command with accepting additional '--save-temps-dir' option and
  with setting GAST_* environment variables.
EOL
            exit 0
            ;;
        *)
            echo "usage : ${ORIGINAL_NAME} [--version|-v] [--help|-h]"
            exit 1
            ;;
        esac
    done

    cd "${SCRIPT_PATH}"
    for cmd in rpmbuild
    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 full-path name of original rpmbuild/rpm2cpio command.
RPMBUILD_BIN=`which rpmbuild 2>/dev/null`
if [ -z "${RPMBUILD_BIN}" ]; then
    echo "${ORIGINAL_NAME} : Couldn't find a path to rpmbuild command. exit" >&2
    exit 1
fi

# Set this script's directory to PATH environment variable.
# (to be found a gcc wrapper script (=gast) in advance of original compiler binary).
PATH="${SCRIPT_PATH}:${PATH}"
export PATH

# Set environment variable to get compiler's temporalry file
export GAST_ENABLE=

# Analyse command options. Get directory name to move temporary file.
next_is_savedir=
idx=0
# new_args_a : argument list for rpmbuild {-bb|-bc}
# new_args_b : argument list for rpmbuild {--recompile|--rebuild}
# srpm_list  : list of srpm file(s)
for arg in "$@"
do
    if [ "${arg}" = "--save-temps-dir" ]; then
        next_is_savedir="yes"
    elif [ -n "${next_is_savedir}" ]; then
        next_is_savedir=
        case "${arg}" in
            \-*)	GAST_SAVE_DIR=""
                        echo "${ORIGINAL_NAME} : Error in --save-temps-dir option. ignore this option." >&2
                        new_args[$idx]="${arg}"
                        let idx+=1
                        ;;
            '')		GAST_SAVE_DIR="" ;;
            '.')	GAST_SAVE_DIR="${CWD}" ;;
            \./*)	GAST_SAVE_DIR="${CWD}/${arg#*/}" ;;
            '~')	GAST_SAVE_DIR="${HOME}" ;;
            \~/*)	GAST_SAVE_DIR="${HOME}/${arg#*/}" ;;
            \/*)	GAST_SAVE_DIR="${arg}" ;;
            *)		GAST_SAVE_DIR="${CWD}/${arg}" ;;
        esac

        if [ -n "${GAST_SAVE_DIR}" ]; then
            GAST_SAVE_DIR="${GAST_SAVE_DIR%/}"
            export GAST_SAVE_DIR
        else
            unset GAST_SAVE_DIR
        fi
    else
        new_args[$idx]="${arg}"
        let idx+=1
    fi
done
exec "${RPMBUILD_BIN}" "${new_args[@]}"
