#!/bin/sh
#
# Copyright (c) 2003 Thomas Klausner.
#
# 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 AUTHOR ``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 AUTHOR 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.

grep=${GREP:-grep}
zcat=${ZCAT:-zstdcat}

endofopts=0
pattern_found=0
grep_args=""

prog=${0##*/}

# handle being called 'zegrep' or 'zfgrep'
case $prog in
    *egrep*) prog=zegrep; grep_args='-E';;
    *fgrep*) prog=zfgrep; grep_args='-F';;
    *)       prog=zstdgrep;;
esac

# skip all options and pass them on to grep taking care of options
# with arguments, and if -e was supplied

escape='
  s/'\''/'\''\\'\'''\''/g
  $s/$/'\''/
'

# We might want to create a c program in the future
# and replace this file with that if these
# unsupported options become necessary
usage="Usage: $0 [OPTION]... [-e] PATTERN [FILE]...
OPTIONs are the same as grep with the exception of
the following unsupported options:
    --dereference-recursive (-R)
    --directories (-d)
    --exclude
    --exclude-from
    --exclude-dir
    --include
    --null (-Z),
    --null-data (-z)
    --recursive (-r)
grep --help below:
"

operands=
files_with_matches=0
files_without_matches=0
no_filename=0
with_filename=0

while [ "$#" -gt 0 ] && [ "${endofopts}" -eq 0 ]; do
    option=$1
    shift
    optarg=

    case $option in
        (-[0123456789EFGHIKLPRTUVZabchilnoqrsuvwxyz]?*)
            arg2=-\'$(expr "X$option" : 'X-.[0-9]*\(.*\)' | sed "$escape")
            eval "set -- $arg2 "'${1+"$@"}'
            option=$(expr "X$option" : 'X\(-.[0-9]*\)');;
        (--binary-*=* | --[lm]a*=* | --reg*=*) ;;
        (-[ABCDXdefm] | binary-* | --file | --[lm]a* | --reg*)
            case ${1?"$option option requires an argument"} in
                (*\'*) optarg=" '"$(printf '%s\n' "$1" | sed "$escape");;
                (*) optarg=" '$1'";;
            esac
            shift;;
        (-f?*\'*) optarg=" '"$(expr "X$option" : 'X-f\(.*\)' | sed "$escape"); option=-f;;
        (-f?*) optarg=" '"$(expr "X$option" : 'X-f\(.*\)')\'; option=-f;;
        (--file=*\'*) optarg=" '"$(expr "X$option" : 'X--file=\(.*\)' | sed "$escape"); option=--file;;
        (--file=*) optarg=" '"$(expr "X$option" : 'X--file=\(.*\)')\'; option=--file;;
        (--) endofopts=1; break;;
        (-?*) ;;
        (*)
            case $option in
                (*\'*) operands="$operands '"$(printf '%s\n' "$option" | sed "$escape");;
                (*) operands="$operands '$option'";;
            esac
            ${POSIXLY_CORRECT+break}
            endofopts=1
            continue;;
        esac

        case $option in
            (-[drRzZ] | --di* | --exc* | --inc* | --rec* | --nu*)
                printf >&2 '%s: %s: option not supported\n' "$0" "$option"
                exit 2;;
            (-e* | --reg*) pattern_found=1;;
            (-f | --file)
                case $optarg in
                    (" '-'" | " '/dev/stdin'" | " '/dev/fd/0'")
                        option=-e
                        optarg=" '"$(sed "$escape") || exit 2;;
                esac
                pattern_found=1;;
            (--h | --he | --hel | --help) echo "$usage"; eval "$grep --help" || exit 2; exit;;
            (-H | --wi | --wit | --with | --with- | --with-f | --with-fi \
            | --with-fil | --with-file | --with-filen | --with-filena | --with-filenam \
            | --with-filename)
                with_filename=1
                continue;;
            (-l | --files-with-*) files_with_matches=1;;
            (-L | --files-witho*) files_without_matches=1;;
            (-h | --no-f*) no_filename=1;;
        esac

        case $option in
            (*\'?*) option=\'$(printf '%s\n' "$option" | sed "$escape");;
            (*) option="'$option'";;
        esac

        grep_args="$option$optarg"
        grep="$grep $grep_args"
    done

eval "set -- $operands "'${1+"$@"}'

if test $pattern_found -eq 0; then
    case ${1?"missing pattern; try \`$0 --help' for help"} in
        (*\'*) grep="$grep -- '"$(printf '%s\n' "$1" | sed "$escape");;
        (*) grep="$grep -- '$1'";;
    esac
    shift
fi

if test $# -eq 0; then
    set -- -
fi

exec 3>&1
res=0

for i do
    zcat_status=$(
        exec 5>&1
        ($zcat -- "$i" 5>&-; echo $? >&5) 3>&- |
        if test $files_with_matches -eq 1; then
            eval "$grep" >/dev/null && { printf '%s\n' "$i" || exit 2; }
        elif test $files_without_matches -eq 1; then
            eval "$grep" >/dev/null || {
                r=$?
                if test $r -eq 1; then
                printf '%s\n' "$i" || r=2
                fi
                exit $r
            }
        elif test $with_filename -eq 0 && { test $# -eq 1 || test $no_filename -eq 1; }; then
            eval "$grep"
        else
            case $i in
            (*'
        '* | *'&'* | *'\'* | *'|'*)
                i=$(printf '%s\n' "$i" |
                    sed '
                    $!N
                    $s/[&\|]/\\&/g
                    $s/\n/\\n/g
                    ');;
            esac
            sed_script="s|^|$i:|"

            # Fail if grep or sed fails.
            r=$(
                exec 4>&1
                (eval "$grep" 4>&-; echo $? >&4) 3>&- | sed "$sed_script" >&3 4>&-
            ) && exit $r
            r=$?
            test 1 -lt $r && exit $r || exit 2
        fi >&3 5>&-
    )
    r=$?
    test 128 -lt $r && exit $r
    test "$zcat_status" -eq 0 || test "$zcat_status" -eq 2 || r=2
    test $res -lt $r && res=$r
done
exit $res
