#!/bin/sh
#
# Copyright 2006, 2007 by
# Manuel Carrasco
#
# This file is part of the LookXP project, and may only be used, modified,
# and distributed under the terms of the LookXP project license,
# LICENSE.TXT.  By continuing to use, modify, or distribute this file you
# indicate that you have read the license and understand and accept it
# fully.
#
#             http://lxp.sourceforge.net


## Commands in preferences icewm files
# RunCommand=lxp-cmd --run
# MailCommand=lxp-cmd --mail
# NewMailCommand=lxp-cmd --newmail
# LockCommand=lxp-cmd --lock
# ClockCommand=lxp-cmd --calendar
# OpenCommand=lxp-cmd --open
# TerminalCommand=xterm -bg black -fg white

isInstalled() {
  [ -z "$1" ] && return 1
  H=`which "$1" 2>/dev/null`
  [ -n "$H" -a -x "$H" ] && return 0 || return 1
}
Error() {
  tit="LookXP: Error"; msg=" ${@}"
  isInstalled zenity  && exec zenity  --title="$tit" --error --text "$msg"
  isInstalled kdialog && exec kdialog --title="$tit" --error "$msg"
  isInstalled Xdialog && exec Xdialog --title "$tit" --no-cancel --msgbox "$msg" 0 0
}
Msg() {
  tit="LookXP"; msg=" ${@}"
  isInstalled zenity  && exec zenity  --title="$tit" --info --text "$msg"
  isInstalled kdialog && exec kdialog --title="$tit" --msgbox "$msg"
  isInstalled Xdialog && exec Xdialog --title "$tit" --no-cancel --msgbox "$msg" 0 0
}
Wait() {
  tit="LookXP"; msg=" ${@}"
  isInstalled zenity  && exec zenity  --title="$tit" --progress --pulsate --text "$msg" </dev/zero
  isInstalled kdialog && exec kdialog --title="$tit" --msgbox "$msg"
  isInstalled Xdialog && exec Xdialog --title "$tit" --no-cancel --msgbox "$msg" 0 0
}
Run() {
  tit="LookXP: Run"; msg="Run command ..."
  if   isInstalled zenity; then
       run=`zenity --title="$tit" --entry --text="$msg" 2>/dev/null`
       [ $? != 0 ] && return
  elif isInstalled kdialog; then
       kdialog --title "$tit" --inputbox "$msg" >/tmp/$$ 2>/dev/null
       [ $? != 0 ] && rm -f /tmp/$$ && return
       run=`cat /tmp/$$`
       rm -f /tmp/$$
  elif isInstalled Xdialog; then
       run=`Xdialog --stdout --title "$tit" --inputbox "$msg" 0 0 2>/dev/null`
       [ $? != 0 ] && return
  else
       return
  fi
  command=`echo $run | awk '{print $1}'`
  isInstalled $command || Error "$command: Command not found" 
  exec $run
}
Calendar() {
  tit="LookXP: Calendar"; msg="Calendar"
  if   isInstalled zenity; then
       exec zenity --title="$tit" --calendar --text="" >/dev/null 2>&1
  elif isInstalled Xdialog; then
       exec Xdialog --no-cancel --stdout --title "$title" --calendar "$msg" 0 0 0 0 0 >/dev/null 2>&1
  elif isInstalled xclock; then
       exec xclock
  fi
}
OsView() {
  isInstalled xosview && exec xosview +net
}
Open() {
  Error "${@}"
}
Logout(){
  D=`cat ~/.icewm/.defaultdesktop 2>/dev/null | head -1`
  echo LOGOUT $D >&2
  killall $D >/dev/null 2>&1
  killall icewm >/dev/null 2>&1
  exit 0
}
Idesk() {
:
}


###
### MAIN
###
arg=$1
shift 1
case $arg in
    --calendar)   Calendar    "${@}";;
    --msg)        Msg    "${@}";;
    --error)      Error    "${@}";;
    --run)        Run        "${@}";;
    --open)       Open        "${@}";;
    --wait)       Wait        "${@}";;
    --cpu|--net)  OsView        "${@}";;
    --logout)     Logout;;
    *)            Error " LXP: $arg option is not implemented yet";;
esac
