#!/bin/sh
#author:Wos
#a script to use simpletranslate
#DOMAIN="https://pol1.sptl.ggtyler.dev/"
#TO_LANG="zh-CN"

DOMAIN="https://simplytranslate.leemoon.network/"
TO_LANG="Chinese+%28Simplified%29"
HTMP=/tmp/simpletranslate.html
TTMP=/tmp/1.txt

url="${DOMAIN}?engine=google"

function usage(){
    echo "usage:`basename $0` -show #display previous result"
    echo "     :`basename $0`       #translate content from clipboard"
    exit -1
}
if [[ "$1" == "-h" ]]; then
    usage
elif [[ "$1" == "-show" ]]; then
    surf -s -a a -i -d -t -g -n -x ${HTMP} 2>/dev/null &
    exit -1
fi

if ! command -v xclip &> /dev/null || ! command -v jq &> /dev/null || ! command -v curl &> /dev/null || ! command -v surf &> /dev/null; then
    echo "please install them first(xclip,jq,curl,surf)"
    exit 1
fi

xclip -selection clipboard -o > ${TTMP} 2>/dev/null || echo "There is no content in the clipboard. Please copy the string to be translated and run the script again."
xclip -selection clipboard /dev/null
if [[  ! -s ${TTMP} ]]; then
    exit -1 
fi

#Remove extra blank lines
sed -i '/./,/^$/!d;/^$/n;' ${TTMP}

cat <<BOF  > ${HTMP} 
<html><head><meta charset="UTF-8"/><style>body{font-size:2.8em;color:gold;background-color:black}pre{white-space: pre-wrap;word-wrap: break-word;}</style></head>
<body>
<pre>
BOF

#for one line long file format
if [ $(cat ${TTMP}|wc -l) -eq 0 ] && [ $(cat ${TTMP}|wc -c) -gt 5000 ]; then
    ST=1
    ED=666
    while :;
    do
        text=$(cat ${TTMP}|cut -d' ' -f ${ST}-${ED}|jq -s -R -r @uri)
        #post data format one
        #curl -XPOST -d "from=auto&to=${TO_LANG}&text=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>$//g' >> ${HTMP} 
        #post data format two 
        curl -XPOST -d "from_language=Autodetect&to_language="${TO_LANG}"&input=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>//g'|tr -d '\n' >> ${HTMP} 
	ST=$(($ED+1))
        ED=$(($ST+666))
        if [ ${ED} -ge $(cat ${TTMP}|wc -w) ]; then
            ED=$(cat ${TTMP}|wc -w)
            text=$(cat ${TTMP}|cut -d' ' -f ${ST}-${ED}|jq -s -R -r @uri)
            #post data format one
            #curl -XPOST -d "from=auto&to=${TO_LANG}&text=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>$//g' >> ${HTMP} 
            #post data format two 
            curl -XPOST -d "from_language=Autodetect&to_language="${TO_LANG}"&input=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>//g'|tr -d '\n' >> ${HTMP} 
	    break
        fi
        continue
    done
else
    ST=1
    ED=2
    _ED=0
    while :;
    do
        if [ $(sed -n "${ST},${ED}p" ${TTMP}|wc -c) -eq 0 ] && [ $ED -gt $(cat ${TTMP}|wc -l) ]; then
            break
        elif [ $(sed -n "${ST},${ED}p" ${TTMP}|wc -c) -le  4900 ] && [ $ED -gt $(cat ${TTMP}|wc -l) ]; then
            text=$(sed -n "${ST},${ED}p" ${TTMP}|jq -s -R -r @uri)
            #post data format one
            #curl -XPOST -d "from=auto&to=${TO_LANG}&text=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>$//g' >> ${HTMP} 
            #post data format two 
            curl -XPOST -d "from_language=Autodetect&to_language="${TO_LANG}"&input=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>//g' >> ${HTMP} 
            break
        elif [ $(sed -n "${ST},${ED}p" ${TTMP}|wc -c) -le  4900 ]; then
            ED=$(($ED+1))
            continue
        elif [ $(sed -n "${ST},${ED}p" ${TTMP}|wc -c) -gt 4900 ]; then
            _ED=$(($ED-1))
	    while :;
            do
	        line=$(sed -n "${_ED}p" ${TTMP})
                    if [ -z "$line" ]; then
                        break
                    elif [[ $line =~ [!??!,,.。\"\”\’\']$ ]]; then
                        break
                    else
                        _ED=$((${_ED}-1))
                        continue
                    fi
	    done
            text=$(sed -n "${ST},${_ED}p" ${TTMP}|jq -s -R -r @uri)
            #post data format one
            #curl -XPOST -d "from=auto&to=${TO_LANG}&text=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>$//g' >> ${HTMP} 
            #post data format two 
            curl -XPOST -d "from_language=Autodetect&to_language="${TO_LANG}"&input=${text}" -s "${url}"|grep -Pzo 'readonly>(?s:.*)</textarea>'|sed -e '1s/^[ \t]*readonly>//g' -e '$s/<\/textarea>//g' >> ${HTMP} 
            ST=$((${_ED}+1))
            ED=$(($ST+1))
            continue
        fi
    done
fi
cat <<BOF  >> ${HTMP} 
</pre>
</body></html>
BOF
#replace NULL string with new blank line
sed -i 's/\x0/\n\n/g' ${HTMP}
surf -s -a a -i -d -t -g -n -x ${HTMP} 2>/dev/null &