head	1.1;
access;
symbols;
locks; strict;
comment	@# @;


1.1
date	2000.10.26.11.33.41;	author wkoch;	state Exp;
branches;
next	;


desc
@@


1.1
log
@New web design.  Translators: Get your emacs/vi/ed ready.
@
text
@#!/bin/bash
# This script creates the ready for publication webpages
# from a PRCS or CVS project and puts them into a target directroy
# Old frodo PRCS Id: mkwebsite 1.4 Tue, 05 Oct 1999 16:53:59 +0200 wk
# $Id$

pgmname=mkwebsite
use_cvs=yes
use_this_dir=no
rsync_target=""

MKMIRRORLIST="$HOME/bin/mkmirrorlist"

if [ "$1" = "--prcs" ]; then shift; use_cvs=no; fi
if [ "$1" = "--cvs" ]; then shift; use_cvs=yes; fi
if [ "$1" = "--use-this-dir" ]; then shift; use_this_dir=yes; fi

if [ $use_this_dir = yes ]; then
  if [ $# != 1 ]; then
      echo "usage: mkwebsite --use-this-dir target-dir" >&2
      exit 1
  fi
  project="[unused]"
  target_dir="$1"
else
  if [ $# != 2 ]; then
      echo "usage: mkwebsite [--prcs] project target-dir" >&2
      exit 1
  fi
  project="$1"
  target_dir="$2"
fi

if echo "$target_dir" | grep -q : ; then
  rsync_target="$target_dir"
  target_dir="/tmp/mkwebsite.target.$$"
fi




chdir () {
    if ! cd $1; then
	echo "$pgmname: Cannot cd to \`$1'" >&2
	exit 1
    fi
}

fatal () {
    echo "$1" >&2
    exit 1
}


extract_date () {
  if [ $use_cvs == yes ]; then
    # This strange construct is to speed up things.  Grouping a "s" and a "q"
    # does not work. Anyway we should use awk instead.
    # Have to quote the I from $Id so that CVS does not expand it
    sed '/^.*\$[I]d:.*\$.*$/q' $1 |
    sed -n 's!^.*\$[I]d: [^ ]\+ [^ ]\+ \([0-9]*\)/\([0-9]*\)/\([0-9]*\) [^ ]\+ \([^ ]\+\).*$!\1-\2-\3 \4!p'
  else
    ident  -q  < $1 | awk '/\$[I]d:/ { printf "%04d-%02d-%02d\n", $7,
    int(index("xxx:Jan:Feb:Mar:Apr:May:Jun:Jul:Aug:Sep:Oct:Nov:Dec",$6)/4),$5}'
  fi
}


# Create an advanced menulist with all the arrows etc.
create_menulist () {
    ml_tmpl="$1"
    ml_kword="$2"

    gawk -v want="$ml_kword" < $ml_tmpl '
BEGIN	    { split( want, want_parts, "."); maxindex=0; maxlevel=0; printit=1}
/^<\?.*mi=/ { mi = gensub( /^.*mi="([^"]*)".*$/, "\\1", 1 );
	    href = gensub( /^.*href="([^"]*)".*$/, "\\1", 1 );
	    text = gensub( /^.*text="([^"]*)".*$/, "\\1", 1  );
	    temp = mi; level = gsub( /\./, ".", temp );
	    if( level > maxlevel ) { maxlevel = level; }
	    menu[maxindex,"href"] = href;
	    menu[maxindex,"text"] = text;
	    menu[maxindex,"level"] = level;
	    menu[maxindex,"match"] = (mi == want) ? 1:0
	    menu[maxindex,"next"]  = -1
	    menu[maxindex,"child"] = -1
	    maxindex++;
	    next;
	  }

/^<\?show_tree_menu/ {
	    scolor = gensub( /^.*scolor="([^"]*)".*$/, "\\1", 1  );
	    ucolor = gensub( /^.*ucolor="([^"]*)".*$/, "\\1", 1  );
	    build_tree()
	    set_attribs(0)
	    print_tree(0)
	    next
	  }
/^<\?ifndef/   {  if( $2 == want ) { printit=0 }; next }
/^<\?endif/   {  printit=1 ; next }

/^<\?/	  { next }

	  { if( printit ) { printf "%s\\\n", $0 } }


function build_tree( i, li ) # all are local
{
    # link all siblings together
    for(level=0; level <= maxlevel; level++ ) {
	li = -1
	for( i=0; i < maxindex; i++ ) {
	    if( menu[i,"level"] == level ) {
		if( li != -1 ) {
		    menu[li,"next"] = i;
		}
		li = i;
		continue
	    }
	    if( menu[i,"level"] < level ) {
		li = -1;
	    }
	}
    }
    # link first sibling to parent
    for( i=1; i < maxindex; i++ ) {
	if( menu[i,"level"] > menu[i-1,"level"] ) {
	    menu[i-1,"child"] = i;
	}
    }
}


function do_indent( i )
{
    for(; i > 0; i-- ) {
	printf "&nbsp;"
	printf "&nbsp;"
	printf "&nbsp;"
    }
}


