#!/bin/sh
#
## RJP 20.8.2025 Sript for making a custom resolution file in FreeBSD
# packages Yad, Xrandr and Cvt are required
#
time=$(date "+%F-%H-%M-%S")
xrandr | grep -m 1 "connected" > /tmp/connected0
cat /tmp/connected0 | awk '{print $1}' > /tmp/connected
RUUTU=$(cat /tmp/connected)
#
yad yad --width=700 --text-align=center --center --title="Make resolution file " --text="Would you like to make custom resolution file?" --title="Make custom resolution file" --text="Make custom resolution file" --title="Make custom resolution file"
WIDHT=`yad --center --width=500 --height=100 --text-align=center --title="WIDHT" --text="PUT WIDHT TO THIS" --entry --entry-label=BOX --entry-text=""`
HEIGHT=`yad --center --width=500 --height=100 --text-align=center --title="HEIGHT" --text="PUT HEIGHT TO THIS" --entry --entry-label=BOX --entry-text=""`
if [ $? = 0 ];
then
yad --center --width=500 --height=100 --text-align=center --title="INFORMATION" --text="YOU SET: $WIDHT $HEIGHT"
/bin/sh -c "cvt $WIDHT $HEIGHT > /tmp/modeline " && \
cat /tmp/modeline | grep Modeline > /tmp/modeline2
cat /tmp/modeline2 | awk '{print $2,$3,$4,$5,$6,$7,$8,$9,$10,$11}' > /tmp/newline0
cat /tmp/modeline2 | awk '{print $2}' > /tmp/reso
xrandr --newmode $(cat /tmp/newline0) 
xrandr --addmode $RUUTU $(cat /tmp/reso)
sed 's/_.*//' /tmp/reso > /tmp/reso2
sed 's/^.\{1\}//' /tmp/reso2 > /tmp/reso3
touch custom_resolution$time.sh
echo "#/bin/sh" > custom_resolution$time.sh 
echo "xrandr --newmode $(cat /tmp/newline0)" >> custom_resolution$time.sh
echo "xrandr --addmode $RUUTU $(cat /tmp/reso)" >> custom_resolution$time.sh
echo "xrandr -s $(cat /tmp/reso3)"  >> custom_resolution$time.sh
sleep 0,1
#
chmod +x custom_resolution$time.sh
#
sleep 0,1
# 
yad --width=200 --center --buttons-layout=center --borders=5 --text-align=center --title="READY" --text="Custon resolution set" --button=OK:1 --center             
#
# To test custom resolution
yad --width=700 --text-align=center --center --title=$"Test resolution" --text="Would you like to test custom resolution file for 5 seconds?" --title="Test custom resolution file"
if [ $? = 0 ];
then
./custom_resolution$time.sh && sleep 5 && xrandr -s 0
else exit 0
fi
fi

cd /tmp && rm connected connected0 modeline modeline2 newline0 reso reso2 reso3

