#!/bin/bash

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

ID=$1
EVENT=$2
shift 2

case $EVENT in

  init)
    cat <<END
      menu KCalc,kcalc,run kcalc
      submenu editmenu,&Edit

      tooltip "Copy some text. When you paste it into this demo, it will be converted to PDF."

      # We want mclick to send us the current selected text
      use selection

      # Place Copy/Cut/Paste in editmenu.
      use clipboard editmenu

      # Otherwise, they will be in the main menu:
      #use clipboard 

      # We can "understand" these MIME types, listed in order of preference
      clipboard accepts text/plain;charset=UTF-8, text/*

      # We give out a PDF in Copy/Cut (optional; used as default) 
      clipboard provides application/pdf

END
  ;;

  click)
    # For this demo, "click" set the contents of the clipboard (simulates a Copy)

    ## string: A simple sequence of characters
    #echo clipboard string "Hello world!"

    ## file: A reference to a local or remote file (URI; text/uri-list)
    # Use this if you want to e.g. paste the actual contents into Konqueror as a new file

    #echo clipboard file /home/alex/temp/file_to_paste_into_konqueror
    #echo clipboard file http://www.google.com/intl/en_ALL/images/logo.gif
    #echo clipboard file ftp://ftp.redhat.com/pub/redhat/linux/README

    ## text: The contents of a local text file
    #echo clipboard text /home/alex/temp/mytextfile.txt
    
    ## image: An image (for pasting into e.g. KolourPaint, The GIMP, etc.)
    #echo clipboard image /home/alex/temp/freshmeat.png

    ## pixmap: A pixmap optimized for drawing; loads more slowly than image
    #echo clipboard pixmap /home/alex/temp/myimage.jpg

    ## data: Generic data -- useful for custom MIME types
    #  In most cases, using "clipboard file" is better.
    #echo clipboard data /home/alex/temp/my-binary.xyz

    #echo clipboard data /home/alex/temp/testdoc.pdf

    ## link: A .desktop link file (shortcut)
    #  If provided, the additional parameter will form part of the pasted filename

    #echo clipboard link /home/alex/temp/mypic.jpg
    #echo clipboard link file:///home/alex/temp/mypic.jpg
    #echo clipboard link http://www.google.com/intl/en_ALL/images/logo.gif
    #echo clipboard link http://www.google.com/intl/en_ALL/images/logo.gif
    #echo clipboard link ftp://ftp.redhat.com/pub/redhat/linux/
    
    echo clipboard link http://konch.kdex.org/ Konch

    ## Update immediately, without waiting for manual Copy
    echo clipboard update

    echo popup "Try pasting into Konqueror (file manager) now"
  ;;

  rclick)
    echo menu show
  ;;

  mclick)
    FILE=$1
    ## Script should remove the temporary file once processed
    #  or it can move it somewhere more permanent if need be
    if [ $FILE ]; then
      kdialog --textbox $FILE 500 300
    else
      echo balloon Select some text first
    fi
  ;;

  copy)
    FILE=$1
    MIME=$2
    echo balloon "You copied <b>$FILE</b> ($MIME)"
    echo clipboard set
  ;;

  cut)
    FILE=$1
    MIME=$2
    echo balloon "You cut <b>$FILE</b> ($MIME)"
    echo clipboard set
    # rm -f $FILE
  ;;

  paste)
    FILE=$1
    MIME=$2

    ## MIME might be something like "text/plain;charset=UTF-8",
    #  so cut it off at the semicolon
    BASEMIME=`echo $MIME | cut -d\; -f1`

    if [ $FILE ]; then

      ## Check for UTF-16 (not usually in MIME type)
      U16=`file "$FILE" | grep UTF-16`
      if [ "$U16" != "" ]; then
        iconv -f UTF-16 -t UTF-8 "$FILE" > "$FILE.temp"
        rm -f "$FILE"
      else
        mv "$FILE" "$FILE.temp"
      fi

      case $BASEMIME in 
        text/*)
          ## Make a nice filename.. 
          FNAME=`cat $FILE.temp | grep -v "^$" | head -1` # first non-blank line

          # ...trimming whitespace, and based on the first 3 words
          TITLE=`echo $FNAME | cut -d" " -f1-3` 

          # ...using underscores instead of spaces
          FNAME=`echo $TITLE | tr -s -c "[:alnum:]" "_"` 

          ## KONCH_TMP is the local temp directory for this script
          #  Anything placed there will automatically be deleted when Konch is closed

          mv "$FILE.temp" "$KONCH_TMP/$FNAME"
          FILE="$KONCH_TMP/$FNAME"
        ;;

        *)
          mv "$FILE.temp" "$FILE"
        ;;
      esac

      if [ "$BASEMIME" == "text/html" ]; then
        mv "$FILE" "$FILE.html"
        FILE="$FILE.html"
      fi

      PDF="$FILE.pdf"

      HAS_A2PS=`which a2ps 2>/dev/null`
      HAS_ENSCRIPT=`which enscript 2>/dev/null`
      
      if [ -z $HAS_A2PS -a -z $HAS_ENSCRIPT ]; then
        echo popup "Please install enscript or a2ps first."
      else
        if [ $HAS_A2PS ]; then 
          a2ps "$FILE" -o- --center-title="$TITLE" | ps2pdf - "$PDF"
        elif [ $HAS_ENSCRIPT ]; then
          enscript -2rG -p- "$FILE" -b "$TITLE" | ps2pdf - "$PDF"
        fi
        
        ## Tell Konch about the new file.
        #  It's not yet in clipboard until copied or you send "clipboard update"
        echo clipboard file "$PDF" 
        BASE=`basename $PDF`
        echo balloon "File <b>$BASE</b> is ready to be copied to the clipboard."
      fi
    fi

  ;;

esac