function set_attribs( i )
{
    while ( i != -1 ) {
	if( set_attribs( menu[i,"child"] ) ) {
	    menu[i,"match"] = 2
	    return 1
	}
	if( menu[i,"match"] == 1 ) {
	    return 1
	}
	i = menu[i,"next"];
    }
    return 0
}

function print_tree( i )
{
    while ( i != -1 ) {
	if( menu[i,"match"] == 1 ) {
	    do_indent( menu[i,"level"] )
	    printf "<img src=\"../graphics/arrow-r-10.png\" width=\"10\" height=\"10\" alt=\"[&gt;]\">"
	    if( scolor ) {
		printf "&nbsp;<b><font color=\"%s\">%s</font></b><br>\\\n",
				scolor, menu[i,"text"] ;
	    }
	    else {
		printf "&nbsp;<i>%s</i><br>\\\n",
				menu[i,"text"] ;
	    }
	    print_tree( menu[i,"child"] )
	}
	else {
	    do_indent( menu[i,"level"] )
	    if( menu[i,"match"] == 2 ) {
		printf "<img src=\"../graphics/arrow-d-10.png\" width=\"10\" height=\"10\" alt=\"[v]\">"
	    }
	    else {
		printf "<img src=\"../graphics/space-10.png\" width=\"10\" height=\"10\" alt=\"[&nbsp;]\">"
	    }
	    if( ucolor ) {
		printf "&nbsp;<a href=\"%s\"><font color=\"%s\">%s</font></a><br>\\\n",
			   menu[i,"href"], ucolor, menu[i,"text"] ;
	    }
	    else {
		printf "&nbsp;<a href=\"%s\">%s</a><br>\\\n",
			   menu[i,"href"], menu[i,"text"] ;
	    }
	    if( menu[i,"match"] == 2 ) {
		print_tree( menu[i,"child"] )
	    }
	}
	i = menu[i,"next"];
    }
}
'
# end of awk script
    # print an empty line so that the last line does not have a backslash
    echo
}


# S1 = Sourcefile
# $2 = menu template
# This function deletes the <a href> stuff form the menuline which
# has the corresponding word from @@MENUBAR-word@@ as it's href file
# For later processing with sed, a backslash is appended to all but the
# last line
prepare_menu () {
    tmp=`sed -n '/@@MENUBAR-.*@@/ { s/^.*@@MENUBAR-\(.*\)@@.*$/\1/p
			       q
			     }' $1 `
    if grep -q '<\?mi=' $2; then
	# advanced menulist
	create_menulist $2 $tmp
    else
	# Simple old style menubar
	tmp2="href=\"$tmp.html\""
	if [ -n "$tmp2" ] ; then
	    sed "/$tmp2/ s/<[^>]*>//g" $2 | sed "s/@@NAME@@/$tmp/"  \
					  | sed '$ ! s/^.*$/\0\\/'
	else
	    sed '/@@NAME@@/d' $2 | sed '$ ! s/^.*$/\0\\/'
	fi
    fi
}

# simpler version of prepare menubar, but used only for HEADER to
# process the language selection bar.
# We use an ugly kludge to get the name of the page.  It would be
# better to lookup the pagename in the menubar.html.inc
prepare_header () {
    tmp=`basename $1`
                             
    if [ -n "$tmp" ]; then
       sed  "s/@@NAME@@/$tmp/" $2  
    else
       cat $2
    fi
}


proc_html () {
    sf="$1"
    df="$2"
    sdir="$3"
    src="$work_dir"
    updated=`extract_date $sf`
    if [ -f "$sdir/menubar.html.inc" ]; then
	this_menubar=`prepare_menu $sf $sdir/menubar.html.inc`
    else
	this_menubar="<!-- missing menu template -->"
    fi
    if [ -f "$sdir/header.html.inc" ]; then
	prepare_header $sf $sdir/header.html.inc  >$sdir/header.html.tmp
    else
	echo '<!-- missing header template -->'   >$sdir/header.html.tmp
    fi
    if [ -f $src/en/MIRRORS ]; then
	if grep -q '@@FTP-MIRROR-LIST@@' $sf; then
	    $MKMIRRORLIST FTP $src/en/MIRRORS $sdir/countries.trn >$sdir/ftp-mirror.tmp
	fi
	if grep -q '@@WWW-MIRROR-LIST@@' $sf; then
	    $MKMIRRORLIST WWW $src/en/MIRRORS $sdir/countries.trn >$sdir/www-mirror.tmp
	fi
    fi
    sed "/@@FOOTER@@/ {
	    r $sdir/footer.html.inc
	    d
	    }
	/@@FOOTER-EN@@/ {
	    r $sdir/footer-EN.html.inc
	    d
	    }
	/@@HEADER@@/ {
	    r $sdir/header.html.tmp
	    d
	   }
	/^<body>$/ {
	    r $sdir/body-tag.html.inc
	    d
	    }
	/@@UPDATED@@/c\\
