#!/bin/sh
# Super Simple Calculator by stemsee, ycalc.sh
# Copyright (C) 2025 stemsee <cou645@gmail.com/>
## gpl 3 license applies for personal unpaid uses,
# https://www.gnu.org/licenses/gpl-3.0.txt
# else a Unique Arbitrary License 
# must be obtained from the Copyright holder.
# Use at own risk. No liability accepted.

if [[ $(id -u) -ne 0 ]]; then
	[[ "$DISPLAY" ]] && exec gtksu "xsr.sh" "$0" "$@" || exec su -c "$0 $*"
fi

( for i in yad xdotool perl
do
[ -z "$(type -p $i)" ] && echo "$i"
done ) | yad --title="Missing Dependencies" --text-info --back=yellow --fore=red --fontname="sans bold 28" --center --width=200 --height=200 --timeout=1

if [[ -z "$(type -p yad)" ]]||[[ -z "$(type -p xdotool)" ]];then
	echo "Make sure both xdotool and yad are installed!"
	exit 1
fi

[ -f /tmp/test ] && rm -f /tmp/test
export var="$$"
[[ ! -p /tmp/calc-"$var" ]] && mkfifo /tmp/calc-"$var"
exec 3<>/tmp/calc-"$var"

function codefn {
		cat /tmp/usertxt > /tmp/usert
	[[ ! -z $(cat /tmp/comms) ]] && cat /tmp/comms | grep -E -v -e '^$' -e 'CPU ' -e '[0-9a-zA-Z]+[@]{1}.*[.com]{1}' | while read line
		do
		code=$(echo "$line" | cut -f2 -d'@')
		result="$($code)"
			perl -n -i -0777 -pe "s|$code|$result|s" /tmp/usert
			sed -i -E -e 's|,$||g' -e 's|@$||g' -e 's|^@||g' /tmp/usert
		done
		mv -f /tmp/usert /tmp/usertxt
		TXT=$(cat /tmp/usertxt)
		echo -e '\f' >/tmp/calc-"$var"
		printf "%s\n" "$TXT" | tee /tmp/MVIV >/tmp/calc-"$var"		
		[ -f /tmp/MVIII ] && sed -i -E 's|^$||g' /tmp/MVIII
};export -f codefn

function userfn {
		text=$(echo "$(xclip -o)")
		case "$text" in
		screenshot) IFS='+' read -r O P Q <<<"$(xrectsel)";sleep 0.2;ffmpeg -y -t 00:00:01 -f x11grab -re  -video_size "$O" -i :0.0+"$P","$Q" -vf scale=600:-1 /root/ycalc-"$DATE".png
		exit;;
		help) calfn help;exit;;
		esac
		echo "$text" >/tmp/usertxt
		# MATHS
		maths=$(grep -e '#,' /tmp/usertxt | awk -F'#' '{print $2}')
		echo -e "${maths}" | grep -E -e '[0-9]+' | while read line
		do 
	    IFS=',' read -r none one two three four five six seven<<<"$line"
		printf "%s\n%s\n%s\n%s\n%s\n%s\n" "$one" "$two" "$three" "$four" "$five" "$six" "$seven" | grep -v -e '@' | while read number
			do
				if [[ ! -z "${number}" ]]; then
				sum=$(echo "${number}" | sed -E -e 's| ||g' -e 's|÷|/|g' -e 's|x|*|g' -e 's|(sqrt){1}([0-9.]+)|\1(\2)|g' -e 's|([csal]){1}([0-9.]+)|\1(\2)|g')
					if [[ ! -z $(echo "${sum}" | grep -e '%') ]]; then
						string=$(echo "scale=0;${sum}" | bc)
					else
						string=$(echo "scale=7;${sum}" | bc -l)
					fi
				export string=$(echo "${string}" | sed -E -e '/^[.]{1}/s|^|0|g' -e '/^[-]{1}[!.]/s|-|-0|g' -e '/^[-]{1}[.]{1}/s|^[-]{1}[.]{1}|\-0\.|g')
						sed -i -e "/,${number}/s|,${number}|   (${number}) $sum = ${string} |" /tmp/usertxt
				[[ ! -z "$string" ]] && echo "(${number}) $sum = $string" >> /root/.ycalc.history
				fi
			done
		done
		sed -i -e '/#,/s/#,/ /g' -e 's/# / /g' /tmp/usertxt
		commands=$(cat /tmp/usertxt | grep '@' | sed 's|@,|@\n|g')
		echo "$commands" > /tmp/comms
		codefn
		[ -f /tmp/MVIII ] && sed -i -E 's|^$||g' /tmp/MVIII
}; export -f userfn

