#!/bin/sh
remove_line() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	# path to file first, lines as a whitespace delimited string second
	for l in $2; do
		sed -i "${l}d" "$1"
		[ -z "$3" ] && return 0
		# While the line we get is not empty, remove it, this serves to
		# remove any follow-up lines after we remove the first one.
		# We certainly don't want stray empty lines
		while ! sed -n "${l}p" "$1" | grep -q '.'; do
			sed -i "${l}d" "$1"
		done
	done
}

remove_return_1() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	# path to file first, lines as a whitespace delimited string second
	for l in $2; do
		prev_line=$(( l - 1 )) # The line before the one we should delete

		# Check if the line we were given contains only || return 1
		if sed -n "${l}p" "$1" | grep -q '^[[:blank:]]*|| return 1[[:blank:]]*$'; then
			# If that is true then delete the line outright, no point in keeping
			# an empty line around
			sed -i "${l}d" "$1"

			# Remove backslash of previous line, this is to deal with cases
			# where return is in a line of its own and the command is in the
			# previous line, commonly used in autoconf scripts, as an example:
			# autoconf \
			#	--prefix=/usr \
			#	|| return 1
			sed -i "${prev_line}s/\\\\$//g" "$1"
		else
			# Remove the || return 1 from the specific line
			sed -i "${l}s/|| return 1//g" "$1"
		fi

	done
}

# Rename _builddir to builddir, both the variable in the global scope
# and any other occurrences
rename__builddir() {
	sed -i 's/_builddir/builddir/g' "$1"
}

# Self-explanatory, remove any whitespace after ending text
remove_trailing_whitespace() {
	sed -i 's/[[:space:]]\+$//' "$1"
}

# Given a file $1 and lines $2, rewrite the function which is composed of
# letters and numbers
rewrite_function() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	for l in $2; do
		sed -ri " {$l s|^([A-Za-z0-9]*).*|\\1() {|g }" "$1"
	done
}

# Remove braces from a variable in a line
remove_braces() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	printf "%s\\n" "$2" | while read -r line variable; do
		# Make variable without braces
		unbraced_variable="$(printf "%s\\n" "$variable" | sed 's|[{}]||g')"
		sed -i "{$line s|$variable|$unbraced_variable|g }" "$1"
	done
}

remove_pkgname_from_source() {
	[ -z "$1" ] || [ -z "$2" ] && return 0
	for l in $2; do
		sed -i "{$l s|\$pkgname|$pkgname|g }" "$1"
	done
}

for apkbuild; do
	if [ -f "$apkbuild" ]; then

	# Source the apkbuild, required to get pkgname
	srcdir="" . "$apkbuild" || {
		echo "Failed to source APKBUILD in '$apkbuild'" ;
		exit 1;
	}

	# Remove trailing whitespace
	remove_trailing_whitespace "$apkbuild"
	
	# Before running apkbuild-lint, perform rename on builddir
	grep -q "^_builddir=" "$apkbuild" && rename__builddir "$apkbuild"
	
	# Get all the violations
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Rewrite functions (no chance of deleting line)
	rewrite_functions="$(echo "$violations" | \
						 grep -E '\[AL(10|11)\]'| \
						 cut -d : -f4 | \
						 sort -rV | \
						 paste -sd ' ')"

	remove_braces="$(echo "$violations" | \
					 grep -F '[AL32]' | \
					 awk -F: '{print $4" "$6}' | \
					 sort -rV)"
	
	[ -n "$rewrite_functions" ] && rewrite_function "$apkbuild" "$rewrite_functions"
	[ -n "$remove_braces" ] && remove_braces "$apkbuild" "$remove_braces"
	
	# Remove || return 1 (might delete lines)
	return_1_lines="$(echo "$violations" | \
					  grep -F '[AL2]' | \
					  cut -d : -f4 | \
					  sort -rV | \
					  paste -sd ' ')"
	
	[ -n "$return_1_lines" ] && remove_return_1 "$apkbuild" "$return_1_lines"
	
	# Get all violations again now that lines were deleted and it can potentially cause lines
	# to be misdeleted
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Remove lines with empty variables, greedily (will certainly remove lines)
	removable_lines="$(echo "$violations" | \
					   grep -E '\[AL(5|13)\]'| \
					   cut -d : -f4 | \
					   sort -rV | \
					   paste -sd ' ')"
	
	# Third argument enables greedyness
	[ -n "$removable_lines" ] && remove_line "$apkbuild" "$removable_lines" greedy
	
	# Get all violations again now that lines were deleted and it can potentially cause lines
	# to be misdeleted
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Remove lines with default builddir (will certainly remove lines)
	removable_lines="$(echo "$violations" | \
					   grep '\[AL[1]\]'| \
					   cut -d : -f4 | \
					   sort -rV | \
					   paste -sd ' ')"
	
	[ -n "$removable_lines" ] && remove_line "$apkbuild" "$removable_lines"
	
	# Get all violations again now that lines were deleted and it can potentially cause lines
	# to be misdeleted
	violations="$(apkbuild-lint "$apkbuild")"
	[ -z "$violations" ] && return 0
	
	# Rewrite functions
	rewrite_functions="$(echo "$violations" | \
						 grep -E '\[AL(10|11)\]'| \
						 cut -d : -f4 | \
						 sort -rV | \
						 paste -sd ' ')"

	[ -n "$rewrite_functions" ] && rewrite_function "$apkbuild" "$rewrite_functions"

	# Replace $pkgname
	pkgname_in_source="$(echo "$violations" | \
						 grep '\[AL29\]' | \
						 cut -d : -f4 | \
						 sort -rV | \
						 paste -sd ' ')"

	[ -n "$pkgname_in_source" ] && remove_pkgname_from_source "$apkbuild" "$pkgname_in_source"

	fi
done
