#!/bin/sh
## global declaration
FILE_TO_EDIT=""

function edit_function()
{
  EXTENSION=$(extension $1)
  COMMAND=$(association $EXTENSION)
  if [ "$COMMAND" == "" ];
  then
    COMMAND=$(association text)
  fi
    $($COMMAND $*)
}

function getFileFrom()
{
  FILE_TO_EDIT=$(Xdialog --stdout --title "Please choose a file" --fselect $1 0 0)
}

if [ "$1" == "" ];
then
  ### edit from current dir
  getFileFrom $PWD
  if [ $? == 0 ];
  then
    edit $FILE_TO_EDIT
  fi

else
  if [ -d $1 ];
  then
    ### $1 is a directory
    getFileFrom $1
    if [ $? == 0 ];
    then
      edit_function $FILE_TO_EDIT
    fi
  else
    ### $1 is a file
    edit_function $*
  fi
fi


