{ stdenv , coreutils , writeScriptBin , eject , notifyUser ? "gkleen" , showTitle ? true , ffmpeg ? null , gnused ? null , wrapperDir ? "/run/wrappers/bin" }: assert showTitle -> ffmpeg != null && gnused != null && notifyUser != null; writeScriptBin "recv-media" '' #!${stdenv.shell} pid=$? notify=${if notifyUser == null then "false" else "true"} notifyUser=${if notifyUser == null then "" else notifyUser} PATH=${wrapperDir}:${eject}/bin:${coreutils}/bin${if showTitle then '':${ffmpeg}/bin:${gnused}/bin'' else ""} exec 1> >(logger --id=$pid -t recv-media -p user.notice) exec 2> >(logger --id=$pid -t recv-media -p user.error) [[ -z "$1" || -z "$2" ]] && exit 2 dir=/var/media group=$(stat -c '%G' $dir) tmpFile="''${dir}"/.tmp/$(basename "$1") target="''${dir}"/$(basename "$1") if [[ -n "''${3}" ]]; then target="''${dir}"/$(echo ''${3} | base64 -d) fi logger --id=$pid -t recv-media -p user.debug <<EOF $(id) $(stat ''${dir}) $(stat ''${1}) $(echo ''${2}) EOF if [[ $(id -Gn) != *"$group"* ]]; then printf "Groups are ā%sā. Trying to switch primary group to ā%sā..." $(id -Gn) $group exec -- sg $group "$0 $*" fi ( if ! cp -lnv --preserve=all "$1" "''${target}"; then mkdir -pv $(dirname "''${tmpFile}") || exit 1 ionice -c 3 -t cp -vn --preserve=all "$1" "''${tmpFile}" && mv -v "''${tmpFile}" "''${target}" || exit 1 fi touch -c -m -t "$2" "''${target}" || exit 1 ) || ( if $notify; then printf "%s\n%s\n" $(basename "$1") "Failed to download" | notify-''${notifyUser} -a recv-media -u Critical fi ) if $notify; then ( summary=${if showTitle then ''$(ffmpeg -i "''${target}" -f ffmetadata pipe:1 2>/dev/null | sed -r '/^\[CHAPTER\]$/q; /^title=/!d; s/^title=//')'' else ''""''} [[ -z "''${summary}" ]] && summary=$(basename "$1") printf "%s\n%s\n" "''${summary}" "" | notify-''${notifyUser} -a recv-media || true ) || true fi ''