#!/bin/bash
#
# Copyright by Chih-Wei Huang <cwhuang@phys.ntu.edu.tw>  31 August 1998
# All right reserved
#
# This script can only be redistributed as a part of CLDP documents
# Any other modification and derived work must be granted by the author
#
#              Modified:      September 20, 1998 
#              Last Updated   May 18, 1999
#

#
# Usage: make-howto [gb] src_dir dest_dir comp
#        gb:       also make GB documents
#        src_dir:  dir contains all SGML files
#        dest_dir: dir contains plain text output
#        comp:     1 - compress   0 - don't compress
#
if [ "$1" = "gb" ]; then
    gb=1; shift
else
    gb=0
fi
if [ $# -ne 3 ]; then
    echo Usage: $0 src_dir dest_dir comp
    exit 2
fi

PATH=/bin:/usr/bin:/usr/local/bin

if [ `uname` != "Linux" ]; then
    echo Must use a Linux box to run the script!
    exit 1
fi

unset LANG
unset LC_ALL
unset LC_CTYPE

SGML2HTML=bg5sgml2html
SGML2TXT=bg5sgml2txt
SGML2LATEX=bg5sgml2latex

# $1: the dir
function GetFullDir()
{
    if [ "`echo $1 | cut -c1`" = "/" ]; then
        echo $1
    else
        echo `pwd`/$1
    fi
}

# $1: the file
function GetFullDate()
{
    fl=`ls -l --full-time $1`
    case "`echo $fl | awk '{print $7}'`" in
        Jan) month=01 ;;
        Feb) month=02 ;;
        Mar) month=03 ;;
        Apr) month=04 ;;
        May) month=05 ;;
        Jun) month=06 ;;
        Jul) month=07 ;;
        Aug) month=08 ;;
        Sep) month=09 ;;
        Oct) month=10 ;;
        Nov) month=11 ;;
        Dec) month=12 ;;
        *)   month=00 ;;   # shouldn't occur
    esac
    day=`echo $fl | awk '{print $8}'` 

# Note: in GNU fileutils 3.1.6, it's no need to add '0'
#    if [ ${day} -lt 10 ]; then
#        day=0$day
#    fi

    time=`echo $fl | awk '{print $9}'`
    min=`echo $time | awk -F : '{printf $1$2}'`
    sec=`echo $time | awk -F : '{printf $3}'`
    whole_time=$month$day$min`echo $fl | awk '{print $10}'`.$sec
    echo $whole_time
}

# Prepare the doc dir
function MakeFTPDir()
{
    for ddir in $dest_dir $dest_dir/gb; do
        [ ! -d $ddir ] && mkdir -p $ddir
        for sdir in sgml html tex dvi ps \
                    mini mini/sgml mini/html mini/tex mini/dvi mini/ps; do
            [ ! -d $ddir/$sdir ] && mkdir $ddir/$sdir
        done
    done

    [ -f $logfile ] && mv -f $logfile $logfile.old
}

# current dir: the dir containing sgml file
# $1  SGML file to be converted
# $2  dest dir
# $3  gb dir
function ConvertHOWTOSGML()
{
    pushd . > /dev/null
    name=`basename $1 .sgml`
    srcsgml=`pwd`/$name.sgml
    cp -f $name.sgml $2
    touch -r $srcsgml $2/$name.sgml
    cd $2
    echo -n "Convert $name.sgml:"
    echo -n " html"
    $SGML2HTML $name >> $logfile
    echo -n " txt"
    $SGML2TXT $name >> $logfile
    echo -n " tex"
    $SGML2LATEX $name --clean --output=all --font=akai >> $logfile 2>&1
    touch -r $srcsgml $name*.html $name.txt $name.tex $name.dvi $name.ps

    # convert GB documents
    if [ $gb -eq 1 ]; then
        echo -n " gb"
        for file in `/bin/ls $name*.html`; do
            sed "$gbpattern" $file | b2g > $3/$file 2>> $logfile
        done
        for file in $name.sgml $name.txt $name.tex; do
            b2g < $file > $3/$file 2>> $logfile
        done
        touch -r $srcsgml $3/$name.sgml \
                 $3/$name*.html $3/$name.txt $3/$name.tex 
    fi

    if [ $comp -eq 1 ]; then
        echo -n " gzip"
        gzip -9f $name.sgml $name.txt $name.tex $name.dvi $name.ps
        tar zcf $name-html.tar.gz $name*.html --remove-files
        touch -r $srcsgml $name-html.tar.gz
        mv -f $name-html.tar.gz html
        if [ $gb -eq 1 ]; then
            cd $3
            gzip -9f $name.sgml $name.txt $name.tex
            tar zcf $name-html.tar.gz $name*.html --remove-files
            touch -r $srcsgml $name-html.tar.gz
            mv -f $name-html.tar.gz html
            cd $2 
        fi
    else
        mv -f $name*.html html
        if [ $gb -eq 1 ]; then
            mv -f $3/$name*.html $3/html
        fi
    fi 
    for i in sgml tex dvi ps; do
        mv -f $name.$i$suffix $i
    done
    if [ $gb -eq 1 ]; then
        mv -f $3/$name.sgml$suffix $3/sgml
        mv -f $3/$name.tex$suffix $3/tex
    fi
    echo .
    popd > /dev/null
}

