#!/usr/bin/env bash

# Copyright (C) 2020  Matthew "strager" Glazar
# See end of file for extended copyright information.

set -e
set -o pipefail
set -u

cd "$(dirname "${0}")/.."
source tools/files.sh

check_file_copyright_header() {
  local file="${1}"
  case "${file}" in
    benchmark/benchmark-lsp/corpus/*) ;;
    website/public/license/*.gif.html) ;;
    website/public/license/*.png.html) ;;
    website/public/license/*.svg.html) ;;
    website/public/license/CC-*.html) ;;
    po/en_US@snarky.po) ;;
    dist/winget/*.yaml) check_file_copyright_header_winget_manifests "${file}" ;;
    *) check_file_copyright_header_quick_lint_js "${file}" ;;
  esac
}

# quick-lint-js is GPLv3-licensed.
check_file_copyright_header_quick_lint_js() {
  local file="${1}"
  fgrep -q 'Copyright (C)' "${file}" && \
    fgrep -q 'This file is part of quick-lint-js.' "${file}" && \
    fgrep -q 'quick-lint-js finds bugs in JavaScript programs.' "${file}" && \
    fgrep -q 'quick-lint-js is free software' "${file}"
}

# quick-lint-js's winget manifests are under the public
# domain (CC0).
check_file_copyright_header_winget_manifests() {
  local file="${1}"
  fgrep -q 'waived all copyright' "${file}" && \
    fgrep -q 'quick-lint-js winget manifests' "${file}"
}

error=0
for file in $(find_non_vendor_files | matching '(\<CMakeLists\.txt|\.bt|\.c|\.cjs|\.cmake|\.cpp|\.go|\.h|\.hs|\.html|\.js|\.lua|\.mjs|\.nix|\.po|\.pot|\.py|\.sh|\.vim|\.yaml|\.yml)$'); do
  if ! check_file_copyright_header "${file}"; then
    printf 'error: file missing copyright: %s\n' "${file}"
    error=1
  fi
done
exit "${error}"

# quick-lint-js finds bugs in JavaScript programs.
# Copyright (C) 2020  Matthew "strager" Glazar
#
# This file is part of quick-lint-js.
#
# quick-lint-js 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.
#
# quick-lint-js 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 quick-lint-js.  If not, see <https://www.gnu.org/licenses/>.
