#!/bin/bash

# Copyright (C) 2017 by Mike Gabriel <mike.gabriel@das-netzwerkteam.de>
#
# This 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 3 of the License, or
# (at your option) any later version.
#
# This 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 St, Fifth Floor, Boston, MA 02110-1301, USA.

d=. ; while [ ! -d "$d/nx-X11/extras" ] && [ "$(readlink -e "$d")" != "/" ]; do d="$d/.."; done
if [ -d "$d/nx-X11/extras" ] && [ -z "$QUILT_PATCHES" ]; then
    # if in nx-libs tree with unset $QUILT_PATCHES
    export QUILT_PATCHES="../Mesa.patches"
    export QUILT_PATCH_OPTS="--reject-format=unified"
    export QUILT_DIFF_ARGS="-p ab --no-timestamps --no-index --color=auto"
    export QUILT_REFRESH_ARGS="-p ab --no-timestamps --no-index"
    export QUILT_COLORS="diff_hdr=1;32:diff_add=1;34:diff_rem=1;31:diff_hunk=1;33:diff_ctx=35:diff_cctx=33"
    if ! [ -d "$d/nx-X11/extras/Mesa.patches" ]; then mkdir "$d/nx-X11/extras/Mesa.patches"; fi
    cd "$d/nx-X11/extras/Mesa/"
fi

quilt "$@"

cd - 1> /dev/null