#!/bin/sh
#
# This script is a patch to the ifp commandline utility plugin for
# MC (Midnight Commander). When transferring voice recordings from
# the ifp player the *.REC files are not found by MC because the ifp
# command silently renames them to *.mp3 when it has transferred the
# file. This script reverses this behaviour so that *.REC can be
# copied individually using MC.
#
# To use, execute the following commands as root:
#   cp this-script.sh /usr/share/mc/extfs/ifp
#   chmod +x /usr/share/mc/extfs/ifp
#   echo ifp >> /usr/share/mc/extfs/extfs.ini
#
# Make sure that the /usr/bin/ifp is on your system and that is it
# working. You may need to do as root:
#   chown -R your-username /proc/bus/usb
# after you've plugged the media player into the usb port.
#
# Written by Erik-Jan Taal (ejtaal.net / ejtaal2gmail>com)
#
# Update: It may be that this patch may not be necessary to use
# anymore due to the availability of UMS firmware for your player.
# Check the iriver website for details.

# Enable the following command to see what mc is trying to do
#echo "$(date) - mc-ifp arguments: 1: [$1] 2: [$2] 3: [$3] 4: [$4] 5:[$5]" >> /tmp/mc-ifp.log

/usr/bin/ifp "$@"

if [ "$1" = "copyout" ]; then
  FILE_EXTENSION=${3##*.}
  if [ "$FILE_EXTENSION" = "REC" ]; then
    OUTPUT_DIRNAME="$(dirname "$4")"
    OUTPUT_BASENAME="$(basename "$4" $FILE_EXTENSION)"
    if [ -f "$OUTPUT_DIRNAME/${OUTPUT_BASENAME}mp3" ]; then
      mv "$OUTPUT_DIRNAME/${OUTPUT_BASENAME}mp3" "$4"
    fi
  fi
fi
