#! /bin/bash
#
# script to automaticly configure lynx to use gallery
#
# (cl) 1999,2000 Malekith
#
cfg=
search=
ch()
{
	if test x = x$1
	then
		return 1
	fi
	search="$search $1"
	if [ -f $1 ]
	then
		echo lynx.cfg found: $1
		cfg=$1
		return 0
	else
		return 1
	fi
}
ask()
{
	printf "%s (y/n) ? " "$*"
	read
	if test $REPLY = y -o $REPLY = yes
	then
		return 0
	elif test $REPLY = n -o $REPLY = no
	then
		return 1
	else
		ask "Answer: yes or no: "
	fi
}
add()
{
	echo "$*" >> $cfg
	printf "\033[0;36m%s\033[0m\n" "$*"
}

echo "This script will modify your lynx config file to display images with"
echo "gallery viewer under Linux console"
if ask "Do you want to continue"
then
	echo So I will...
else
	exit 1
fi

ch $LYNX_CFG || \
ch ~/lynx.cfg || \
ch /etc/lynx.cfg || \
ch /usr/lib/lynx.cfg || \
ch /usr/lib/lynx/lynx.cfg || \
ch /usr/local/lib/lynx.cfg || \
ch /usr/local/lib/lynx/lynx.cfg

if [ x = x$cfg ]
then
	echo "lynx.cfg hasn't been found! I chacked:"
	echo $search
	exit 1
fi

if grep 'VIEWER:image/jpeg:gallery' $cfg >& /dev/null
then
	echo Your lynx is already configured to use gallery
	exit 1
fi

printf "lines printed in \033[0;36mblue\033[0m are sent to file %s\n" "$cfg"
echo
echo "Now, I'll add SUFFIX: lines to lynx.cfg file. They specifies mime types"
echo "If you have mime.types specified in your lynx.cfg file you may ommmit"
echo "this, but there should be definitions for image types you want to view."
echo "Anyway, it won't harm to say 'yes'"
if ask "Add SUFFIX: lines"
then
	add "SUFFIX:.gif:image/gif"
	add "SUFFIX:.tiff:image/tiff"
	add "SUFFIX:.tif:image/tiff"
	add "SUFFIX:.jpg:image/jpeg"
	add "SUFFIX:.jpeg:image/jpeg"
	add "SUFFIX:.jpe:image/jpeg"
	add "SUFFIX:.xpm:image/x-xpixmap"
	add "SUFFIX:.png:image/png"
	add "SUFFIX:.bmp:image/bmp"
	add "SUFFIX:.pnm:image/x-portable-anymap"
	add "SUFFIX:.pbm:image/x-portable-bitmap"
	add "SUFFIX:.pgm:image/x-portable-graymap"
	add "SUFFIX:.ppm:image/x-portable-pixmap"
fi
echo
echo "Now I'll add lines specifing that images should be viewed with gallery under"
echo "linux console"
echo
add "VIEWER:image/jpeg:gallery %s:NON_XWINDOWS"
add "VIEWER:image/gif:gallery %s:NON_XWINDOWS"
add "VIEWER:image/png:gallery %s:NON_XWINDOWS"
add "VIEWER:image/bmp:gallery %s:NON_XWINDOWS"
add "VIEWER:image/x-xpixmap:gallery %s:NON_XWINDOWS"
add "VIEWER:image/x-tiff:gallery %s:NON_XWINDOWS"
add "VIEWER:image/tiff:gallery %s:NON_XWINDOWS"
add "VIEWER:image/x-portable-anymap:gallery %s:NON_XWINDOWS"
add "VIEWER:image/x-portable-bitmap:gallery %s:NON_XWINDOWS"
add "VIEWER:image/x-portable-graymap:gallery %s:NON_XWINDOWS"
add "VIEWER:image/x-portable-pixmap:gallery %s:NON_XWINDOWS"

if grep '^[[:space:]]*MAKE_LINKS_FOR_ALL_IMAGES' $cfg >& /dev/null
then
	echo
	echo "This is to tell lynx to display links to images"
	echo
	add "MAKE_LINKS_FOR_ALL_IMAGES:TRUE"
else
	if grep '^[[:space:]]*MAKE_LINKS_FOR_ALL_IMAGES:TRUE' $cfg >& /dev/null
	then
		echo
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo "WARNING: you have lynx set *NOT* to display links for all images"
		echo "This could be a problem"
		echo "you may want to replace line:"
		echo "MAKE_LINKS_FOR_ALL_IMAGES:<whatever>"
		echo "with"
		echo "MAKE_LINKS_FOR_ALL_IMAGES:TRUE"
		echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
		echo
	fi
fi

#echo
#echo "If your lynx displays black test on white background and you don't"
#echo "like it, I can fix it."
#if ask "Do you want me to change colours to white on black"
#then
#	add "COLOR:0:lightgray:black"
#	add "COLOR:1:brightblue:black"
#	add "COLOR:2:yellow:blue"
#	add "COLOR:3:brown:black"
#	add "COLOR:4:yellow:black"
#	add "COLOR:5:white:blue"
#	add "COLOR:6:red:black"
#	add "COLOR:7:magenta:cyan"
#fi
echo 
echo Your $cfg file has been updated.
echo