function memfn {
	CHOICE=$(yad --title="SSC - TEXT To Memory Bank" --item-separator="#" --form --field="Choose Memory Bank":CBE "MVI#MVII#MVIII#MVIV")
	ret=$?
	IFS='|' read -r MEM<<<"$CHOICE"
	cat /tmp/usertxt > /tmp/"$MEM"
};export -f memfn

function calfn {
case "$1" in

	help) echo "H E L P
	0) use 'x' not '*' and '÷' not '/' ... same for code or try escaping '\/' .
1) math must be preceded by a '#,' so #,4+5 (changed from '# ')
2) further math in the same line separated by ',' so #,4+5,6+7,9x8 - max seven
3) code format is '@code@,' these can be strung together.
4) code and math on the same line - @code@, #,,4+5,6+7,9x8 or #,4+5,6+7,9x8, @code@, @code@, #,,10x10,4^2 etc
5) a '#' must precede text following math. And better end math with a ','. Commas are field separators for sums.
							Sometimes two passes of calc are needed.
							Use at own risk. No liability accepted." | while read line
do
echo -e '\f' >/tmp/calc-"$var"
echo "$line" >> /tmp/calc-"$var"
sleep 4
done
echo -e '\f' >/tmp/calc-"$var"
exit;;
	MM) HISTORY="$(tac ~/.ycalc.history | tr '\n' '#')"
		CHOICES="$(yad --title="SSC - Load Values For Memory Buttons" --item-separator="#" --form --field="MI":CBE "#$HISTORY" --field="MII":CBE "#$HISTORY" --field="MIII":CBE "#$HISTORY" --field="MIIII":CBE "÷#$HISTORY")"
		ret=$?
		IFS='|' read -r MI MII MIII MIIII<<<"$CHOICES"
		if [[ "$ret" -eq 0 ]]; then
		 for i in MI MII MIII MIIII
		 do
			[[ ! -z "$(echo ${!i})" ]] && echo "${!i}" | cut -f2 -d'=' > /tmp/"$i"
		 done
		fi
		;;
   MI*) sort -u ~/.ycalc.history -o /tmp/ycalc.history; mv -f /tmp/ycalc.history /root/.ycalc.history
		calfn "$(cat /tmp/$1)";;
   clr) rm -f /tmp/test
   		echo -e '\f' > /tmp/calc-"$var"
		exit;;
	bs) sed -E -i 's/[0-9÷x+-=/b* %#expsqrtcoanilg^.\)\(]{1}$//g' /tmp/test
		echo -e '\f' >/tmp/calc-"$var"
		sum=$(cat /tmp/test)
		printf "%s\n" "$sum = $string" >/tmp/calc-"$var"
		exit;;
	=) 	sum=$(cat /tmp/test | sed -E -e 's| ||g' -e 's|(sqrt){1}([0-9.]+)|\1(\2)|g' -e 's|([csal]){1}([0-9.]+)|\1(\2)|g' -e 's|÷|/|g' -e 's|x|*|g')
		if [[ ! -z $(echo $sum | grep -e '%') ]]; then
			if [[ ! -z $(echo $sum | grep -E -e "[sqrtcsal\(\)]") ]]; then
				echo -e "Calculate Modulo in isolation first then use 'M+'.\nbc calculates modulo with zero decimal places.\nsine cos atan log will be inaccurate." | yad --text-info --no-buttons --timeout=4 --center --width=610 --height=120 --fontname="sans 18" --fore=red --back=yellow
			fi
		string=$(echo "scale=0;$sum" | bc -l)
		else
		string=$(echo "scale=11;$sum" | bc -l)
		fi
		export string=$(echo "$string" | sed -E -e '/^[.]{1}.*/s|^|0|g' -e '/^[-]{1}[!.]/s|-|-0|g' -e '/^[-]{1}[.]{1}/s|^[-]{1}[.]{1}|\-0\.|g')
		echo -e '\f' >/tmp/calc-"$var"
		printf "%s\n" "$sum = $string" >/tmp/calc-"$var"
		[[ ! -z "$string" ]] && echo "$sum = $string" >> /root/.ycalc.history;;
	*)  echo -ne "$1 " | tr '#' '%' >>/tmp/test
		echo -e '\f' >/tmp/calc-"$var"
		string=$(cat /tmp/test)
		printf "%s\n" "$string =" >/tmp/calc-"$var"
		exit;;
