#!/bin/sh
#Barry Kauler 2005,2006 GPL
#Updated by Nathan Fisher June 2008.
#Updated for Fatdog64 L18L Dec 2014

alias _=gettext
export TEXTDOMAIN=xlock
. gettext.sh

## 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=\"$(_ 'Clear Xlock password')\" icon_name=\"gtk-clear\" window_position=\"2\">
  <vbox>
   <text><label>\"`eval echo $(_ '     The first time Xlock runs, it will ask for a key, meaning a
password. To change the password, you need to delete the
file $HOME/.xlockrc, which is all that this button does...')`\"</label>
   </text>
   <button>
    <label>$(_ 'Click to clear password')</label>
    <action>rm -f $HOME/.xlockrc</action>
    <action>exit:PASSWD</action>
   </button>
  </vbox>
 </window>
"
 gtkdialog3 --program MAINDIALOG
 exit
fi

if [ "$PARAM1" = "-configure" ];then
 PRETEXT="$(_ 'This window is for configuring Xlock as a screen locker.
Xlock is a privacy system that you invoke manually and with
a password to unlock -- this is what Xlock will do, with bonus
pretty pictures when locked.

After previewing, click the OK button for one you want...')"
 KILLBOX=""
else
 PRETEXT="$(_ '     This window is to choose a desktop background animated
image. Note there is also a xlock icon on the desktop that
will lock the screen, with only the animation displaying.

After previewing, click the OK button for one you want...')"
 KILLBOX="
 <hbox>
  <text><label>$(_ 'Kill any currently running animation:')</label></text>
  <button>
   <label>$(_ 'KILL')</label>
   <action>exit:KILLED</action>
  </button>
 </hbox>
"
fi

if [ ! "`echo "$PARAM1" | grep --extended-regexp "configure|inroot"`" = "" ];then
 #configure xlock...
# TITLE="`eval_gettext 'current mode is $CURRMODE'`"
 TITLE="$(eval echo `_ 'current mode is $CURRMODE'`)"
 TITLE="`eval echo $(_ 'current mode is $CURRMODE')`"
 export MAINDIALOG="<window title=\"$TITLE\" icon_name=\"gtk-preferences\">
 <vbox>
  <text use_markup=\"true\"><label>${PRETEXT}</label>
  </text>
  $KILLBOX
  <tree>
   <label>$(_ 'Mode | Description')</label>
   <variable>MODE</variable>
   <height>150</height>
   <item>ball | $(_ 'shows bouncing balls')</item>
   <item>blank | $(_ 'a black screen')</item>
   <item>dclock | $(_ 'floating digital clock')</item>
   <item>goop | $(_ 'goop from a lava lamp')</item>
   <item>lyapunov | $(_ 'shows lyapunov space')</item>
   <item>mandelbrot | $(_ 'shows mandelbrot sets')</item>
   <item>matrix | $(_ 'shows the matrix (the movie)')</item>
   <item>penrose | $(_ 'shows quasiperiodic tilings')</item>
   <item>solitare | $(_ 'shows a game of solitare')</item>
   <item>spiral | $(_ 'shows a helical locus of points')</item>
   <item>swirl | $(_ 'shows animated swirling patterns')</item>
   <item>bomb | $(_ 'a bomb timer which logs out after a delay')</item>
   <item>random | $(_ 'random mode')</item>
  </tree>
  <hbox>
   <button>
    <input file icon=\"image-x-generic\"></input>
    <label>$(_ '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`"
 TITLE="$(_ 'Create key (password)')"
 defaultterm -g 36x1 -title "$TITLE" -e xlock $XLOCKPARAMS
else
 xlock `cat $HOME/.config/Xlock/xlockscreenparams`
fi

###end###	
