#!/bin/bash

FILENAME=$1
MIMETYPE=$2

BASENAME=$(basename "${FILENAME}")

# Personal laptop uses 12345; work laptop uses port 1234
if [ -n "`netstat -peanut 2>/dev/null|ag 12345`" ]; then
    # $REMOTE_HOST is defined in ~/.ssh/config
    REMOTE_HOST=personal-laptop
    REMOTE_DIR=/home/nieko/Incoming/
    WINDOWS_DIR="D:\\!incoming\\"
    # May not find an extension; tough luck, then
    REMOTE_FILE="${REMOTE_DIR}/${BASENAME}."`/bin/fgrep "${MIMETYPE}" /etc/mime.types | awk '{print $2}'`
    TYPE="windows"
elif [ -n "`netstat -peanut 2>/dev/null|ag 1234`" ]; then
    REMOTE_HOST=work-laptop
    REMOTE_DIR=/home/nieko/Downloads/
    WINDOWS_DIR="C:\\Users\\nieko\\Downloads\\"
    # May not find an extension; tough luck, then
    REMOTE_FILE="${REMOTE_DIR}/${BASENAME}."`/bin/fgrep "${MIMETYPE}" /etc/mime.types | awk '{print $2}'`
    TYPE="windows"
# Linux example
#elif [ -n "`netstat -peanut 2>/dev/null|ag 1234`" ]; then
#    REMOTE_HOST=work-laptop
#    REMOTE_DIR=/tmp/mutt-remote
#    REMOTE_FILE="${REMOTE_DIR}/${BASENAME}"
#    TYPE="linux"
else
    echo "Laptop not recognized"
    exit
fi

# Copy the file to the remote host
ssh "${REMOTE_HOST}" "mkdir -p ${REMOTE_DIR}"
scp "${FILENAME}" "${REMOTE_HOST}:${REMOTE_FILE}"

if [[ "$TYPE" == "linux" ]]; then
    # Set the DISPLAY variable so X appliations can find the display (this
    # assumes the display is on :0, but that's usually the case anyway). We
    # use run-mailcap to view the file copied, which looks up a program in
    # /etc/mailcap. Here we open the single quote for the filename
    # argument.
    COMMAND="DISPLAY=:0 run-mailcap --action=view '"

    if [ -n "${MIMETYPE}" -a "${MIMETYPE}" != "application/octet-stream" ]; then
        # If the mimetype is specified and is not the useless
        # octet-stream mimetype, prefix the the filename with it.
        COMMAND="${COMMAND}${MIMETYPE}:"
    fi

    # Here we close the single quote for the filename argument.
    # Note: Quoting the filename in single quotes won't work well enough
    # when there are single quotes in $REMOTE_FILE.
    COMMAND="${COMMAND}${REMOTE_FILE}'"

    # Run the command in the background. Redirect stdout so ssh terminates
    # after running the command (just backgrouding wasn't enough, but stderr
    # and stdin don't need redirection apparently).
    COMMAND="${COMMAND} > /dev/null &"
elif [[ "$TYPE" == "windows" ]]; then
    # This is a Windows directory; bash-only directories aren't available to cmd.exe
    COMMAND="cd $REMOTE_DIR; /mnt/c/Windows/System32/cmd.exe /c start \"\" '"

    # Change bash path to Windows path
    WINDOWS_FILE="${REMOTE_FILE/$REMOTE_DIR/$WINDOWS_DIR}"
    COMMAND="${COMMAND}${WINDOWS_FILE}'"

    COMMAND="${COMMAND};"
else
    echo "OS type not supported: $TYPE"
    exit
fi

# We wait a short bit so we can see anything that appears on stderr
# directly (like mailcap errors).
COMMAND="${COMMAND} sleep 5"

# Don't forget to delete the file afterwards.
COMMAND="${COMMAND}; rm '${REMOTE_FILE}'"

# Run the command.
ssh "${REMOTE_HOST}" "${COMMAND}"
