#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later

## @file
## @brief Symlink grouped files
##
## It uses geeqie remote connection to get details about grouped files
##

group_list=$(mktemp "${TMPDIR:-/tmp}/geeqie-symlink.XXXXXXXXXX") || exit 1
trap 'rm -f "$group_list"' 0
trap 'exit 1' HUP INT TERM

for file in "$@"
do
	# Capture the query separately so a failed producer cannot look successful.
	if ! geeqie --get-sidecars="$file" > "$group_list"
	then
		printf 'Cannot read grouped files for: %s\n' "$file" >&2
		exit 1
	fi
	if [ ! -s "$group_list" ]
	then
		printf 'No grouped files returned for: %s\n' "$file" >&2
		exit 1
	fi
	while IFS= read -r sidecar || [ -n "$sidecar" ]
	do
		if [ -z "$sidecar" ]
		then
			printf 'Empty grouped filename returned for: %s\n' "$file" >&2
			exit 1
		fi
		if ! destination=$(geeqie --get-destination="$sidecar")
		then
			printf 'Cannot read symlink destination for: %s\n' "$sidecar" >&2
			exit 1
		fi
		if [ -z "$destination" ]
		then
			printf 'No symlink destination returned for: %s\n' "$sidecar" >&2
			exit 1
		fi
		if [ -e "$destination" ] || [ -L "$destination" ]
		then
			printf 'Symlink destination already exists: %s\n' "$destination" >&2
			exit 1
		fi
		if ! ln -s "$sidecar" "$destination"
		then
			printf 'Cannot create symlink: %s -> %s\n' "$destination" "$sidecar" >&2
			exit 1
		fi
	done < "$group_list"
done
