#!/bin/sh

	left=$1;
	right=$2;
	dst=$4;
	if [ -z "${left}" ] || [ -z "${right}" ] || [ -z "${dst}" ]; then
		exit 1;
	fi

	if [ ! -e "${left}" ] || [ ! -e "${right}" ]; then
		[ -e "${left}" ] && /bin/rm -f "${left}" > /dev/null 2>&1
		[ -e "${right}" ] && /bin/rm -f "${right}" > /dev/null 2>&1

		exit 2;
	fi

	# Cilovy adresar - existuje
	dstdir=$( /usr/bin/dirname "${dst}" 2>/dev/null )
	if [ ! -d "${dstdir}" ]; then
		/bin/mkdir -p "${dstdir}" -m 777 > /dev/null 2>&1
	fi

	# LEVY + PRAVY do jednoho sterea
	/bin/nice -n 10 /usr/local/bin/sox "${left}" "${right}" --channels 2 --combine merge "${dst}" > /dev/null 2>&1

	/bin/rm -f "${left}" "${right}" > /dev/null 2>&1

	/bin/chmod 0644 "${dst}" > /dev/null 2>&1

	exit 0;
