#!/bin/sh
#Barry Kauler 2005,2006 GPL
#Updated by Nathan Fisher June 2008.
#100608 lobster: disable ctrl-alt-backspace.
#101206 aragon: bugfix.
#150104 revert prior internationalization, implement gettext.
ps cax | grep picom
if [ $? -eq 0 ]; then
touch /tmp/picomrunning
chmod a+x /tmp/picomrunning
fi
# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

PARAM1="$1"
XLOCKEXE='xlock'
#[ "$(which xglock)" != "" ] && XLOCKEXE='xglock'

export TEXTDOMAIN=xlock_gui
export OUTPUT_CHARSET=UTF-8

# Password dialog
LOC_PASSWINDOW="$(gettext 'Clear Xlock password')"
LOC_PASSTEXT="$(gettext 'The first time Xlock runs, it will ask for a key, meaning a password. To change the password, you need to delete the file /root/.xlockrc, which is all that this button does...')"
LOC_CLEARBUTTON="$(gettext 'Click to clear password')"
# Configuration window
LOC_PRETEXT1="$(gettext '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.')"
LOC_PRETEXT2="$(gettext '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.')"
LOC_PRETEXT3="$(gettext 'After previewing, click the OK button for the one you want...')"
LOC_KILLBOX="$(gettext 'Kill any currently running animation:')"
LOC_KILLBUTTON="$(gettext 'KILL')"
LOC_CFGWINDOW="$(gettext 'Xlock: current mode is')"
LOC_MODE="$(gettext 'Mode')"
LOC_DESCR="$(gettext 'Description')"
LOC_TREE="${LOC_MODE} | ${LOC_DESCR}"
# modes
LOC_BALL="$(gettext 'shows bouncing balls')"
LOC_BLANK="$(gettext 'a black screen')"
LOC_DCLOCK="$(gettext 'floating digital clock')"
LOC_GLOOP="$(gettext 'goop from a lava lamp')"
LOC_LYAPUNOV="$(gettext 'shows lyapunov space')"
LOC_MANDELBROT="$(gettext 'shows mandelbrot sets')"
LOC_MATRIX="$(gettext 'shows the matrix (the movie)')"
LOC_PENROSE="$(gettext 'shows quasiperiodic tilings')"
LOC_SOLITAIRE="$(gettext 'shows a game of solitare')"
LOC_SPIRAL="$(gettext 'shows a helical locus of points')"
LOC_SWIRL="$(gettext 'shows animated swirling patterns')"
LOC_BOMB="$(gettext 'a bomb timer which logs out after a delay')"
LOC_RANDOM="$(gettext 'random mode')"
# buttons
LOC_PREVIEW="$(gettext 'Preview')"

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

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

if [ "$PARAM1" = "-password" ];then
 export XLOCKDIALOG1="<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 /root/.xlockrc</action>
    <action>exit:PASSWD</action>
   </button>
  </vbox>
 </window>"
 gtkdialog --program XLOCKDIALOG1
 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

#   <item>solitare | $LOC_SOLITAIRE</item>
#   <item>random | $LOC_RANDOM</item>
#   <item>bomb | $LOC_BOMB</item>
#   <item>swirl | $LOC_SWIRL</item>

if [ "`echo "$PARAM1" | grep --extended-regexp "configure|inroot"`" != "" ];then
 #configure xlock...
 export XLOCKDIALOG2="<window title=\"${LOC_CFGWINDOW} ${CURRMODE}\" icon_name=\"gtk-preferences\">
 <vbox>
  <text><label>${PRETEXT}</label></text>
  <text><label>${LOC_PRETEXT3}</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>spiral | ${LOC_SPIRAL}</item>
  </tree>
  <hbox>
   <button>
    <input file icon=\"image-x-generic\"></input>
    <label>${LOC_PREVIEW}</label>
    <action>${XLOCKEXE} -inwindow -mode \$MODE</action>
   </button>
   <button cancel><action>exit:abort</action></button>
   <button ok></button>
  </hbox>
 </vbox>
</window>"

 gtkdialog --program XLOCKDIALOG2 > /root/.config/xlocktmp
 . /root/.config/xlocktmp
 rm -f /root/.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}" > /root/.config/Xlock/xlockrootparams
  XLOCKPARAMS="`cat /root/.config/Xlock/xlockrootparams`"
  killall ${XLOCKEXE}
  ${XLOCKEXE} $XLOCKPARAMS &
  exit
 else
  echo -n " -grabserver -echokeys -echokey X -mode ${MODE}" > /root/.config/Xlock/xlockscreenparams
  exit
 fi
fi

#100608 lobster: disable ctrl-alt-backspace by disabling ctrl key...
xmodmap -e 'keycode 37='
xmodmap -e 'keycode 109='

if [ ! -f /root/.xlockrc ];then
killall picom &
sleep 1
 XLOCKPARAMS="`cat /root/.config/Xlock/xlockscreenparams`"
 rxvt -g 36x1 -title "Create key (password)" -e ${XLOCKEXE} ${XLOCKPARAMS}
else

killall picom &
sleep 1
 ${XLOCKEXE} `cat /root/.config/Xlock/xlockscreenparams`
fi

#100608 lobster: restore...
if [ -f /tmp/picomrunning ];then
exec /root/Startup/Picom &
rm /tmp/picomrunning
xmodmap -e 'keycode 37=Control_L'
xmodmap -e 'keycode 109=Control_R'
else
xmodmap -e 'keycode 37=Control_L'
xmodmap -e 'keycode 109=Control_R'
fi


###end###
