#!/bin/bash
# a rough conversion of curfloo user-added commands to curphoo commands
# by 'edge-op' with thanks to the_cpu_fan for curphoo,
#  thanks to hiro_protagonist_ii for curfloo
#  and thanks to slak_hack for another version (fork?) of curphoo
#  thank you the_Hai_Flounder for your tutelage in awk and the
#   elimination of excess temp files and steps
# your mileage may vary; use at your own risk; no warranty of any kind
#
# check to see if curfloo commands and the curphoo directory exist
if [ -f ~/.curfloo/commands ] && [ -d ~/.curphoo ]
    then
        cd ~/.curphoo
#       # save the original ~/.curphoo commands
        if [ ! -f commands.org ]
            then
                cp commands commands.org
        fi
#        # sed to convert curfloo's command: and command::
#        #  to curphoo's :command ::command format
#        # awk to add the \n after each :command and ::command
#        # sed to convert all $0, $1, ... $* in curfloo to the %s in curphoo
#        # (note: this results in some commands that will not work in curphoo)
#        # (note2: trinitAX added a workaround (the regexp expressions that
#        #  deal with !! and !) to combat what is an inconsistency in the 
#        #  way curphoo deals with user commands. for most user commands,
#        #  curphoo uses :com \n [content] and ::com \n [content] to send
#        #  [content] to chat. for echoing the output of system commands,
#        #  however, it uses !com \n [system command] which doesn't quite
#        #  match the pattern)
        sed -e 's/^\([[:alnum:]]*\):!!/!!\1 /g' \
	        -e 's/^\([[:alnum:]]*\):!/!\1 /g' \
	        -e 's/^\([[:alnum:]]*\)\(:\)\(:\)*/\3\2\1 /g' \
                -e 's/\$[0-9*]/%s/g' ~/.curfloo/commands | \
        awk '{ print $1 "\n" \
                substr($0, index($0,$1)+length($1)+1, length($0)) "\n"
        }' > curf_conv
#        # grab the original commands and append the converted ones to that
#        # (this makes sure things work right if you run this multiple times)
        cat commands.org curf_conv > commands
#        # remind the user of breakage
        echo "check and edit your new commands file!"
fi
