#!/usr/bin/env bash
# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

#MISE description="Run trailing WS fix on all files"

set -e

if [[ "$OSTYPE" == "darwin"* ]]; then
    SED_INPLACE="sed -i ''"
else
    SED_INPLACE="sed -i"
fi

if test -f .git/HEAD ; then
    git grep -I -l -O"$SED_INPLACE \"s/[[:space:]]*$//\"" -e '' -- ':!*.patch'
else
    while IFS= read -r -d '' -u 9
    do
        if [[ "$(file -bs --mime-type -- "$REPLY")" = text/* ]]
        then
            $SED_INPLACE "s/[[:space:]]*$//" -- "$REPLY"
        fi
    done 9< <(fd -0)
fi