Updated: $updated \\
<hr>
	/@@HOSTEDBY@@/ {
	    r $sdir/hostedby.html.inc
	    d
	    }
	/@@INCLUDE-FILE-1@@/ {
	    r $src/en/include-file-1.tmp
	    d
	    }
	/@@INCLUDE-FILE-2@@/ {
	    r $src/en/include-file-2.tmp
	    d
	    }
	/@@INCLUDE-FILE-3@@/ {
	    r $src/en/include-file-3.tmp
	    d
	    }
	/@@INCLUDE-FILE-3@@/ {
	    r $src/en/include-file-3.tmp
	    d
	    }
	/@@FTP-MIRROR-LIST@@/ {
	    r $sdir/ftp-mirror.tmp
	    d
	    }
	/@@WWW-MIRROR-LIST@@/ {
	    r $sdir/www-mirror.tmp
	    d
	    }
	/@@MENUBAR-.*@@/c\\
$this_menubar
    " $sf > $df
}


home_dir=`pwd`
if [ -n "$rsync_target" ]; then
   mkdir $target_dir || fatal "cannot make temporary target dir $target_dir"
fi
if [ ! -d "$target_dir" ]; then
    echo "$pgmname: \`$target_dir': No such directory" >&2
    exit 1
fi

if [ $use_this_dir = yes ]; then
    work_dir=$(pwd)
else
    work_dir="/tmp/mkwebsite.$$"

    # clear the working and the target directory
    chdir $target_dir
    target_dir=`pwd`
    chdir $target_dir
    rm -r .* * 2>/dev/null || true

    rm -r $workdir 2>/dev/null || true
    mkdir $work_dir || fatal "cannot create $work_dir"
    chdir $work_dir

    # get the stuff out of the repository
    if [ $use_cvs = yes ]; then
	cvs export -r HEAD -d . -kv $project
    else
	prcs checkout $project
    fi
fi

with_translation=no
if [ -d en ]; then
  with_translation=yes
  # Figure out the language subdirectories
  lang_dirs=""
  for i in `find . -type d -mindepth 1 -maxdepth 1 -name "??" -print` ; do
      case $i in */en ) : ;;
		 * ) lang_dirs="$lang_dirs $(basename $i)" ;;
      esac
  done
  echo "Translations:$lang_dirs"

  # Add symlinks to missing include files in the languages dirs
  chdir en
  for ldir in $lang_dirs ; do
      for sf in `find . ! -type d -mindepth 1 -maxdepth 1 \
					      -name "*.inc"  -print`; do
	  if [ ! -f ../$ldir/$sf ]; then
	      ln -s "../en/$(echo "$sf" | sed 's!^./!!')" ../$ldir/$sf
	  fi
      done
  done
  chdir ..
fi

# Copy the files to the target directory
# thereby replacing some keywords
for srcdir in `find . -type d ! -name CVS -print` ; do
    dstdir="$target_dir/$srcdir"
    mkdir $dstdir 2>/dev/null || true
    echo "Doing $work_dir/$srcdir -> $dstdir"
    for sf in `find $srcdir -type f -maxdepth 1 -print`; do
	df="$dstdir/`basename $sf`"
	case "$df" in
	    *.html)
		proc_html $sf $df $srcdir
		;;
	    *.html.inc | *~ | *.tmp | *.prj | *.prcs_aux | *.sed | *.trn )
		:
		;;
	    */INDEX-FILE | */MIRRORS )
		:
		;;
	    *)
		cat $sf > $df
		;;
	esac
    done
    for sf in `find $srcdir -type l -maxdepth 1 -print`; do
	df="$dstdir/`basename $sf`"
	cp -d $sf $df
    done
    if [ -s $srcdir/INDEX-FILE -a ! -f $dstdir/index.html ]; then
	ln -s $(cat $srcdir/INDEX-FILE) $dstdir/index.html
    fi
done


if [ $with_translation = yes ]; then
  # for all language dirs setup links for missing files
  chdir $target_dir/en
  for ldir in $lang_dirs ; do
      echo "Creating links for missing files in $ldir"
      for sf in `find . ! -type d -mindepth 1 -maxdepth 1 -print`; do
	  if [ ! -f ../$ldir/$sf ]; then
	      ln -s "../en/$(echo "$sf" | sed 's!^./!!')" ../$ldir/$sf
	  fi
      done
  done
  # And copy all html files from the en subdirectory to the
  # root directory and the the references
  echo "Making copies of the EN subdirectory"
  for sf in `find . ! -type d -mindepth 1 -maxdepth 1 -name "*.html" -print`; do
      if [ ! -f ../$sf ]; then
	  if grep -q '\.\./' $sf; then
	      sed 's!="../!="./!g' < $sf  > ../$sf
	  else
	      ln $sf ../$sf
	  fi
      fi
  done
  for sf in `find . ! -type d -mindepth 1 -maxdepth 1 ! -name "*.html" -print`; do
      if [ ! -f ../$sf ]; then
	  ln $sf ../$sf
      fi
  done
fi



chdir /
if [ $use_this_dir = no ]; then
   rm -r $work_dir || true
fi
chdir $target_dir

if [ -n "$rsync_target" ]; then
    echo "Rsyncing to $rsync_target/"
    rsync --verbose -rlt . $rsync_target/
    chdir /
    rm -r $target_dir || true
fi


exit 0
@