# current dir: the dir containing html file
# $1  HTML file to be converted
# $2  dest dir
# $3  gb dir
function ConvertHOWTOHTML()
{
    pushd . > /dev/null
    name=`basename $1 .html`
    srchtml=`pwd`/$name.html
    cp -f $name.html $2
    touch -r $srchtml $2/$name.html
    cd $2
    echo "Convert $name.html: txt"
    lynx -width=80 -dump $name.html > $name.txt
    touch -r $srchtml $name.txt $name.html

    if [ $gb -eq 1 ]; then
        sed "$gbpattern" $name.html | b2g > $3/html/$name.html 2>> $logfile
        b2g < $name.txt > $3/$name.txt 2>> $logfile
        touch -r $srchtml $3/$name.txt $3/html/$name.html
        [ $comp -eq 1 ] && gzip -9f $3/$name.txt $3/html/$name.html
    fi
    [ $comp -eq 1 ] && gzip -9f $name.txt $name.html
    mv -f $name.html$suffix html
    popd > /dev/null
}

# current dir: source dir to create tar.gz
# $1: HOWTOs or mini-HOWTOs
function TarAllHOWTO()
{
    # GTAR all HOWTOs
    echo "Creating archives for all $1..."
    rm -f C-Linux-$1.tar.gz
    tar zcf C-Linux-$1.tar.gz *.gz
    cd sgml
    rm -f C-Linux-$1-sgml.tar.gz
    tar zcf C-Linux-$1-sgml.tar.gz *.sgml.gz
    cd ../html
    rm -f C-Linux-$1-html.tar.gz
    tar zcf C-Linux-$1-html.tar.gz *.gz
}

src_dir=`GetFullDir $1`
dest_dir=`GetFullDir $2`
comp=$3
logfile=$dest_dir/convert.log
gbpattern="s/html; charset=big5/html; charset=gb2312/"

umask 022

if [ $comp -eq 1 ]; then
    suffix=".gz"
else
    suffix=""
fi

unalias -a
MakeFTPDir

modified=0
cd $src_dir
filelist=`ls *.sgml 2> /dev/null`
if [ $? -eq 0 ]; then
    for file in $filelist; do
        dfile=$dest_dir/sgml/$file$suffix
        if [ ! -e $dfile -o $file -nt $dfile -o $file -ot $dfile ]; then
            ConvertHOWTOSGML $file $dest_dir $dest_dir/gb
            modified=1
        fi
    done
fi

if [ $comp -eq 1 -a $modified -eq 1 ]; then
    cd $dest_dir
    TarAllHOWTO HOWTOs
    if [ $gb -eq 1 ]; then
        cd $dest_dir/gb
        TarAllHOWTO HOWTOs
    fi
fi

modified=0
cd $src_dir/mini
filelist=`ls *.sgml 2> /dev/null`
if [ $? -eq 0 ]; then
    for file in $filelist; do
        dfile=$dest_dir/mini/sgml/$file$suffix
        if [ ! -e $dfile -o $file -nt $dfile -o $file -ot $dfile ]; then
            ConvertHOWTOSGML $file $dest_dir/mini $dest_dir/gb/mini
            modified=1
        fi
    done
fi

filelist=`ls *.html 2> /dev/null`
if [ $? -eq 0 ]; then
    for file in $filelist; do
        dfile=$dest_dir/mini/html/$file$suffix
        if [ ! -e $dfile -o $file -nt $dfile -o $file -ot $dfile ]; then
            ConvertHOWTOHTML $file $dest_dir/mini $dest_dir/gb/mini
            modified=1
        fi
    done 
fi

if [ $comp -eq 1 -a $modified -eq 1 ]; then
    cd $dest_dir/mini
    TarAllHOWTO mini-HOWTOs
    if [ $gb -eq 1 ]; then
        cd $dest_dir/gb/mini
        TarAllHOWTO mini-HOWTOs
    fi
fi


exit 0
