#!/bin/bash
#
# NewstoHOWTO: A skript to eliminate the headers from HOWTOs posted
# automatically by the Linux HOWTO coordinator to the Net News
# (Normally to comp.os.linux.answers). By this writing the Linux HOWTO 
# coordinator is Greg Hankins <gregh@sunsite.unc.edu>. The HOWTO's names are
# automatically gessed from within the News files and different parts of the
# same HOWTO that reside in different files will be put together in one
# HOWTO in their correct order.
#
# Syntax: NewstoHOWTO [-h] | [-c] [-q] [-d] files [directory]
#
# Dec 09, 1995 (c) Haykel Ben Jemia (Haykel@cs.tu-berlin.de)
# Jan 09, 1996 : Modified for cron usage [-c] by 
#                     Graham Biswell (graham@tarka.demon.co.uk)
# Apr 03, 1996 : Added * Verbose mode and [-q] to turn it off
#                      * Logfile
#                      * Intercept Ctrl-C and remove temp files before leaving
#                      * Put different parts of a HOWTO together in their CORRECT
#                        order
#

##### Begin of configuration section
#
# Temp dir where to put temporary files (it must have enogh palce for all
# the files that will be processed)
TMPDIR="/tmp"
#
# Program to compress files and extension of compressed files
COMPRESS="gzip -9"
EXT=".gz"
#
##### End of configuration section

# Version and Copyright
VERSION="NewstoHOWTO V.2.0 (C)opyright 1996 by Haykel Ben Jemia "
VERBOSE="yes"

# Names of created files in TMPDIR
TMPFILES=""

# Name of this program
PROGNAME=`basename $0`

# initialise CRONJOB params
CRONJOB=0
LASTRUN=""

# Usage !
Usage() {
echo $VERSION
if [ ! -z "$1" ]; then
  echo "$PROGNAME: $1"
fi
echo "Usage: $PROGNAME [-h] | [-c] [-q] [-d] files [directory]"
echo "Options:"
echo " -h : print this (h)elp and exit"
echo " -c : run as a (c)ronjob"
echo " -q : be (q)uiet, don't make any output to the stdout"
echo " -d : (d)on't compress the output (compressed by default)"
}

# Parse for options
while :; do
  case "$1" in
# Don't compress outputs (Pass them through 'cat')
    -d) COMPRESS="cat"; EXT=""; shift ;;
# Help !
    -h) Usage >&2
        exit 0 ;;
# be quiet
    -q) VERBOSE="no"; shift;;
# run as a cronjob
    -c) CRONJOB=1 ; shift ;;
# unknown options
    -*) Usage "unknown option: $1" >&2
        exit 1 ;;
# no more options
    *) break
  esac
done

# Check for arguments
if [ $# = 0 ]; then 
  Usage "Filenames missing !" >&2
  exit 1
fi

# Check TMPDIR (must be a dir, writable and readable !)
# eliminate last '/' from pathname
TMPDIR=`echo $TMPDIR | sed 's:/$::g'`
if [ ! -d $TMPDIR ]; then
  echo "$PROGNAME: temp directory $TMPDIR don't exist !"; exit 1;
fi
if [ ! -w $TMPDIR ]; then
  echo "$PROGNAME: can't write to temp directory $TMPDIR !"; exit 1;
fi
if [ ! -r $TMPDIR ]; then
  echo "$PROGNAME: can't read from temp directory $TMPDIR !"; exit 1;
fi

# If not quiet print Copyright
if [ $VERBOSE = "yes" ]; then
  echo $VERSION
fi

# If the last parameter is not a directory, the target will be set to the 
# current working directory
for FILE in $*; do
  TO=$FILE
done
# eliminate last '/' from pathname
TO=`echo $TO | sed 's:/$::g'`
if [ ! -d $TO ]; then
  TO=".";
fi
# Can I write in the target directory
if [ ! -w $TO ]; then
  echo "$PROGNAME: can't write to target directory $TO !"; exit 1;
fi

if [ $CRONJOB = 1 ] ; then
  LASTRUN="$TO/.newstohowto"
fi

# Intercept Ctrl-C and delte the temp files before exiting
break_exit()
{
  echo "Ctrl-C intercepted !"
  echo "Removing temporary files ..."
  rm -f $TMPDIR/*.$$
  echo "done."
  exit 1
}
trap break_exit 2

# Get out the names of the HOWTOs and put them in the TMPDIR
# with their part # in the extension (to have them in the wright
# order with 'ls').
if [ $VERBOSE = "yes" ]; then
  echo -ne "\nCreating temp files"
fi
for FILE in $*; do
# show that we are working
  if [ $VERBOSE = "yes" ]; then
    echo -n "." 
  fi
  if [ ! -r $FILE ]; then 
    echo -e "\n$PROGNAME: Warning: can't read file $FILE !" >&2
    continue;
  fi
  if [ ! -f $FILE ]; then continue; fi
  if [ $CRONJOB = 1 ] && [ -f $LASTRUN ]; then
    if [ $LASTRUN -nt $FILE ]; then continue ; fi
  fi
  if [ $FILE = $TO ]; then break; fi
  FILENAME=`grep 'BEGIN Linux' $FILE |\
            sed -e 's/- --- BEGIN Linux \(.*\) part \(.\).*/\1.\2/' -e 's/ /-/g'`
  FILENAME=`echo $FILENAME | sed 's/^\([^ ]*\) .*/\1/'`
  if [ -z "$FILENAME" ]; then
    continue
  fi
  sed "s:\(- --- BEGIN Linux\).*\(part .*\) .*$:\1 \2 <--> $FILE:" $FILE\
      >> $TMPDIR/$FILENAME.$$
  TMPFILES="$TMPFILES `echo $FILENAME | sed 's/^\(.*\)\..*/\1/g'`"
done

# Did we find some (new) HOWTO's ?
if [ -z "$TMPFILES" ]; then
  echo -e "\n$PROGNAME: No HOWTOs created !" >&2
  exit 0
fi

# For the Logfile
LOGFILE="$TO/NtH-`date +%d%b%Y`.log"
TODAYISTHEDAY=`date +'%a %d %b %Y'`
echo > $LOGFILE
echo "                     NewstoHOWTO, Logfile of $TODAYISTHEDAY" >> $LOGFILE
echo "                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" >> $LOGFILE
echo >> $LOGFILE

# Delete the headers, put the parts of a HOWTO together in their target 
# directory and delete the temporary files
if [ $VERBOSE = "yes" ]; then
  echo -ne "\nCreating the HOWTO's : "
fi
for FILE in $TMPFILES; do
  FILE_PARTS=`ls $TMPDIR/$FILE.?.$$ 2> /dev/null`
  if [ ! -z "$FILE_PARTS" ]; then
    if [ -f $TO/$FILE$EXT ]; then
      rm -f $TO/$FILE$EXT
    fi
    echo "$FILE :" >> $LOGFILE
    for FILE_PART in $FILE_PARTS; do
      grep "BEGIN Linux" $FILE_PART |\
           sed 's/^.*\(part .*\)$/     \1/g' >> $LOGFILE
      sed -e '1,/^- --- BEGIN/d'\
          -e '/^- --- END/,/^- --- BEGIN/d' $FILE_PART |\
      $COMPRESS >> $TO/$FILE$EXT
      rm -f $FILE_PART
    done
# show that we are working
  if [ $VERBOSE = "yes" ]; then
    echo -n "$FILE "
  fi
  echo >> $LOGFILE
  fi
done
echo ""
if [ $CRONJOB = 1 ] ; then
  touch $LASTRUN
fi
exit 0