esac
};export -f calfn

case "$1" in
simp) yad --plug=$$ --tabnum=1 --text-info --editable --fontname="sans bold 24" --margins=10 --fore=lightblue --back=darkgreen --listen --wrap <&3  &
yad --plug=$$ --tabnum=2 --form --columns=5 --fontname="sans bold 24" --focus-field=20 --no-buttons \
--field='1':fbtn "bash -c 'calfn 1'" --field='4':fbtn "bash -c 'calfn 4'" --field='7':fbtn "bash -c 'calfn 7'" --field='+':fbtn "bash -c 'calfn \+'" \
--field='2':fbtn "bash -c 'calfn 2'" --field='5':fbtn "bash -c 'calfn 5'" --field='8':fbtn "bash -c 'calfn 8'" --field='0':fbtn "bash -c 'calfn 0'" \
--field='3':fbtn "bash -c 'calfn 3'" --field='6':fbtn "bash -c 'calfn 6'" --field='9':fbtn "bash -c 'calfn 9'" --field='-':fbtn "bash -c 'calfn -'" \
--field='÷':fbtn "bash -c 'calfn ÷'" --field='x':fbtn "bash -c 'calfn x'" --field='%':fbtn "bash -c 'calfn \#'" --field='.':fbtn "bash -c 'calfn \.'" \
--field="^":fbtn "bash -c 'calfn ^'" --field='Clr':fbtn "bash -c 'calfn clr'" --field='Del':fbtn "bash -c 'calfn bs'" --field='=':fbtn "bash -c 'calfn ='" &
yad --title="SSC" --paned --key=$$ --tab=sum --fontname="sans bold 24" --tab=pad --splitter=-40 --orient=vert --no-buttons --geometry="649x434+263+477" &
;;
vance|*) yad --plug=$$ --tabnum=1 --text-info --editable --fontname="sans bold 24" --margins=10 --fore=#6A6A6A --back=#EBEBEB --listen --wrap --file-op --enable-spell --show-uri --uri-handler="defaultbrowser" --uri-color=green <&3  &
yad --plug=$$ --tabnum=2 --form --columns=5 --fontname="sans bold 24" --focus-field=30 --no-buttons \
--field="<span fgcolor='green' font='sans bold 14'>sqrt</span>!!square root of following number sqrt 9":fbtn "bash -c 'calfn sqrt'" --field="<span fgcolor='blue' bgcolor='pink' font='sans bold 12'>M+</span>!!Choose which calculation results from history to store in memory M1-5":fbtn "bash -c 'calfn MM'" --field="<span fgcolor='purple' bgcolor='white' font='sans bold 14'>1</span>!!one":fbtn "bash -c 'calfn 1'" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>4</span>!!four":fbtn "bash -c 'calfn 4'" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>7</span>!!seven":fbtn "bash -c 'calfn 7'" --field="<span fgcolor='black' font='sans bold 14'>+</span>!!addition with the plus sign":fbtn "bash -c 'calfn \+'" \
--field="<span fgcolor='green' font='sans bold 14'>cos</span>!!cosine of following number":fbtn "bash -c 'calfn c'" --field="<span fgcolor='blue' font='sans bold 12'>M1</span>!!Press to paste stored result into calculation":fbtn "bash -c \"calfn MI \"" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>2</span>!!two":fbtn "bash -c 'calfn 2'" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>5</span>!!five":fbtn "bash -c 'calfn 5'" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>8</span>!!eight":fbtn "bash -c 'calfn 8'" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>0</span>!!zero":fbtn "bash -c 'calfn 0'" \
--field="<span fgcolor='green' font='sans bold 14'>sin</span>!!sine of following number":fbtn "bash -c 'calfn s'" --field="<span fgcolor='blue' font='sans bold 12'>M2</span>":fbtn "bash -c \"calfn MII \"" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>3</span>!!three":fbtn "bash -c 'calfn 3'" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>6</span>!!six":fbtn "bash -c 'calfn 6'" --field="<span fgcolor='purple' bgcolor='white'  font='sans bold 14'>9</span>!!nine":fbtn "bash -c 'calfn 9'" --field="<span fgcolor='black' font='sans bold 18'>-</span>!!subtraction with the minus symbol":fbtn "bash -c 'calfn -'" \
--field="<span fgcolor='green' font='sans bold 14'>atan</span>!!arc tangent of following number ":fbtn "bash -c 'calfn a'" --field="<span fgcolor='blue' font='sans bold 12'>M3</span>":fbtn "bash -c \"calfn MIII \"" --field="<span fgcolor='black' font='sans bold 16'>÷</span>!!division also /":fbtn "bash -c 'calfn ÷'" --field="<span fgcolor='black' font='sans bold 14'>x</span>!!multiplication 5x5=25, or *":fbtn "bash -c 'calfn x'" --field="<span fgcolor='black' font='sans bold 14'>%</span>!!Modulo - Remainder":fbtn "bash -c 'calfn \#'" --field="<span fgcolor='black' font='sans bold 14'>.</span>!!floating point for decimal numbers":fbtn "bash -c 'calfn \.'" \
--field="<span fgcolor='green' font='sans bold 14'>nlog</span>!!natural log of following number":fbtn "bash -c 'calfn l'" --field="<span fgcolor='blue' font='sans bold 12'>M4</span>":fbtn "bash -c \"calfn MIIII \"" --field="<span fgcolor='black' font='sans bold 14'>^</span>!!expoential - raise to the power ... 4^2=16":fbtn "bash -c 'calfn ^'" --field="<span fgcolor='yellow' bgcolor='red' font='sans bold 14'>Clr</span>!!Clears History":fbtn "bash -c 'calfn clr'" --field="<span fgcolor='red' bgcolor='yellow'  font='sans bold 14'>Del</span>!!BackSpace":fbtn "bash -c 'calfn bs'" --field="<span fgcolor='white'  bgcolor='purple' font='sans bold 18'>=</span>!!Equals - perform calculations":fbtn "bash -c 'calfn ='" &
yad --title="Super Simple Calculator" --paned --key=$$ --tab=sum --fontname="sans bold 24" --tab=pad --splitter=-400 --orient=vert --buttons-layout=center --button="<span fgcolor='#0F6EC9' font='sans bold 12'>MV</span>!!paste text":"bash -c \"cat /tmp/MV >> /tmp/calc-$var \"" --button="<span fgcolor='#0F6EC9' font='sans bold 12'>MVI</span>!!Paste text":"bash -c \"cat /tmp/MVI >> /tmp/calc-$var \"" --button="<span fgcolor='#0F6EC9' font='sans bold 12'>MVII</span>!!Paste text":"bash -c \"cat /tmp/MVII >> /tmp/calc-$var \"" \
--button="<span fgcolor='#0F6EC9' font='sans bold 12'>MVIII</span>!!usually stores multi-line results of code execution - click to paste text":"bash -c \"cat /tmp/MVIII >> /tmp/calc-$var \"" --button="<span fgcolor='#0F6EC9' font='sans bold 12'>MVIV</span>!!Always stores latest operation - click to paste text":"bash -c \"cat /tmp/MVIV >> /tmp/calc-$var \"" --button="<span fgcolor='pink' bgcolor='#0F6EC9' font='sans bold 12'>M+</span>!!Assign Text To Memory Bank":"bash -c 'memfn'" --button="<span fgcolor='white' bgcolor='darkgreen' font='sans bold 12'>Calc</span>!!After clicking this button click in the Viewer. This processes math and code among text.":"bash -c 'sleep 2; xdotool key ctrl+a;userfn'" --geometry="649x434+263+477" &
;;
esac
ret=$?
sleep 1
wait $!

case "$ret" in
*) rm -f /tmp/calc-"$var" 
rm -f /tmp/test 
;;
esac
