#! /bin/sh

#================================================================
# ccdoc
# Document generator for C/C++ sources
#================================================================


# set variables
progname='ccdoc'
stylefile='ccstyle.css'
indexfile='index.html'
lang='en'
charset='ISO-8859-1'
targets=''
sufregxp='(\.h|\.c|\.cc|\.cxx|\.cpp)$'
outdir='srcdoc'
title='Source Documents'
overview=''
numopt=''
dark='no'


# function to show usage and exit
usage(){
  printf 'usage: %s [options] sourcefiles ...\n' "$progname"
  printf 'options:\n' 2>&1
  printf '  -l lang       assign the language of documents\n' 2>&1
  printf '  -cs charset   assign the charcter set of documents\n' 2>&1
  printf '  -t title      assign the title of the index file\n' 2>&1
  printf '  -d directory  assign the directory in which documents are stored\n' 2>&1
  printf '  -ov file      assign the HTML file for overview\n' 2>&1
  printf '  -n            number all output lines\n' 2>&1
  printf '  -dark         set the style dark\n' 2>&1
  exit 1;
}


# check dependency
if source-highlight --version > /dev/null
then
  true
else
  printf '%s: requires GNU source-highlight\n' "$progname" 2>&1
  exit 1
fi


# parse arguments
while true
do
  if printf '%s\n' "$1" | grep '^-' > /dev/null
  then
    case "$1" in
    '-l') [ $# -le 1 ] && usage ; shift ; lang="$1" ;;
    '-cs') [ $# -le 1 ] && usage ; shift ; charset="$1" ;;
    '-t') [ $# -le 1 ] && usage ; shift ; title="$1" ;;
    '-d') [ $# -le 1 ] && usage ; shift ; outdir="$1" ;;
    '-ov') [ $# -le 1 ] && usage ; shift ; overview="$1" ;;
    '-n') numopt='-n' ;;
    '-dark') dark='yes' ;;
    *) usage ;;
    esac
  else
    targets="$targets $1"
  fi
  if [ $# -lt 1 ]
  then
    break
  else
    shift
  fi
done
targets=`printf '%s' "$targets" | sed -e 's/^ *//' -e 's/^ *$//'`
if [ -z "$targets" ]
then
  usage
fi


# find target files
printf 'finding targets ... '
tfiles=""
for file in $targets
do
  if [ -f "$file" ]
  then
    tfiles="$tfiles $file"
  elif [ -d "$file" ]
  then
    tfiles="$tfiles `find $file -print | egrep $sufregxp | sed 's/^\.\///' | sort`"
  else
    printf '%s: %s: not found\n' "$progname" "$file"  2>&1
    exit 1
  fi
done
printf 'ok\n'


# create the target directory
printf 'creating %s ... ' "$outdir"
mkdir -p "$outdir"
[ $? != 0 ] && exit 1
printf 'ok\n'


# create the index file
printf 'creating %s ... ' "$outdir/$indexfile"
cat << __EOF > "$outdir/$indexfile"
<?xml version="1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="$lang" xml:lang="$lang">
<head>
<meta http-equiv="content-type" content="text/html; charset=$charset" />
<title>$title</title>
<link rel="stylesheet" href="$stylefile" type="text/css" />
<link rel="contents" href="$indexfile" />
</head>
<body>
<h1>$title</h1>
<hr />
__EOF
if [ -n "$overview" ]
then
  printf '<h2>Overview</h2>\n' >> "$outdir/$indexfile"
  cat "$overview" >> "$outdir/$indexfile"
  if [ $? != 0 ]
  then
    printf '%s: warning: %s: cannot write overview\n' "$progname" "$outdir/$indexfile" 2>&1
  fi
  printf '<hr />\n' >> "$outdir/$indexfile"
fi
printf '<h2>Sources</h2>\n' >> "$outdir/$indexfile"
printf '<ul>\n' >> "$outdir/$indexfile"
for file in $tfiles
do
  outfile="$file.html"
  printf '<li><a href="%s">%s</a></li>\n' "$outfile" "$file" >> "$outdir/$indexfile"
done
printf '</ul>\n' >> "$outdir/$indexfile"
if [ $? != 0 ]
then
  printf '%s: %s: cannot create\n' "$progname" "$outdir/$indexfile" 2>&1
  exit 1
fi
version=`source-highlight --version | head -n 1`
cat << __EOF >> "$outdir/$indexfile"
<hr />
<div class="note"><small>Generated by $progname using $version.</small></div>
</body>
</html>
__EOF
if [ $? != 0 ]
then
  printf '%s: %s: cannot create\n' "$progname" "$outdir/$indexfile" 2>&1
  exit 1
fi
printf 'ok\n'


# create the style file
printf 'creating %s ... ' "$outdir/$stylefile"
if [ "$dark" = "yes" ]
then
cat << __EOF > "$outdir/$stylefile"
body { background-color: #002222; color: #667788; margin: 0em 0em; padding: 0.5em 0.8em; }
p,div { margin: 1em 2em; }
p { text-indent: 0.8em; }
a:link,a:visited,a:active { text-decoration: none; color: #88aaff; }
a:hover { text-decoration: underline; color: #66eeff; }
pre { margin: 0em 0em; padding: 0em 0em; }
.normal { color: #ddeeff; }
.comment { color: #bb1100; }
.preproc { color: #884488; }
.function { color: #88ddbb; }
.keyword { color: #4488ee; }
.type { color: #88bbff; }
.string { color: #8899aa; }
.number,.symbol,.cbracket { color: #ccddee; }
.note { text-align: right; margin: 0.2em 0.2em; color: #8899aa; }
h1,h2,h3,p,div,li { color: #ddeeff; }
strong { color: #eeee99; }
__EOF
else
cat << __EOF > "$outdir/$stylefile"
body { background-color: #ffffff; color: #888888; margin: 0em 0em; padding: 0.5em 0.8em; }
p,div { margin: 1em 2em; }
p { text-indent: 0.8em; }
a:link,a:visited,a:active { text-decoration: none; color: #0022aa; }
a:hover { text-decoration: underline; color: #0066ff; }
pre { margin: 0em 0em; padding: 0em 0em; }
.normal { color: #111111; }
.comment { color: #119966; }
.preproc { color: #cc2200; }
.function { color: #5500bb; }
.keyword { color: #0022ff; }
.type { color: #003399; }
.string { color: #555555; }
.number,.symbol,.cbracket { color: #333333; }
.note { text-align: right; margin: 0.2em 0.2em; color: #555555}
h1,h2,h3,p,div,li { color: #111111; }
strong { color: #6600bb; }
__EOF
fi
if [ $? != 0 ]
then
  printf '%s: %s: cannot create\n' "$progname" "$outdir/$stylefile" 2>&1
  exit 1
fi
printf 'ok\n'


# create documents of targets
for file in $tfiles
do
  outfile="$file.html"
  if printf '%s' "$file" | grep '/' > /dev/null
  then
    subdir=`printf '%s' "$file" | sed 's/\/[^\/]*$//'`
    if [ -d "$outdir/$subdir" ]
    then
      [ "$subdir" != "." ] && rm -f "$outdir/$subdir/$stylefile"
    else
      mkdir -p "$outdir/$subdir"
    fi
    [ -f "$outdir/$subdir/$stylefile" ] || cp -f "$outdir/$stylefile" "$outdir/$subdir"
  fi
  printf 'creating %s ... ' "$outdir/$outfile"
  source-highlight -s cpp -f xhtml -d -T "$file ($title)" -c "$stylefile" $numopt < "$file" |\
    sed -e "1,13 s/<html>/<html lang=\"$lang\">/" \
      -e "1,13 s/charset=.*\\/>/charset=$charset\" \\/>/" > "$outdir/$outfile"
  [ $? != 0 ] && exit 1
  printf 'ok\n'
done


# exit normally
printf 'all ok\n'
exit 0



# END OF FILE
