#!/bin/bash

###########################################################################
#
#	Shell program to convert html files into text.
#
#	Copyright 2001, USM Bish <bish@nde.vsnl.net.in>.
#
#	This program is free software; you can redistribute it and/ or
#	modify it under the terms of the GNU General Public License as
#	published by the Free Software Foundation; either version 2 of
#	the License, or (at your option) any later version. 
#
#	This program is distributed in the hope that it will be useful
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY  or  FITNESS FOR A PARTICULAR PURPOSE. See the
#	GNU General Public License for more details.
#
#	Usage:
#
#		html2text [ filename.html ] [ filename.txt ]
#
#	Revisions:
#
#	11/10/2001	File created       .......... ver 0.1
#
###########################################################################


###########################################################################
#	Constants
###########################################################################

PROGNAME=$(basename $0)
VERSION="0.1"
TEMP_FILE=/tmp/${PROGNAME}.$$

###########################################################################
#	Functions
###########################################################################


function clean_up
{
	rm -f ${TEMP_FILE}
}


function graceful_exit
{
	clean_up
	exit
}

function term_exit
{
	echo "${PROGNAME}: Terminated"
	clean_up
	exit
}


function int_exit
{
	echo "${PROGNAME}: Aborted by user"
	clean_up
	exit
}


function usage
{
	echo "Usage: ${PROGNAME} [filename.txt] [filename.html]"
}


###########################################################################
#	Program starts here
###########################################################################

# Trap TERM, HUP, and INT signals and properly exit

trap term_exit TERM HUP
trap int_exit INT

if ! [ $2 ]; then
   usage
   term_exit
fi
   
if ! [ -s $1 ]; then
   echo "ERROR : File not found - "$1
   usage
   term_exit
fi

#######################################
lynx -dump $1 > $TEMP_FILE
#######################################
   
if [ -s $2 ]; then
   echo "ERROR : File exists - "$2
   echo -en "Over write ? [y/n] .. "
   read YN
   case $YN in

   y|Y ) cat $TEMP_FILE > $2
         ;;

   * )   echo -en "Enter new Filename : "
         read FYLE
         cat $TEMP_FILE > $FYLE
   esac 

else
 
   cat $TEMP_FILE > $2
             
fi

graceful_exit

