#!/bin/sh

TOUCH=${TOUCH:-%TOUCH%}
MKDIR=${MKDIR:-"%MKDIR%"}

infodir=${INFODIR:-%INFODIR%}
infofile=${INFOFILE:-%INFOFILE%}
elispdir=${ELISPDIR:-%ELISPDIR%}
addir=${ADDIR:-%ADDIR%}

dirsection=${DIRSECTION:-%DIRSECTION%}

OptionStart="Emacs-w3 configuration options"
OptionEnd="End of Emacs-w3 configuration options"

if [ "X$2" = X"POST-INSTALL" ]; then
	if [ ! -f ${elispdir}/site-start.el ]; then 
		 ${TOUCH} ${elispdir}/site-start.el
	fi
	${MKDIR} ${addir}
	if [ ! -f ${addir}/Emacs ]; then 
		 ${TOUCH} ${addir}/Emacs
	fi
	if [ ! "`grep \"START-INFO-DIR-ENTRY\" ${infodir}/${infofile}`" ]; then
		echo "INFO-DIR-SECTION ${dirsection}" \
						>> ${infodir}/${infofile}
		echo "START-INFO-DIR-ENTRY"	>> ${infodir}/${infofile}
		echo "* W3: (`basename ${infofile} .info`).			WWW browser." \
						>> ${infodir}/${infofile}
		echo "END-INFO-DIR-ENTRY"	>> ${infodir}/${infofile}
	fi
	echo "Adding \"w3\" entry to ${infodir}/dir"
	install-info ${infodir}/${infofile} ${infodir}/dir
	sed -e "/^;;; ${OptionStart}/,/^;;; ${OptionEnd}/d" \
		${elispdir}/site-start.el > ${elispdir}/site-start.el.bak
	mv ${elispdir}/site-start.el.bak ${elispdir}/site-start.el
	echo "Adding \"w3\" entry to ${elispdir}/site-start.el"
	echo ";;; ${OptionStart}" \
						>> ${elispdir}/site-start.el
	echo "(setq load-path "			>> ${elispdir}/site-start.el
	echo "	(cons (expand-file-name " >> ${elispdir}/site-start.el
	echo "		\"${elispdir}/w3\") load-path))" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-preview-this-buffer \"w3\" \"WWW Previewer\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-follow-url-at-point" \
						>> ${elispdir}/site-start.el
	echo "	\"w3\" \"Find document at pt\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3 \"w3\" \"WWW Browser\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-open-local" >> ${elispdir}/site-start.el
	echo "	\"w3\" \"Open local file for WWW browsing\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-fetch"	>> ${elispdir}/site-start.el
	echo "	\"w3\" \"Open remote file for WWW browsing\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-use-hotlist" >> ${elispdir}/site-start.el
	echo "	\"w3\" \"Use shortcuts to view WWW docs\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-show-hotlist" >> ${elispdir}/site-start.el
	echo "	\"w3\" \"Use shortcuts to view WWW docs\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-follow-link" >> ${elispdir}/site-start.el
	echo "	\"w3\" \"Follow a hypertext link.\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'w3-batch-fetch" >> ${elispdir}/site-start.el
	echo "	\"w3\" \"Batch retrieval of URLs\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'url-get-url-at-point" >> ${elispdir}/site-start.el
	echo "	\"url\" \"Find the url under the cursor\" nil)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'url-file-attributes" \
						>> ${elispdir}/site-start.el
	echo "	\"url\" \"File attributes of a URL\" nil)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'url-popup-info"	>> ${elispdir}/site-start.el
	echo "	\"url\" \"Get info on a URL\" t)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'url-retrieve"		>> ${elispdir}/site-start.el
	echo "	\"url\" \"Retrieve a URL\" nil)" >> ${elispdir}/site-start.el
	echo "(autoload 'url-buffer-visiting" \
						>> ${elispdir}/site-start.el
	echo "	\"url\" \"Find buffer visiting a URL.\" nil)" \
						>> ${elispdir}/site-start.el
	echo "(autoload 'gopher-dispatch-object" \
						>> ${elispdir}/site-start.el
	echo "	\"gopher\" \"Fetch gopher dir\" t)" \
						>> ${elispdir}/site-start.el
	echo ";;; ${OptionEnd}" \
						 >> ${elispdir}/site-start.el
	if [ ! -f ${addir}/Emacs ]; then
		touch ${addir}/Emacs
	fi
	sed -e 's/^![ 	]*\(Emacs\*w3-.*\)$/&/' \
		-e "/^!!! ${OptionStart}/,/^!!! ${OptionEnd}/d" \
			${addir}/Emacs > ${addir}/Emacs.bak
	mv ${addir}/Emacs.bak ${addir}/Emacs
	echo "Adding \"w3\" entry to ${addir}/Emacs"
	sed -e 's/^[ 	]*Emacs\*w3-.*$/! &/'	${addir}/Emacs \
						> ${addir}/Emacs.bak
	echo "!!! ${OptionStart}"		>> ${addir}/Emacs.bak
	echo "Emacs*w3-node-style.attributeForeground: purple" \
						>> ${addir}/Emacs.bak
	echo "Emacs*w3-visited-node-style.attributeForeground: blue" \
						>> ${addir}/Emacs.bak
	echo "!!! End of ${OptionStart}"	>> ${addir}/Emacs.bak
	mv ${addir}/Emacs.bak ${addir}/Emacs
	exit 0
else
	exit 0
fi
