#!/bin/bash

# dcop-demo-hosted, for Konch 1.00+
# Author: Alex Ibrado <alex@kdex.org>
# Part of the Konch distribution, http://konch.kdex.org/

function do_hello {
  NAME=$1
  AGE=$2

  if [ $AGE -gt 0 -a "$NAME" != "" ]; then
    echo "return Hello $NAME! You are $AGE years old."
  elif [ "$NAME" != "" ]; then
    echo "return Hello $NAME!"
  else
    echo "return Hello there!"
  fi
}

while [ 1 ]; do
  read ID CMD PARM
 
  case $CMD in
    init)
      cat <<END
        name "Hosted DCOP Demo"
        menu Run kdcop,view_top_bottom,kdcop
        
        /*
          "use dcop" notes

          "use dcop" not specified or no params => dcop $KONCH Script ...
          use dcop myscript => dcop myscript Script ...
          use dcop myscrypt- => dcop myscript-<pid> Script ...
          use dcop myscript MyDCOP => dcop myscript MyDCOP ...
          use dcop myscript- MyDCOP => dcop myscript-<pid> MyDCOP ...

          Note that "myscript" is just a convenient global alias for $KONCH
        */
          
        // Make it "dcop konchscript Demo ..."
        use dcop konchscript Demo 

        // dcop konchscript Demo fader
        dcop void fader()

        // dcop konchscript Demo blinker 250
        dcop void blinker(int x)

        // dcop konchscript Demo adder 123 456
        dcop int adder(int a, int b)

        // Demo of QStringList (multiline) result
        // dcop konchscript Demo turtles
        dcop QStringList turtles()

        ## Overloaded methods ####
        ##
        ## *** Please use KDCOP to fully test hello() ***
        ##
        ## The "dcop" command-line client just looks for the first function
        ##  that has the correct number of parameters. 
        ##
        ## You don't need to use overloading, just use different function names

        // Script is called with "hello"
        // dcop konchscript Demo hello
        dcop QString hello()

        /* Script is called with "hello_1" because "hello" already exists
           ... then hello_2, hello_3, etc. */
        // dcop konchscript Demo hello "Alex Ibrado"
        dcop QString hello(QString name)

        // Script is called with "MyHello" because an alias was specified
        // dcop konchscript Demo hello "Alex Ibrado" 18
        // ...18?! I wish! :-P
        dcop QString hello(QString name, int age) MyHello

        ###
        # These two will only work via kdcop
        ###

        // hello_2
        dcop QString hello(int age, QString name) 

        // MyHello_1
        dcop QString hello(int age) MyHello

END
        kdialog --msgbox "Please use kdcop to fully test the hello() functions. The command-line client has a limitation: it only finds the first function with the correct number of parameters." &
    ;;
        
    rclick)
      echo menu show
    ;;


  ### Our script's events ###

    kdcop)
      kdcop &
    ;;

    fader)
      echo fade
    ;;

    blinker)
      # PARM already has the required value
      echo blink $PARM
    ;;

    adder)
      # There's one more parameter (in addition to PARM). Read it from STDIN.
      read Y

      let Z=PARM+Y
      echo return $Z
    ;;

    turtles)
      # Note the [ brackets ]. You can also use ( parentheses ).
      cat <<END
        return [
           Leonardo
           Raphael
           Michelangelo
           Donatello
           Pong Pagong (Wheee!)
        ]
END
    ;;

    hello)
      do_hello
    ;;

    hello_1)
      # Need to quote in case of spaces in name
      do_hello "$PARM"
    ;;

    MyHello)
      read AGE
      do_hello "$PARM" $AGE
    ;;

    # Won't be usable by "dcop", only by "kdcop"
    hello_2)
      read NAME
      do_hello "$NAME" $PARM
    ;;

    # Won't be usable by "dcop", only by "kdcop"
    MyHello_1)
      do_hello "stranger" $PARM
    ;;

  esac
done
