#!/bin/sh

# Remove temporary files and dirs
##exit_handler() {
  ##trap - EXIT
  ##test -f "$LINK" && rm "$LINK" || : 
  ##exit $rc
##}
##trap exit_handler EXIT HUP INT QUIT PIPE TERM

File="${1:-$TMPDIR/$(basename $0)}"
URL="$2"

# pick up vim commands
case "$URL" in
  */twiki/*)			# TWiki
  	Ext="twiki";
  	VimCmd="$VimCmd set ft=twiki"
	;;
  *action=edit\&editor=text|*\?action=edit|*[#]preview)	# MoinMoin
  	Ext="Moin";
  	VimCmd="$VimCmd set ft=moin"
	;;
  *livejournal.com*|*lj.rossia.org*)
  	Ext="html"
	;;
  *)				# Dunno
  	Ext="txt";
	;;
esac

# Make a proper name for ftplugin
case $(file "$File") in
	"$File: "HTML*) Ext="html" ;;
esac
LINK="$File.$Ext"
ln -f "$File" "$LINK"

# pick up encoding
case "$URL" in
  *) export LC_ALL=ru_RU.UTF-8
     export LANG=ru_RU.UTF-8
     ;;
esac
shift; shift;

which unicodize >/dev/null 2>&1 && unicodize - "$LINK" "$Ext" || :
which gvim >/dev/null 2>&1 && {
  gvim +"$VimCmd" -f "$LINK" "$@"
} || {
  xterm -e vim +"$VimCmd" "$LINK" "$@"
}

which unicodize >/dev/null 2>&1 && unicodize "$LINK" "$Ext" || :
ln -f "$LINK" "$File"