#!/bin/sh
XAPPDEFS="/usr/share/X11/app-defaults"
BINDIR="/usr/bin"
LIMITVAL="3000"
echo ""
echo "tgif-reconf"
echo ""
echo "This serves to reconfigure tgif in case you changed the display resolution"
echo "or switch to another monitor that comes with a higher / different resolution."
echo ""
echo "Please note that this script reconfigures the geometry of tgif according to"
echo "data derived from your system setup".
echo "If you need an individual configuration it is highly recommended to enter the"
echo "corresponding values in ~/.Xdefaults-<HOSTNAME>, the value for <HOSTNAME> can"
echo "easily determined via"
echo ""
echo "echo \$HOSTNAME"
echo ""
echo "in a terminal. If you want your own geometry specification please edit your local"
echo ".Xdefaults-<HOSTNAME> and enter a line such as:"
echo ""
echo "Tgif.Geometry:			1000x1000+0+0"
echo ""
echo "what will start tgif with a size of 1000 x 1000 at a position 0 / 0, i. e. on the"
echo "upper left corner of your display independent of the settings created by tgif-reconf."
echo "The settings determined by tgif-reconf get written into:"
echo ""
echo "/usr/share/X11/app-defaults/Tgif"
echo ""
echo "Moreover the binary in use might be changed depending on the actual resolution of the"
echo "display. This can improve the usability on lowres and/or highres displays significantly"
echo "If you are not sure what to do simply reinstall tgif via zypper and / or yast2"
echo ""
echo -n "Please enter y (Y) to continue, anything else to quit: "
echo -ne "N"'\b'
read DOIT
if test -z "${DOIT}"; then
	DOIT=N
fi

if [[ "Y" != "${DOIT}" && "y" != "${DOIT}" ]]; then
	echo ""
	echo "Ok, I quit!"
	exit 0
fi
echo ""

WHOAMI=`whoami`

if [ "root" != "${WHOAMI}" ]; then
	echo "You have to be root to use this script!"
	echo "I quit now"
	echo ""
	exit 1
fi

if [[ ! -f "${BINDIR}/tgif.Stdres" || ! -f "${XAPPDEFS}/Tgif.Stdres" || ! -f "${BINDIR}/tgif.Highres" || ! -f "${XAPPDEFS}/Tgif.Stdres" ]]; then
	echo "Seems as if this installation differs from what I would expect!"
	echo "Files that are required are not in place."
	echo "You should have kept all files from the original installation in place for this to work."
	echo "Please check if your installation stems from openSUSE of 2021"
	echo ""
	echo "Therefore I quit!"
	exit 1
fi

VAL=`xrandr 2>/dev/null | sed '/\ connected primary/!d; s/^.*primary\ //g;s/+.*//g'`
XVAL=`echo ${VAL} | sed 's/x.*//g'`
YVAL=`echo ${VAL} | sed 's/.*x//g'`
# Check whether the result is a number, if not, assign 0 to avoid trouble ...
case ${XVAL} in
    ''|*[!0-9]*) XVAL=0 ;;
    *) ;;
esac
case ${YVAL} in
    ''|*[!0-9]*) YVAL=0 ;;
    *) ;;
esac

# Check whether the link to Tgif already exists in xappdefs ...
if [ -f ${XAPPDEFS}/Tgif ]; then
	rm -f ${XAPPDEFS}/Tgif
fi
# ${LIMITVAL} has been chosen as arbitrary limit, might depend on individual taste ...
echo -n "Adjusting links in ${XAPPDEFS} and ${BINDIR} ..."
if [ "${XVAL}" -gt "${LIMITVAL}" ]; then 
	echo -n "... linking to high resolution variant ..."
        (cd ${XAPPDEFS}; ln -s Tgif.Highres Tgif)
        (cd ${BINDIR}; test -f tgif && rm -f tgif; ln -s tgif.Highres tgif)
else
	echo -n "... linking to standard resolution variant ..."
        (cd ${XAPPDEFS}; ln -s Tgif.Stdres Tgif)
        (cd ${BINDIR}; test -f tgif && rm -f tgif; ln -s tgif.Stdres tgif)
fi
echo "... done"
echo ""
# Ok. Now if the values are meaningful let us preset a resolution that might fit to the current setup.
# User might modify accordingly at any time he likes to.

echo -n "Adjusting geometry information in ${XAPPDEFS}/Tgif ..."

if [[ "${XVAL}" != "0" && "${YVAL}" != "0" ]]; then
	# I initially wanted to use 0.85 but 0.84 provides smaller int-multiplication values ....
        # 0.84=84/100=21/25 ....
        NEWXVAL=$[${XVAL}*21/25]
        NEWYVAL=$[${YVAL}*21/25]
        XOFFS=$[(${XVAL} - ${NEWXVAL})/2]
        YOFFS=$[(${YVAL} - ${NEWYVAL})/2]
	GEOMETRY="${NEWXVAL}x${NEWYVAL}+${XOFFS}+${YOFFS}"
	TGIFGEOMETRY="Tgif.Geometry:			${GEOMETRY}"
	# sed -i converts the link to a regular file -> this should not happen!
	if [ "${XVAL}" -gt "${LIMITVAL}" ]; then 
	        echo -n "... high resolution variant ..."
		sed -i "s/^Tgif.Geometry.*/${TGIFGEOMETRY}/" ${XAPPDEFS}/Tgif.Highres
	else
	        echo -n "... standard resolution variant ..."
		sed -i "s/^Tgif.Geometry.*/${TGIFGEOMETRY}/" ${XAPPDEFS}/Tgif.Stdres
	fi
fi
echo "... done"
echo ""
echo "Please start tgif now. It should be nearly centered to the current screen."
echo ""
