# !/bin/bash
# 20051210 makeman.sh hse@ukr.net http://docs.linux.org.ua/dlou/index.php/Man
# Distributed under the terms of the GNU General Public License v2 or later

# This script automate manpages-lang-encode-version.tar.bz2 generations from wiki
# formated file using wiki to man converter. Can be added to cron jobs.

# Edit to suite yours configuration:
# You language:
lang=uk
# Location of wiki formated man pages:
wikimandir=~/doc/Man
# Wiki to man converter (wiki2man.py):
converter=~/programs/man/wiki2man/wiki2man.py
# Some file for man instalation:
addfile=~/programs/man/wiki2man/manpages-$lang
# Working directory:
dirout=~/programs/man/test
# E-mail address for converter errors...
email=docs@linux.org.ua
# Release version:
version=`date +%Y%m%d`
# List of encodings for manual pages:
manencoding="utf8"

if [ -f $dirout/errors ]
  then
    rm -f $dirout/errors
fi

# Prepare output directory:
for i in $manencoding
  do
    # Name of man pages file. Next line will produce in the directory
    # $dirout file: "manpages-uk-utf8-20051114.tar.bz2"
    mansources=manpages-$lang-$i-$version
    # You also will get manpages sources in the directory:
    mandir=$dirout/$mansources

    if [ -d $mandir ]
      then
        rm -rf $mandir
    fi

    for i in 1 2 3 4 5 6 7 8
      do
        mkdir -p $mandir/man$i
        if [ -f $wikimandir/man$i/ALIASES ]
          then
            cp $wikimandir/man$i/ALIASES $mandir/man$i/
        fi
      done
  done

# Reformatting wiki -> roff:
for i in 1 2 3 4 5 6 7 8
  do
    if [ -d $wikimandir/man$i ]
      then
        cd $wikimandir/man$i
        for j in `ls |grep '\.'`
          do
            echo -n "Reformatting: $j ..."
            dirin=$wikimandir/man$i/
            file=`echo $j |awk -F'.' '{print $1}'`
            if <span class="createlink"><a href="/cgi-bin/ikiwiki.cgi?page=awk_-F__39__.__39_____39____123__print___36__3__125____39____96_____61____61_____39____39___&amp;from=man%2F%D0%A0%D0%B5%D0%B4%D0%B0%D0%B3%D1%83%D0%B2%D0%B0%D0%BD%D0%BD%D1%8F_%D1%81%D1%82%D0%BE%D1%80%D1%96%D0%BD%D0%BE%D0%BA_%D0%BF%D1%96%D0%B4%D1%80%D1%83%D1%87%D0%BD%D0%B8%D0%BA%D0%B0%2Fmakeman.sh&amp;do=create" rel="nofollow">?</a> `echo $j </span>
              then
                ext=`echo $j |awk -F'.' '{print $2}'`
              else
                file=$file.`echo $j |awk -F'.' '{print $2}'`
                ext=`echo $j |awk -F'.' '{print $3}'`
            fi
            #time=`date -I`
            time=`ls -l --time-style=long-iso $j |awk '{print $6}'`"-"`ls -l --time-style=long-iso $j |awk '{print $7}'`
            $converter 2>&1 $lang $dirin $dirout $file $ext $time $version $manencoding >> $dirout/errors
            echo "   OK!"
          done
    fi
  done

# Errors notification or release!
if [ -s $dirout/errors ]
  then
    cat $dirout/errors
    mail -s "wiki2man.py errors" $email < $dirout/errors
    exit 1
  else
    # List of encodings for manual pages:
    for i in $manencoding
      do
        # Name of man pages file. Next line will produce in the directory
        # $dirout file: "manpages-uk-utf8-20051114.tar.bz2"
        mansources=manpages-$lang-$i-$version
        # You also will get manpages sources in the directory:
        mandir=$dirout/$mansources

        # Copy some things to do manpage source code:
        cp -r $addfile/* $mandir

        # Create archive:
        cd $dirout
        tar cf $mansources.tar $mansources
        bzip2 -zf9 $mansources.tar

        # Release!
        #cp -f $dirout/$mansources.tar.bz2 /var/ftp/pub/man/$mansources.tar.bz2
      done
    exit 0
fi