#!/bin/bash

VIEWER_PATH="/usr/lib/nspluginwrapper/i386/linux"
cd "$VIEWER_PATH"

BIN="npviewer.bin"
BIN_REAL="$BIN.real"

FILE_TYPE=`file -b --mime-type "$BIN"`

function close_apps
{
  APP=$1
  echo "Cannot write to $APP, please close any running Firefox etc. and try again."
  exit 1
}

if [ "$FILE_TYPE" = "application/x-executable" ]
then
  echo "Trying to replace $BIN with a wrapper shell script."
  cp "$BIN" "$BIN_REAL" 2>/dev/null || close_apps "$BIN_REAL"
else
  echo "$BIN is not of the expected type, not replacing it!"
  exit 0
fi

truncate --size 0 "$BIN" 2>/dev/null || close_apps "$BIN"

echo \#\!/bin/sh > "$BIN"
echo GDK_NATIVE_WINDOWS=true "$VIEWER_PATH/$BIN_REAL" \$\* >> "$BIN"
chmod 755 "$BIN"

echo "Done!"
