#!/bin/sh

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

VERSION="0.6.6"

# Make sure working space and files are in place
[ ! -d $HOME/.config/wallpaper ] && mkdir $HOME/.config/wallpaper
PREFS="$HOME/.config/wallpaper/preferences"
[ ! -f $PREFS ] && touch $PREFS
. $PREFS
[ "$INT" = "" ] && echo 'INT="15"' >> $PREFS
[ "$SLIDEDIR" = "" ] && echo 'SLIDEDIR="/usr/share/backgrounds"' >> $PREFS
[ "$FILER" = "" ] && echo 'FILER="defaultfilemanager"' >> $PREFS
[ "$VIEWER" = "" ] && echo 'VIEWER="defaultimageviewer"' >> $PREFS
[ "$IMGEDITOR" = "" ] && echo 'IMGEDITOR="defaultimageeditor"' >> $PREFS
. $PREFS
echo $FILER > $HOME/.config/wallpaper/FILER
echo $VIEWER > $HOME/.config/wallpaper/VIEWER
echo $IMGEDITOR > $HOME/.config/wallpaper/EDITOR

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $APPDIR/locale/english.po #always run to fill gaps in translation
[ -f "$APPDIR/locale/${LANGUAGE}.po" ] && . $APPDIR/locale/${LANGUAGE}.po 2> /dev/null

### begin About ###

about_wallpaper() {
export ABOUT_DLG="
<window title=\"$LOC_ABOUT_WINDOW\" icon_name=\"gtk-about\" window_position=\"2\">
 <vbox>
  <text use_markup=\"true\">
   <label>\"<b>Wallpaper-$VERSION</b>\"</label>
  </text>
  <frame $LOC_ABOUT_FRAME>
   <text>
    <label>\"$LOC_ABOUT_TEXT\"</label>
   </text>
  </frame>
  <hbox>
   <button>
    <label>$LOC_CLOSE</label>
    <input file stock=\"gtk-close\"></input>
   </button>
  </hbox>
 </vbox>
</window>
"
gtkdialog -p ABOUT_DLG &
}

### end About ###

## begin Preferences ###

wallpaper_preferences() {
echo $INT > $HOME/.config/wallpaper/int

export Wallpaper_preferences='
<window title="'$LOC_OPTIONS_WINDOW'" icon_name="gtk-preferences">
<vbox>
  <frame '$LOC_SLIDESHOW_FRAME'>
    <text>
      <label>'$LOC_DELAY'</label>
    </text>
    <hscale draw-value="true" update-policy="1" scale-step="1" scale-min="1" scale-max="60">
      <variable>INT</variable>
      <input file>'$HOME'/.config/wallpaper/int</input>
    </hscale>   
    <text><label>""</label></text>
    <text><label>'$LOC_DIR'</label></text>
    <hbox>
      <entry accept="directory">
        <default>'$SLIDEDIR'</default>
        <variable>SLIDEDIR</variable>
      </entry>
      <button>
        <input file stock="gtk-open"></input>
        <action type="fileselect">SLIDEDIR</action>
      </button>
    </hbox>
  </frame>
  <frame '$LOC_DEFAULTS'>
    <hbox>
      <text width-request="120"><label>'$LOC_FILEMANAGER'</label></text>
      <entry>
        <variable>FILER</variable>
        <input file>'$HOME'/.config/wallpaper/FILER</input>
      </entry>
      <button>
        <input file stock="gtk-open"></input>
        <action>programchooser $FILER system-file-manager FileManager None > '$HOME'/.config/wallpaper/FILER</action>
        <action type="refresh">FILER</action>
      </button>
    </hbox>
    <hbox>
      <text width-request="120"><label>'$LOC_VIEWER'</label></text>
      <entry>
        <variable>VIEWER</variable>
        <input file>'$HOME'/.config/wallpaper/VIEWER</input>
      </entry>
      <button>
        <input file stock="gtk-open"></input>
        <action>programchooser $VIEWER image-x-generic 2DGraphics None > '$HOME'/.config/wallpaper/VIEWER</action>
        <action type="refresh">VIEWER</action>
      </button>
    </hbox>
    <hbox>
      <text width-request="120"><label>'$LOC_EDITOR'</label></text>
      <entry>
        <variable>IMGEDITOR</variable>
        <input file>'$HOME'/.config/wallpaper/EDITOR</input>
      </entry>
      <button>
        <input file stock="gtk-open"></input>
        <action>programchooser $IMGEDITOR image-x-generic 2DGraphics Viewer > '$HOME'/.config/wallpaper/EDITOR</action>
        <action type="refresh">IMGEDITOR</action>
      </button>
    </hbox>
  </frame>
  <hbox>
    <button ok></button>
  </hbox>
</vbox>
</window>
'

gtkdialog -p Wallpaper_preferences > $HOME/.config/wallpaper/preferences
rm -f $HOME/.config/wallpaper/int
}

### end Preferences ###

### begin Filer ###

open_filer() {
. $PREFS
DIR="`cat $HOME/.config/wallpaper/bgdir`"
exec $FILER "$DIR" &
}

### end Filer ###

# parse our arguments
case $1 in
 -about)
 about_wallpaper
 exit 0
 ;;
 -preferences)
 wallpaper_preferences
 exit 0
 ;;
 -filer)
 open_filer
 exit 0
 ;;
 *)
 exit
 ;;
esac





