#!/bin/sh
#Barry Kauler 2005,2006 GPL
#Updated by Nathan Fisher June 2008.

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

# 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
. $APPDIR/locale/${LANGUAGE}.po 2> /dev/null

PARAM1="$1"

[ ! -d $HOME/.config/Xlock ] && mkdir -p $HOME/.config/Xlock

if [ ! -f $HOME/.config/Xlock/xlockscreenparams ];then
 echo -n ' -mousemotion -grabserver -echokeys -echokey X -mode goop'  > $HOME/.config/Xlock/xlockscreenparams
fi
if [ ! -f $HOME/.config/Xlock/xlockrootparams ];then
 echo -n ' -bg white -inroot -mode goop'  > $HOME/.config/Xlock/xlockrootparams
fi
if [ "$PARAM1" = "-inroot" ];then
 CURRMODE="`cat $HOME/.config/Xlock/xlockrootparams | tr -s ' ' | tr ' ' "\n" | tail -n 1`"
else
 CURRMODE="`cat $HOME/.config/Xlock/xlockscreenparams | tr -s ' ' | tr ' ' "\n" | tail -n 1`"
fi
if [ "$CURRMODE" = "-mode" ];then
 CURRMODE=""
fi

if [ "$PARAM1" = "-password" ];then
 export MAINDIALOG="<window title=\"$LOC_PASSWINDOW\" icon_name=\"gtk-clear\" window_position=\"2\">
  <vbox>
   <text><label>\"$LOC_PASSTEXT\"</label>
   </text>
   <button>
    <label>$LOC_CLEARBUTTON</label>
    <action>rm -f $HOME/.xlockrc</action>
    <action>exit:PASSWD</action>
   </button>
  </vbox>
 </window>
"
 gtkdialog3 --program MAINDIALOG
 exit
fi

if [ "$PARAM1" = "-configure" ];then
 PRETEXT="$LOC_PRETEXT1"
 KILLBOX=""
else
 PRETEXT="$LOC_PRETEXT2"
 KILLBOX="
 <hbox>
  <text><label>$LOC_KILLBOX</label></text>
  <button>
   <label>$LOC_KILLBUTTON</label>
   <action>exit:KILLED</action>
  </button>
 </hbox>
"
fi

if [ ! "`echo "$PARAM1" | grep --extended-regexp "configure|inroot"`" = "" ];then
 #configure xlock...
 export MAINDIALOG="<window title=\"$LOC_CFGWINDOW $CURRMODE\" icon_name=\"gtk-preferences\">
 <vbox>
  <text use_markup=\"true\"><label>$PRETEXT</label>
  </text>
  $KILLBOX
  <tree>
   <label>$LOC_TREE</label>
   <variable>MODE</variable>
   <height>150</height>
   <item>ball | $LOC_BALL</item>
   <item>blank | $LOC_BLANK</item>
   <item>dclock | $LOC_DCLOCK</item>
   <item>goop | $LOC_GLOOP</item>
   <item>lyapunov | $LOC_LYAPUNOV</item>
   <item>mandelbrot | $LOC_MANDELBROT</item>
   <item>matrix | $LOC_MATRIX</item>
   <item>penrose | $LOC_PENROSE</item>
   <item>solitare | $LOC_SOLITAIRE</item>
   <item>spiral | $LOC_SPIRAL</item>
   <item>swirl | $LOC_SWIRL</item>
   <item>bomb | $LOC_BOMB</item>
   <item>random | $LOC_RANDOM</item>
  </tree>
  <hbox>
   <button>
    <input file icon=\"image-x-generic\"></input>
    <label>$LOC_PREVIEW</label>
    <action>xlock -inwindow -mode \$MODE</action>
   </button>
   <button cancel><action>exit:abort</action></button>
   <button ok></button>
  </hbox>
 </vbox>
</window>
"

 gtkdialog3 --program MAINDIALOG > $HOME/.config/xlocktmp
 . $HOME/.config/xlocktmp
 rm -f $HOME/.config/xlocktmp
 [ "$EXIT" = "abort" ] && exit 0

 #extra params chosen here...
 # -delay microseconds  --time between batch repeats
 # -count number        --number of things to do per batch
 # -size number         --size of an object
 # -erasemode           --random_lines, random_squares, venetian, triple_wipe, quad_wipe, circle_wipe, three_circle_wipe, squaretate,  fizzle,   spiral,   slide_lines,   losira, no_fade
 # -erasedelay microseconds --time of each step of erase

 #paranoid precaution...
 if [ "`echo "$MODE" | grep --extended-regexp "solitare|mandelbrot|swirl|spiral|penrose|matrix|lyapunov|goop|dclock|ball|blank"`" = "" ];then
  MODE="$CURRMODE"
 fi

 if [ "$PARAM1" = "-inroot" ];then
  echo -n " -inroot -mode $MYCHOICE" > $HOME/.config/Xlock/xlockrootparams
  XLOCKPARAMS="`cat $HOME/.config/Xlock/xlockrootparams`"
  killall xlock
  xlock $XLOCKPARAMS &
  exit
 else
  echo -n " -grabserver -echokeys -echokey X -mode $MODE" > $HOME/.config/Xlock/xlockscreenparams
  exit
 fi
fi


if [ ! -f $HOME/.xlockrc ];then
 XLOCKPARAMS="`cat $HOME/.config/Xlock/xlockscreenparams`"
 defaultterm -g 36x1 -title "Create key (password)" -e xlock $XLOCKPARAMS
else
 xlock `cat $HOME/.config/Xlock/xlockscreenparams`
fi

###end###
