#!/bin/bash
## __________________________________________________________________
## Universal Help Bash Scripting  
## interhacktives Scripting Tutorial    
## 
## ___________________________________________________________________
## copyright by karl.thomas.schmidt@interhacktive.de 2012-2018 
## http://www.interhacktive.de     
## ___________________________________________________________________

username=${HOST:0:3}-${USER:0:3}
headers=()

#                Stupid, but your choice.
# ask-missing == if command given with --execute is not found
#                ask user to update command-table
#
# ask|auto|silent|never|ask-missing
auto_update='ask'

# PASTER sources first /etc/paster/paster.conf
# then $HOME/.paster/paster.conf if they exist
# It is perfectly OK to use PASTER without any configuration
# Hence both config dirs can be deleted any time without 
# having side effects to paster itself. Just all keys are gone then.
#
user_conf_dir="$HOME/.paster"
sys_conf_dir="/etc/paster"

cmds_filename='command-table'
paster_filename='paster'
sys_cmds_pathname="$sys_conf_dir/$cmds_filename"
cmds_pathname="~/.paster/$cmds_filename"

# URLs for pasting to the server
# for the time beeing only paste.interhacktive.de 
# but other paste servers can be easily included.
# drop a mail at https://paste.interhacktive.de 
# if you need other paste servers as well
#
# 
url_base='https://paste.interhacktive.de'
url_version_check=$url_base'/version'
url_version_paster=$url_version_check/$paster_filename
url_version_cmds=$url_version_check/$cmds_filename
url_download_cmds=$url_base/download/$cmds_filename
url_download_paster=$url_base/download/$paster_filename

url_check_paste='https://paste.interhacktive.de/putcheck'
put_url='https://paste.interhacktive.de/upload/'


# PASTER uses basic GNU tools and openSSL for encryption
# for putting results into clipboard xclip is used.
# PASTER checks, whether all needed programs are installed.
# If not, it asks, whether they should be installed.
programs_needed=(df which mktemp cat curl sed  hexdump type openssl cryfs)
programs_needed=(df which mktemp cat curl sed awk hexdump openssl )


# https://paste.InterHacktive.de can be used to exchange encrypted
# files with others. PASTER creates private/public keypairs for the user
# and a keypairs for the recipient. PASTER does NOT distribute the keypairs.
# This is up to the user. But paste.interhacktive.de can be used to hand the
# public keys over to the recipient.
# Encrypted pastes will never be shown on website. They are private pastes
# by default.
#
# Keyfile names are never transmitted to any server or any recipients.
# They are solely for the user to easily identify the keypairs.
# They can of course have names like "1234567890", "wrdlbrmpft" or "fake president"
# In fact they are just filenames. So every valid filename is OK.
#
# Keypairs can be savely deleted at any time. (Of course no file encrypted
# can be decrypted anymore )
#
# the following default keypair is special in that it is not used
# for encryption of pastes. It allows user to create private posts without
# registering with the server which requires a vaild email
# address
key_filename="$USER@paster"
#  
# This keypair is used only for pasting private posts on the server
# without the need for registering with the website.
# Registering is of course always possible, but needs a valid email address. 
# The private Key of this  "special" keypair is send to the pasteserver
# The client encrypts meta data with the puclic key and sends it to the server 
# encrpted. 
# With this private key the server decrypts the meta data
# and is hence capable to map the paste to the user without a need
# for registering with the server. 
#
# The mapping of a private paste is done by the sha-hash of the key.
# (see docs of openSSL for details )
# This way a non- registered user can give the "privat paste hash" 
# and the url of the paste. Without entering the key-hash the paste
# cannot bee seen.
# No information needs to be disclosed at any time this way.
#
# And of course you can use encrytion for a private paste too.
# This enables the user to pass a crypted non-public paste to
# another user without registering one or both of the participants.

# key_store_dir will be used as a plain dir or as mountpoint for "cryfs"
# depending on the value of \$use_crypted_keystore
# All keypairs are stored in directory:
keystore_dir="$user_conf_dir"/"keystore"

# the keystore can be encrypted with "cryfs"
# use_crypted_keystore= yes | no
use_crypted_keystore='yes'

