#!/usr/bin/env bash

# Copyright (C) 2016 Paul Kocialkowski <contact@paulk.fr>
#
# This program 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 program 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, see <http://www.gnu.org/licenses/>.

arguments() {
	local project=$1

	if [ -z "$project" ]
	then
		ls -A "$root/$PROJECTS/" | while read project
		do
			printf '%s\n' "$project"
		done
	else
		shift
		project_action_helper "arguments" "$project" "$@"
	fi
}

usage() {
	tool_usage_actions "$tool" "verify"
	tool_usage_arguments "$tool" "$@"
}

update() {
	local tool_path=$( tool_path "$tool" )
	local sources_path=$( tool_sources_path "$tool" )
	local deblob_check_path="$sources_path/deblob-check"
	local patches_path="$tool_path/$PATCHES/$WILDDOTPATCH"

	wget "https://www.fsfla.org/svn/fsfla/software/linux-libre/scripts/deblob-check" -O "$deblob_check_path"
	perl -0777 -i -pe "s/  blobna \(\) \{[^\}]*\}/  blobna () \{\n    true\n  \}/" "$deblob_check_path"
	perl -0777 -i -pe "s/  blobname \(\) \{[^\}]*\}/  blobname () \{\n    true\n  \}/" "$deblob_check_path"
	chmod a+x "$deblob_check_path"

	cd "$sources_path"

	path_wildcard_expand "$patches_path" | while read patch_path
	do
		if ! [ -f "$patch_path" ]
		then
			continue
		fi

		patch -p1 < "$patch_path"
	done
}

verify() {
	local project=$1
	shift

	local project_path=$( project_path "$project" )
	local project_sources_path=$( project_sources_path "$project" "$repository" "$@" )
	local project_blobs_path=$( project_blobs_path "$project" "$@" )
	local project_blobs_ignore_path=$( project_blobs_ignore_path "$project" "$@" )
	local project_blobs_discover_path

	local ifs_save
	local file
	local choice

	requirements "sort"

	if ! [ -z "$project_blobs_path" ]
	then
		project_blobs_discover_path="$( dirname $project_blobs_path )/$BLOBS_DISCOVER"
	else
		project_blobs_discover_path="$project_path/$CONFIGS/$BLOBS_DISCOVER"
	fi

	file_exists_check "$project_blobs_discover_path"

	# This it to allow space characters in arguments.
	ifs_save=$IFS
	IFS=$'\n'

	for file in $( cat "$project_blobs_discover_path" )
	do
		(
			IFS=$ifs_save

			printf '\n%s\n' "Discovered blob: $file"

			$EDITOR "$project_sources_path/$file"

			printf '%s' "Type \"blob\" to keep or press enter to discard: "
			read choice

			if [ "$choice" = "blob" ]
			then
				printf '%s\n' "$file" >> "$project_blobs_path"
			else
				printf '%s\n' "$file" >> "$project_blobs_ignore_path"
			fi

			sed "\|^$file$|d" -i "$project_blobs_discover_path"
		) < /dev/tty
	done

	IFS=$ifs_save

	rm "$project_blobs_discover_path"

	sort -o "$project_blobs_path" "$project_blobs_path"
	sort -o "$project_blobs_ignore_path" "$project_blobs_ignore_path"
}

execute() {
	local project=$1
	shift

	local repository=$project
	local repository_path=$( git_project_repository_path "$repository" )

	# This assumes that the repository is the project, which is not always the case.
	project_sources_directory_missing_empty_error "$project" "$repository"

	local sources_path=$( tool_sources_path "$tool" )
	local project_path=$( project_path "$project" )
	local project_sources_path=$( project_sources_path "$project" "$repository" "$@" )
	local project_blobs_path=$( project_blobs_path "$project" "$@" )
	local project_blobs_ignore_path=$( project_blobs_ignore_path "$project" "$@" )
	local project_blobs_directory_path

	if ! [ -z "$project_blobs_path" ]
	then
		project_blobs_discover_path="$( dirname $project_blobs_path )/$BLOBS_DISCOVER"
	else
		project_blobs_discover_path="$project_path/$CONFIGS/$BLOBS_DISCOVER"
	fi

	local deblob_check_path="$sources_path/deblob-check"

	if git_project_check "$repository"
	then
		git_project_checkout "$project" "$repository" "$@"
		git_clean "$repository_path"
	fi

	printf '\n%s\n' 'Discovering new blobs, this may take a while...'

	files=$( find "$project_sources_path" -type f | grep -vP "\.git/|\.tar|\.patch" )

	touch "$project_blobs_discover_path"

	printf '%s\n' "$files" | while read file
	do
		if ! [ -z "$project_blobs_ignore_path" ]
		then
			match=$( grep "$file" "$project_blobs_ignore_path" || true)
			if [ ! -z "$match" ]
			then
				continue
			fi
		fi

		match=$( "$deblob_check_path" -l -i "" "$file" || true )
		if ! [ -z "$match" ]
		then
			printf '%s\n' "$match" >> "$project_blobs_discover_path"
		fi
	done

	printf '\n%s\n' "Discovered blobs were stored in $project_blobs_discover_path"
}

execute_check() {
	local project=$1
	shift

	local project_path=$( project_path "$project" )
	local project_blobs_path=$( project_blobs_path "$project" "$@" )
	local project_blobs_ignore_path=$( project_blobs_ignore_path "$project" "$@" )
	local project_blobs_discover_path

	if ! [ -z "$project_blobs_path" ]
	then
		project_blobs_discover_path="$( dirname $project_blobs_path )/$BLOBS_DISCOVER"
	else
		project_blobs_discover_path="$project_path/$CONFIGS/$BLOBS_DISCOVER"
	fi

	file_exists_check "$project_blobs_discover_path"
}
