#!/usr/bin/env ruby
# frozen_string_literal: true

# SPDX-License-Identifier: MIT OR Apache-2.0
#
# Copyright (c) 2018-2021 Andre Richter <andre.o.richter@gmail.com>

require_relative '../utils/devtool/copyright'

def copyright_check(staged_files)
    source_files_exts = ['.S', '.rs', '.rb']

    staged_files = staged_files.select do |f|
        next if f.include?('build.rs')
        next if f.include?('boot_test_string.rb')

        f.include?('Makefile') ||
            f.include?('Dockerfile') ||
            source_files_exts.include?(File.extname(f))
    end
    return true if staged_files.empty?

    copyright_check_files(staged_files)
end

##--------------------------------------------------------------------------------------------------
## Execution starts here
##--------------------------------------------------------------------------------------------------
staged_files = `git --no-pager diff --name-only --cached --diff-filter=d`.split(/\n/)
root_dir = `git rev-parse --show-toplevel`.strip

# Copyright must be fixed manually.
exit(1) unless copyright_check(staged_files)

# Brute-force format. Don't care if it affects non-staged files as well, since we only add back the
# staged ones.
Dir.chdir(root_dir) { system('ruby utils/devtool.rb fmt') }

staged_files.each { |f| system("git add #{f}") }
exit(0)