# if use_crypted_keystore is 'yes' we need additionally
# a directory where the crypted data reside
cryfs_dir="$user_conf_dir"/"crydir"

# version string
# DO NOT EDIT version!!
# is done completly automatically!
version=0.1.3


programs_needed[${#programs_needed[*]}]=fold
programs_needed[${#programs_needed[*]}]=tput
declare -x supported_colors=$(tput colors)



base03=$( tput setaf 234)
base02=$( tput setaf 235)
base01=$( tput setaf 240)
base00=$( tput setaf 241)
base0=$( tput setaf 244)
base1=$( tput setaf 245)
base2=$( tput setaf 254)
base3=$( tput setaf 230)
yellow=$( tput setaf 136)
orange=$( tput setaf 166)
red=$( tput setaf 160)
magenta=$( tput setaf 125)
violet=$( tput setaf 61)
blue=$( tput setaf 33)
cyan=$( tput setaf 37)
green=$( tput setaf 64)

isCheckWinSizeOn=$(LANG=C shopt | grep checkwinsize | awk '{print$NF}')
shopt -s checkwinsize


bold=$(tput bold)
hbmode=$(tput dim)         # turn on half-bright mode
ul=$(tput smul)            # begin underline mode
ul_end=$(tput rmul)         # exit underline mode
reverse=$(tput rev)        # Turn on reverse mode
standout=$(tput smso)      # Enter standout mode (bold on rxvt)
standout_end=$(tput rmso)  # Exit standout mode
reset=$( tput sgr0)        #  Turn off all attributes

ok_prefix="[${green}OK${reset}]"
fail_prefix="$(tput setab 246)[${red}${bold}FAIL!$reset$(tput setab 246)]$reset"
info_prefix="[${yellow}INFO$reset]"



declare -x msg_indent_cols
declare -x msg_indent_char
declare -x msg_indent
declare -x msg_width


# taken from https://stackoverflow.com/questions/10060500/bash-how-to-evaluate-ps1-ps2#10060576
PS1_expanded=$(echo WrdlbrmpfdHalliGalli | bash -i  2>&1 \
         | grep WrdlbrmpfdHalliGalli                     \
         | head -1                                       \
         | sed 's/WrdlbrmpfdHalliGalli//g')



dimensions_update(){
    cols=$(tput cols)
    lines=$(tput lines)
    msg_indent_cols=2
    msg_indent_char=' '
    msg_indent=$( while ((msg_indent_cols > i)); do printf "%s" "$msg_indent_char"; ((i++)); done; )
    msg_width=$((cols - msg_indent_cols))
}
dimensions_update

trap dimensions_update WINCH

indent_long_msg(){
    echo $* |  fold -s -w $msg_width | while read line; do
                                         printf "$msg_indent$line\n"
                                       done
}


msg(){
    #printf "$msg_indent$*\n"
    indent_long_msg $*
}

msg_fail(){
    printf "${msg_indent}$fail_prefix$reset $*$reset\n"
}

msg_ok(){
    printf "$msg_indent$ok_prefix $*\n"
}

msg_info(){
    printf "$msg_indent$info_prefix $*\n"
}

msg_ask(){
    local answer_prefix='['
    local answer_postfix=']'
    local i=0
    local choices=$2
    local numChoices=${#choices}
    local default=$( while (( i < numChoices )); do 
                        if [[ ${choices:i:1} =~ [A-Z] ]] ; then 
                            printf ${choices:i:1}
                        fi
                        ((i++))
                     done
                   )
    if [[ ${#default} -gt 1 ]] ; then
        msg_fail $( printf "$( gettext "too many default answers given. Default cannot be \"$default\". Wrong call to function \"${FUNCNAME[1]}\" in file \"${BASH_SOURCE[1]}\" at line \"${BASH_LINENO}\"")" "$default" ) 
        exit 1
    fi
    ((i=0))
    local answer_choices="$answer_prefix$choices$answer_postfix"
    printf "$1 $answer_choices : "
    while read -n1 answer; do
        if [[ "${choices}" == *"${answer}"* || "${choices}" == *"${answer^^}"*  ]]; then
            declare -x answer="$answer"
            REPLY=$answer
            echo
            break
        elif [[ $answer == '' ]]; then
            declare -x answer="$answer"
            REPLY=$answer
            echo
        else
            printf "\n%s\n" "$(gettext "Wrong answer. Try again." )"
            printf "$1 $answer_choices : "
        fi
    done
}

build_answers_array(){
    IFS=',' args=( ${*}) 
    n=${#args[*]}
    while ((i<${n})); do
        arg=${args[i]}
        messages[${#messages[@]}]="${arg#*:}"
        keys="$answer""${arg%:*}"
        ((i++))
        echo answer=$keys message=${messages[*]}
    done 
}

#msg_ask "$@"
#build_answers_array "$*"



_initTemp(){
    #TODO make this configurable
    temp_dir=$( mktemp -d /dev/shm/$(basename $0)-$(date +%s)-XXXXXXXXX )
}

mkTempFile(){
    printf "%s\n" "$( mktemp $temp_dir/$(basename $0)-$(date +%s)-XXXXXXXX )"
}

cleanup(){
    rm -rf $temp_dir
}

_initTemp
trap cleanup EXIT

 
show_colors(){
    #i18n:
    # help message when colors are listed. displayes at last line of screen
    # if output takes more then a "window" height to display all colors
    less_prompt=$(gettext 'Color Table\:  <b> PageUp, <blank> PageDown, <q> quit')
    shopt -s checkwinsize
    
    rs='\033[0m'
    
    declare -ax array_fg
    declare -ax array_bg
    
    
    cols=$(tput cols)
    lines=$(tput lines)
    available_colors=$(tput colors)
    i=0
    while (( i < available_colors)); do
        fgc=$(tput setaf $i)
        bgc=$(tput setab $i)
        printf "%-14s = $fgc%-10s$rs │ %-3s │ %-14s = $bgc%-10s$rs\n"  "tput setaf $i"  "Vordergrund" $i "tput setab $i" "Hintergrund" 
        ((i++))
    done | LESS="-P ""$less_prompt""$" less -R 
    
    [[ $ckwin_status =~ -u ]] || shopt -u checkwinsize
    
}


read_data_into_tempfile(){
    if [[ $# = 0 ]]; then
        while IFS= read -r line; do
            printf '%s\n' "$line" >> $tempfile
        done <&0
    else
        this_file=$( get_absolute_pathname $1 ) 
        [[ -r $this_file ]]                   \
             || { msg_fail $(gettext "No file given. Nothing to do. STOP")
                  exit 1 ; }
        cat $this_file >> $tempfile
    fi
}


check_updates(){
    msg_dialog_new_commandtable_available=$(gettext "New commands are available. Downloading new command table.")
    msg_dialog_new_commandtable_done=$(gettext "New command table installed. Will be used from now on.")
    msg_dialog_new_version_available=$(gettext "Found new version of \"%s \"")
    #i18n: the answer to this question will be added from another place
    # it is a Yes or No question. Default is Y  and the displeyed choices are  [Y|n]
    msg_dialog_ask_download=$(gettext "Do You want to download and use new version?")
    msg_updating_script=$( gettext "updating script" )
    msg_script_is_up_to_date=$( gettext "paster is up to date. version:")
    msg_server_version_is_older=$(gettext "The installed version of paster is newer than the server reports it. This is somewhat strange and should not happen.")
    #i18n: This is the answer to Yes or No questions.
    # Default is uppercases.
    msg_answer_choice_Yes_no=$( gettext "[Y|n]")
    #i18n: this is the YES reply char to a Yes or No question
    # it is case insensitive
    msg_answer_Yes_reply=$( gettext "y" )
    #i18n: this is the NO reply char to a Yes or No question
    # it is case insensitive
    msg_answer_No_reply=$( gettext "n" )
    #i18n: a wrong key typed. did not match any of vaild choices
    msg_wrong_answer_given=$( gettext "wrong answer given. Try again.")
    
    msg_ask_download_script=$( gettext "a newer script is available. Do you want to download it now?")
    #i18n: %s is substituted with the localized date/time string.
    # set in at an appropriate place 
    msg_ask_download_commands=$( gettext " a newer commands file from %s is available")
 
    server_version_script="$( curl -k -L $url_version_paster 2>/dev/null)"
    sver=( ${server_version_script//./ }  )
    sver=$(( ${sver[0]}*1000000000 + ${sver[1]}*1000000 + ${sver[2]}))

    #TODO check local /etc AND $HOME version
    ver=( ${version//./ }  )
    ver=$(( ${ver[0]}*1000000000 + ${ver[1]}*1000000 + ${ver[2]} ))
    if (( sver > ver )); then
        printf "$msg_dialog_new_version_available %s\n" $server_version_script
        printf "$msg_dialog_ask_download $msg_answer_choice_Yes_no : "
        while true; do
            read -n 1
            printf "\n"
            if [[ ${REPLY^^} == ${msg_answer_Yes_reply^^} || x$REPLY == "x" ]] ; then  
                printf "$msg_updating_script\n"
                update_script
                break
            elif  [[ ${REPLY^^} == ${msg_answer_No_reply^^}  ]] ; then  
                echo doing nothing
                break            
            else
                msg "\n$msg_wrong_answer_given\n"
                msg "$msg_dialog_ask_download $msg_answer_choice_Yes_no : "
            fi
        done
        
    elif (( sver == ver )); then
        msg_info  "$msg_script_is_up_to_date\n"
    else
        msg_info  "$msg_server_version_is_older\n"
    fi

    # UPDATE-CHECK  COMMAND-TABLE
    [[ -r $sys_cmds_pathname ]] && sys_cmd_tbl_timestamp=$( sed -rn 's/^timestamp:[[:blank:]]*([[:digit:]]+)/\1/p' $sys_cmds_pathname )
    [[ -r $cmds_pathname ]] && cmd_tbl_timestamp=$( sed -rn 's/^timestamp:[[:blank:]]*([[:digit:]]+)/\1/p' $cmds_pathname )
    
    #TODO check local /etc AND $HOME
    local_cmds_version=$cmd_tbl_timestamp
    server_cmds_version="$( curl -k -L $url_version_cmds 2>/dev/null )"
    if ((local_cmds_version < server_cmds_version )); then
        # 
        case $auto_update in
            ask|auto )
                printf $msg_ask_download_commands  $( date -d @${local_cmds_version// } +"%x %X" )
                printf "$( gettext "found server version from : %s") $( date -d @${server_cmds_version// } +"%x %X" )"
                curl -k -L $url_download_cmds > $cmds_pathname 
                ret=$?  
                echo returncode : $ret
                ;;
            silent )
                curl -k -L $url_version_cmds > $cmds_pathname 2>/dev/null
                ;;
        esac
    #else
    fi
}

update_script(){
    if [[ -w $0 ]] ; then
        echo do update
    else
        echo new name needed
    fi
}


#check_updates

#i18n: 89 entries describing all error messages of "curl"
#      at this time (2019 January) we use HTTPS PUT/GET/POST only.
#      Thus FTP can be ignored ro the time being"
#i18n: curl message
curl_errors[0]=$( gettext "OK" )
#i18n: curl message
curl_errors[1]=$( gettext "Unsupported protocol. This build of curl has no support for this protocol.")
#i18n: curl message
curl_errors[2]=$( gettext "Failed to initialize.")
#i18n: curl message
curl_errors[3]=$( gettext "URL malformed. The syntax was not correct.")
#i18n: curl message
curl_errors[4]=$( gettext "A feature or option that was needed to perform the desired request was not enabled or was explicitly disabled at build-time.")
#i18n: curl message
curl_errors[5]=$( gettext "Couldn't resolve proxy. The given proxy host could not be resolved.")
#i18n: curl message
curl_errors[6]=$( gettext "Couldn't resolve host. The given remote host was not resolved.")
#i18n: curl message
curl_errors[7]=$( gettext "Failed to connect to host.")
#i18n: curl message
curl_errors[8]=$( gettext "FTP weird server reply. The server sent data curl couldn't parse.")
#i18n: curl message
curl_errors[9]=$( gettext "FTP  access  denied.  The  server denied login or denied access to the particular resource or directory you wanted to reach.")
#i18n: curl message
curl_errors[11]=$( gettext "FTP weird PASS reply. Curl couldn't parse the reply sent to the PASS request.")
#i18n: curl message
curl_errors[13]=$( gettext "FTP weird PASV reply, Curl couldn't parse the reply sent to the PASV request.")
#i18n: curl message
curl_errors[14]=$( gettext "FTP weird 227 format. Curl couldn't parse the 227-line the server sent.")
#i18n: curl message
curl_errors[15]=$( gettext "FTP can't get host. Couldn't resolve the host IP we got in the 227-line.")
#i18n: curl message
curl_errors[17]=$( gettext "FTP couldn't set binary. Couldn't change transfer method to binary.")
#i18n: curl message
curl_errors[18]=$( gettext "Partial file. Only a part of the file was transferred.")
#i18n: curl message
curl_errors[19]=$( gettext "FTP couldn't download/access the given file, the RETR (or similar) command failed.")
#i18n: curl message
curl_errors[21]=$( gettext "FTP quote error. A quote command returned error from the server.")
#i18n: curl message
curl_errors[22]=$( gettext "HTTP page not retrieved. The requested url was not found or returned another error with the HTTP error  code  being  400  or")
#i18n: curl message
curl_errors[23]=$( gettext "Write error. Curl couldn't write data to a local filesystem or similar.")
#i18n: curl message
curl_errors[25]=$( gettext "FTP couldn't STOR file. The server denied the STOR operation, used for FTP uploading.")
#i18n: curl message
curl_errors[26]=$( gettext "Read error. Various reading problems.")
#i18n: curl message
curl_errors[27]=$( gettext "Out of memory. A memory allocation request failed.")
#i18n: curl message
curl_errors[28]=$( gettext "Operation timeout. The specified time-out period was reached according to the conditions.")
#i18n: curl message
curl_errors[30]=$( gettext "FTP  PORT  failed.  The  PORT  command failed. Not all FTP servers support the PORT command, try doing a transfer using PASV")
#i18n: curl message
curl_errors[31]=$( gettext "FTP couldn't use REST. The REST command failed. This command is used for resumed FTP transfers.")
#i18n: curl message
curl_errors[33]=$( gettext "HTTP range error. The range "command" didn't work.")
#i18n: curl message
curl_errors[34]=$( gettext "HTTP post error. Internal post-request generation error.")
#i18n: curl message
curl_errors[35]=$( gettext "SSL connect error. The SSL handshaking failed.")
#i18n: curl message
curl_errors[36]=$( gettext "FTP bad download resume. Couldn't continue an earlier aborted download.")
#i18n: curl message
curl_errors[37]=$( gettext "FILE couldn't read file. Failed to open the file. Permissions?")
#i18n: curl message
curl_errors[38]=$( gettext "LDAP cannot bind. LDAP bind operation failed.")
#i18n: curl message
curl_errors[39]=$( gettext "LDAP search failed.")
#i18n: curl message
curl_errors[41]=$( gettext "Function not found. A required LDAP function was not found.")
#i18n: curl message
curl_errors[42]=$( gettext "Aborted by callback. An application told curl to abort the operation.")
#i18n: curl message
curl_errors[43]=$( gettext "Internal error. A function was called with a bad parameter.")
#i18n: curl message
curl_errors[45]=$( gettext "Interface error. A specified outgoing interface could not be used.")
#i18n: curl message
curl_errors[47]=$( gettext "Too many redirects. When following redirects, curl hit the maximum amount.")
#i18n: curl message
curl_errors[48]=$( gettext "Unknown option specified to libcurl. This indicates that you passed a weird option to curl that was passed on to libcurl and")
#i18n: curl message
curl_errors[49]=$( gettext "Malformed telnet option.")
#i18n: curl message
curl_errors[51]=$( gettext "The peer's SSL certificate or SSH MD5 fingerprint was not OK.")
#i18n: curl message
curl_errors[52]=$( gettext "The server didn't reply anything, which here is considered an error.")
#i18n: curl message
curl_errors[53]=$( gettext "SSL crypto engine not found.")
#i18n: curl message
curl_errors[54]=$( gettext "Cannot set SSL crypto engine as default.")
#i18n: curl message
curl_errors[55]=$( gettext "Failed sending network data.")
#i18n: curl message
curl_errors[56]=$( gettext "Failure in receiving network data.")
#i18n: curl message
curl_errors[58]=$( gettext "Problem with the local certificate.")
#i18n: curl message
curl_errors[59]=$( gettext "Couldn't use specified SSL cipher.")
#i18n: curl message
curl_errors[60]=$( gettext "Peer certificate cannot be authenticated with known CA certificates.")
#i18n: curl message
curl_errors[61]=$( gettext "Unrecognized transfer encoding.")
#i18n: curl message
curl_errors[62]=$( gettext "Invalid LDAP URL.")
#i18n: curl message
curl_errors[63]=$( gettext "Maximum file size exceeded.")
#i18n: curl message
curl_errors[64]=$( gettext "Requested FTP SSL level failed.")
#i18n: curl message
curl_errors[65]=$( gettext "Sending the data requires a rewind that failed.")
#i18n: curl message
curl_errors[66]=$( gettext "Failed to initialise SSL Engine.")
#i18n: curl message
curl_errors[67]=$( gettext "The user name, password, or similar was not accepted and curl failed to log in.")
#i18n: curl message
curl_errors[68]=$( gettext "File not found on TFTP server.")
#i18n: curl message
curl_errors[69]=$( gettext "Permission problem on TFTP server.")
#i18n: curl message
curl_errors[70]=$( gettext "Out of disk space on TFTP server.")
#i18n: curl message
curl_errors[71]=$( gettext "Illegal TFTP operation.")
#i18n: curl message
curl_errors[72]=$( gettext "Unknown TFTP transfer ID.")
#i18n: curl message
curl_errors[73]=$( gettext "File already exists (TFTP).")
#i18n: curl message
curl_errors[74]=$( gettext "No such user (TFTP).")
#i18n: curl message
curl_errors[75]=$( gettext "Character conversion failed.")
#i18n: curl message
curl_errors[76]=$( gettext "Character conversion functions required.")
#i18n: curl message
curl_errors[77]=$( gettext "Problem with reading the SSL CA cert (path? access rights?).")
#i18n: curl message
curl_errors[78]=$( gettext "The resource referenced in the URL does not exist.")
#i18n: curl message
curl_errors[79]=$( gettext "An unspecified error occurred during the SSH session.")
#i18n: curl message
curl_errors[80]=$( gettext "Failed to shut down the SSL connection.")
#i18n: curl message
curl_errors[82]=$( gettext "Could not load CRL file, missing or wrong format (added in 7.19.0).")
#i18n: curl message
curl_errors[83]=$( gettext "Issuer check failed (added in 7.19.0).")
#i18n: curl message
curl_errors[84]=$( gettext "The FTP PRET command failed")
#i18n: curl message
curl_errors[85]=$( gettext "RTSP: mismatch of CSeq numbers")
#i18n: curl message
curl_errors[86]=$( gettext "RTSP: mismatch of Session Identifiers")
#i18n: curl message
curl_errors[87]=$( gettext "unable to parse FTP file list")
#i18n: curl message
curl_errors[88]=$( gettext "FTP chunk callback reported error")
#i18n: curl message
curl_errors[89]=$( gettext "No connection available, the session will be queued")

# complete write-out string of curl command
# gives some insight on what happend communicating with a server
curl_writeout_string='#####CURL STATS#####\ncontent_type=%{content_type}\nfilename_effective=%{filename_effective}\nftp_entry_path=%{ftp_entry_path}\nhttp_code=%{http_code}\nhttp_connect=%{http_connect}\nlocal_ip=%{local_ip}\nlocal_port=%{local_port}\nnum_connects=%{num_connects}\nnum_redirects=%{num_redirects}\nredirect_url=%{redirect_url}\nremote_ip=%{remote_ip}\nremote_port=%{remote_port}\nsize_download=%{size_download}\nsize_header=%{size_header}\nsize_request=%{size_request}\nsize_upload=%{size_upload}\nspeed_download=%{speed_download}\nspeed_upload=%{speed_upload}\nssl_verify_result=%{ssl_verify_result}\ntime_appconnect=%{time_appconnect}\ntime_connect=%{time_connect}\ntime_namelookup=%{time_namelookup}\ntime_pretransfer=%{time_pretransfer}\ntime_redirect=%{time_redirect}\ntime_starttransfer=%{time_starttransfer}\ntime_total=%{time_total}\nurl_effective=%{url_effective}\n'


PUT_paste_to_webserver(){
    curl -k -i  -T  $tempfile $put_url/  > /dev/null 2>/dev/null
    ret=$?
    if ((ret!=0)); then
        msg_fail $(gettext "Putting file on server failed!")
        msg_fail $(gettext "Server (or any box inbetween) responded:")
        msg_fail ${curl_errors[ret]}
        exit 1
    else
        paste_url=$(curl  -k -L  -H  'Content-Type:application/x-www-form-urlencoded' \
  $url_check_paste/$(basename $tempfile)  -d "user=$username;title=$title;file=${file_to_paste:-$paste_file}") # 2>result ) #/dev/null )
        ret=$?
        if (( $( echo $paste_url | wc -l) > 1 )) ; then
            # something wrong with curl putcheck
            cat $paste_url > error.html
            printf "  $bold$red $(gettext "Something went wrong pasting to the server.") $reset\n"
            printf "  $bold$green $(gettext "Read message from server.") $reset\n"
            xdg-open error.html
            sleep 2
            rm error.html
            exit
        fi
        if ((ret!=0)); then
            msg_fail $(gettext "Getting new URL for pasted file failed!")
            msg_fail $(gettext "Server (or any box inbetween) responded:")
            msg_fail ${curl_errors[ret]}
        fi
    fi
    2>/dev/null xclip -sel clip <<<$paste_url
    inClipboard=$?
    msg_ok  $( gettext "Your paste is available at" ) "$yellow$paste_url$reset"
    ((inClipboard==0)) &&  msg_ok  $( gettext "( URL is available from clipboard too.)" ) 
}



read_data_into_tempfile(){
    if [[ $# = 0 ]]; then
        while IFS= read -r line; do
            printf '%s\n' "$line" >> $tempfile
        done <&0
    else
        this_file=$( get_absolute_pathname $1 ) 
        [[ -r $this_file ]]                   \
             || { msg_fail $(gettext "No file given. Nothing to do. STOP")
                  exit 1 ; }
        cat $this_file >> $tempfile
    fi
}


copy_tempfile_into_clipboard(){
    msg_info_paste_into_clipboard=$"Pasting output into clipboard from temp file "
    msg_info_success_paste_to_clipboard=$"Contents successfully pasted to clipboard."
    msg_info_fail_paste_to_clipboard=$"Could not paste to clipboard. It seems that there is no display of a X-server associated with this terminal."
    xclip -sel clip < $tempfile 2>/dev/null
    rc=$?
    (( $rc == 0 )) && msg_ok $msg_info_success_paste_to_clipboard  || msg_fail $msg_info_fail_paste_to_clipboard
}


indent_long_msg(){
    echo $* |  fold -s -w $msg_width | while read line; do
                                        printf "$msg_indent$line\n"
                                       done
}


get_command(){
    # get_command <command-table-name>
    # 
    # this function is part of \"paster\"
    #
    # it reads a command from \"$command_table\" and prints it out
    # for further information see \"man command-table\"
    #
    # Param: \$1 == CommandName corresponding to a section header
    #               in \"paster\"s \"command-table\"
    #
    sed -rn '/^#/d;
             /^$/d;
             /^\[[[:blank:]]*'"$1"'[[:blank:]]*\]/,/^\[[[:blank:]]*.+[[:blank:]]*\]/{
                /^Command:/,/^\[[[:blank:]]*.+[[:blank:]]*\]/{
                    s/^Command://
                    /^\[/d 
                    /^$/d
                    p
                 }
            }
            ' $cmds_pathname
}

get_command_description(){
    echo in command_descript
    sed -rn '/^#/d;
             /^$/d;
             /^\[[[:blank:]]*'"$1"'[[:blank:]]*\]/,/^\[[[:blank:]]*.+[[:blank:]]*\]/{
                /^Description:/,/^Command:/{
                    s/^Description:[[:blank:]]*//
                    /^Command:/d
                    /^$/d
                    p
                }
             }
            ' $cmds_pathname 
}


get_programs_needed(){
    sed -rn '/^#/d;
             /^$/d;
             /^\[[[:blank:]]*'"$1"'[[:blank:]]*\]/,/^\[[[:blank:]]*.+[[:blank:]]*\]/{
                /^CommandsUsed:/,/^Description:/{
                    s/^CommandsUsed:[[:blank:]]*//
                    /^Description/d
                    /^$/d
                    p
                }
             }
            ' $cmds_pathname
}

execute_command(){
    get_command "$1" > $temp_command
    chmod +x $temp_command
    msg_ok $(printf "executing $yellow%s$reset from command-table" "$1"  )
    bash $temp_command | $0
    exit $?
}

get_absolute_pathname(){
    if (($# < 1 )); then
        msg_fail $(gettext "No file given.")
    else
        file="${1##*/}"
        dir="${1%/*}"
        original_dir="$dir"
        while true; do
            if [[ -x "$dir" && -r "$dir/$file" ]] ; then
                [[ -r "$dir/$file" ]] && { printf "%s\n" "$dir/$file" ; return 3; }
            else
                dir="${dir%/*}"                
            fi
            [[ "$dir" == "" ]] && {
                    #i18n: function walks an entire path to find a given file.
                    # first %s is placeholder for the file, second for path  
                    msg_fail $(printf "$( gettext "could not find file %s in entire path %s")\n"\
                               "$yellow$file$red" "$yellow$original_dir$reset" )
                    exit ; 
                    }
        done
    fi
}




#TODO check /usr/shar
declare -xr TEXTDOMAIN=paster
declare -xr TEXTDOMAINDIR=$user_conf_dir/locales


clip_and_paste(){
    declare -x tempfile=$(mkTempFile)
    if [[ $paste_file_given == "true" ]]; then
        read_data_into_tempfile < <(cat $file_to_paste)
    else
        read_data_into_tempfile $*
    fi
    copy_tempfile_into_clipboard
    PUT_paste_to_webserver
}

clip_only(){
    declare -x tempfile=$(mkTempFile)
    shift
    read_data_into_tempfile $*
    copy_tempfile_into_clipboard
}

paste_only(){
    declare -x tempfile=$(mkTempFile)
    shift
    read_data_into_tempfile $*
    PUT_paste_to_webserver
}


usage(){
    printf "\n"
    printf "   ${bold}paster${reset} is a little tool to paste any content onto a paste server.\n"
    printf "   \n"
    printf "   It reads from a pipe or a file if -f is given.\n"
    printf "   The output is copied to the clipboard and to the server.\n"
    printf "   The resulting URL from the paste operation is copied\n"
    printf "   to the clipboard as well\n"
    printf "   URL of server is ${bold}https://paste.interhacktive.de$reset\n"
    printf "\n   command [ | command ]... ${bold}| paster  [-t|--title <title>] [-u|--user <user name>] $reset" 
    printf "\n${bold}   paster -f ${ul}FILE${ul_end} [-t|--title <title>] [-u|--user <user name>]$reset\n" 
    printf "   \n"
    printf "     -f|--file    path/to/${ul}FILE${ul_end}$reset\n"
    printf "     -h|--help    display this help\n"
    printf "     -t|--title   a title to ease finding paste in listing on server\n"
    printf "     -u|--user    username, same like title \n\n"

}

write_json_data(){
    json_file=$(mkTempFile)
    cat > $json_file <<-EOF
'{ "user":\""$username"\","title":\""$title"\","file":\""$paste_file"\" }'
EOF
}

while (($# >=0  )); do
    case ${1} in
        -h|--help   )
            usage
            exit 0; 
          ;;
        -f|--file )
            paste_file_given="true"
            paste_file="$2"
            file_to_paste="$2"            
            shift 2
         
            ;;            
        -t|--title )
            title="$2"
            shift 2
            ;;
        -u|--user )
            username=$2
            shift 2
            ;;
        * )
            paste_file="tempfile"
            clip_and_paste $*
            exit 0
    esac
done

