#!/bin/bash
#
## RJP 19.2.2025 Sript for making a custom resolution file
# Packages xcvt xrandr and yad must be installed
# 22.2.2025 Typos corrected from costom_resolution file
# 22.2.2025 x-terminal-emulator removed from /tmp/modeline command for getting the script working with other linux systems too
# 18.6.2026 Fixed dialogs and scripts
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=500 --text-align=center --center --title=$"Make resolution file " --text="Would you like to make custom resolution file?" --title="Make custom resolution file" --text="Would you like to make a custom resolution file?" --title="Make custom resolution file"
if [ $? = 0 ];
then
WIDHT=`yad --center --width=500 --height=100 --text-align=center --button=OK:0 --title=$"WIDHT" --text="PUT WIDHT TO THIS" --entry --entry-label=BOX --entry-text=""`
HEIGHT=`yad --center --width=500 --height=100 --text-align=center --button=OK:0 --title=$"HEIGHT" --text="PUT HEIGHT TO THIS" --entry --entry-label=BOX --entry-text=""`
else
exit 0
fi

if [ $? = 0 ];
then
yad --center --width=500 --height=100 --text-align=center --button=OK:0 --title=$"INFORMATION" --text="YOU SET: $WIDHT $HEIGHT"
/bin/bash -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 -i '1s/.//' /tmp/reso2
touch custom_resolution$time.sh
echo "#/bin/bash" > 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/reso2)"  >> 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 1
fi
fi
