#!/bin/bash
#############################################################
#                                                           #
# create-jinrlib-entry:  Create standard JINRLIB entry.     #
#                                                           #
# Input:                                                    #
#                                                           #
#   Site configuration file is required.                    #
#   It sets keywords used for JINRLIB entries               #
#   via variables:                                          #
#      Name, Author, CoAuthors, Summary,                    #
#      Version, Date, Language, Platforms,                  #
#      Index, Keywords,                                     #
#      Description (README file is taken if exists),        #
#      Mail (corresponding author)                          #
#   The core distribution assumed to be in `pwd`            #
#   Ahy docs assumed to be in `pwd`/docs                    #
#                                                           #
# Output:                                                   #
#                                                           #
# `pwd`                                                     #
#   |----> index.html            -- general site (russian)  #
#   |----> indexe.html           -- general site (enflish)  #
#   `----> site                                             #
#            |----> ru           -- frames (russian)        #
#            |       |----> menu.html                       #
#            |       |----> about.html                      #
#            |       `----> download.html                   #
#            `----> en           -- frames (enflish)        #
#                    |----> menu.html                       #
#                    |----> about.html                      #
#                    `----> download.html                   #
#                                                           #
#############################################################
# (C) Sat Apr 12 18:32:38 MSK 2014 by Alexei Soloviev       #
# Email: solovjev@jinr.ru                                   #
#############################################################


#############################################################
# Arguments processing
#############################################################

if [ $# -lt 1 ]; then
    echo "Usage: `basename $0` [options] file, for help give option -h"
    exit 1
fi

while getopts hes Options
do
    case $Options in
    h) echo \
"Usage: `basename $0` [options] file
Options: [defaults in brackets after descriptions]
-e         create english site, indexe.html [index.html]
-s         insert russian/english switches in sites [no]
-h         display this help and exit
File:
           site configuration file"
    exit 0 ;;
    e) English="Yes" ;;
    s) Switch="Yes" ;;
    esac
done

eval infile=\$${OPTIND}   

if [ -z "$infile" ]; then
    echo `basename $0`: no input file specified
    exit 1
fi

while [ $1 != $infile ]; do
    shift
done

. $infile


#############################################################
# Constants definitions
#############################################################

IndexFile="index.html"
IndexeFile="indexe.html"

# Output:
[ x$English != x ] && indexfile=$IndexeFile || indexfile=$IndexFile
[ x$English != x ] && Site="en" || Site="ru"

# Header:
[ x$English != x ] && JINRLIBWord="Library" || JINRLIBWord="Библиотека"

# Info:
if [ x$English != x ]; then
    [ -n "$CoAuthors" ] && AuthorWord="Authors" || AuthorWord="Author"
else
    [ -n "$CoAuthors" ] && AuthorWord="Авторы" || AuthorWord="Автор"
fi
[ x$English != x ] && VersionWord="Version" || VersionWord="Версия"
[ x$English != x ] && LanguageWord="Language" || LanguageWord="Язык"
[ x$English != x ] && PlatformsWord="Platforms" || PlatformsWord="Платформы"
[ x$English != x ] && IndexWord="Index" || IndexWord="Индекс"
[ x$English != x ] && KeywordsWord="Keywords" || KeywordsWord="Ключевые слова"

# Package:
[ x$English != x ] && SRCWord="Source package" || SRCWord="Архив программы"
RPMWord="RPM"
SRPMWord="Source RPM"
PKGWord="Solaris 7 package"
EXEWord="exe (Windows)"
[ x$English != x ] && SampleDataWord="Input data example" || SampleDataWord="Пример входных данных"
[ x$English != x ] && PrintedDocsWord="Documentation (printed version)" || PrintedDocsWord="Документация (версия для печати)"

# Menu:
[ x$English != x ] && AboutWord="About" || AboutWord="О программе"
[ x$English != x ] && DocsWord="Docs" || DocsWord="Документация"
[ x$English != x ] && DownloadWord="Download" || DownloadWord="Скачать"
[ x$English != x ] && UpWord="JINRLIB index" || UpWord="JINRLIB: оглавление"
[ x$English != x ] && YouWord="You are" || YouWord="Вы"
[ x$English != x ] && VisitorWord="visitor here" || VisitorWord="посетитель"


#############################################################
# Here we go!
#############################################################

rm -rf site/$Site
mkdir -p site/$Site


#############################################################
# INDEX Creation
#############################################################

# Common HTML decoration:

if [ x$English != x ]; then
    Meta="HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html\""
else
    Meta="HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf-8\""
fi
Meta="<meta $Meta>"

Style="<style type=\"text/css\">a {color:red; font-weight:bold;}</style>"

BodySide="<body background=\"../../../bg1.gif\" bgcolor=\"white\" text=\"red\" link=\"red\" vlink=\"red\">"
BodyMain="<body bgcolor=\"white\" text=\"000080\" link=\"red\" vlink=\"red\">"

# Writing:

cat > $indexfile << INDEX
<html>
    <head>
	<title>JINRLIB: $Name</title>
    </head>
    <frameset cols="180,*" border="0">
	<frame name="side" src="site/$Site/menu.html" scrolling="auto" NORESIZE>
	<frame name="main" src="site/$Site/about.html" scrolling="auto" NORESIZE>
    </frameset>
</html>
INDEX


#############################################################
# MENU Frame Creation
#############################################################

# References:

Image="<img src=\"../../../iconcomp.jpg\" border=\"0\">"

About="<a href=\"about.html\" target=\"main\">$AboutWord</a>"
[ -f docs/html/index.html ] && 
Docs="<a href=\"../../docs/html/index.html\" target=\"main\">$DocsWord</a>"
Download="<a href=\"download.html\" target=\"main\">$DownloadWord</a>"

Up="<a href=\"../../../$indexfile\" target=\"_top\">$UpWord</a>"

if [ x$Switch != x ]; then
    SwitchImgRu="rus.gif"
    SwitchImgEn="eng.gif"
    cp /usr/local/share/icons/$SwitchImgRu site/$SwitchImgRu 2>/dev/null
    cp /usr/local/share/icons/$SwitchImgEn site/$SwitchImgEn 2>/dev/null
    [ x$English != x ] && SwitchFile=$IndexFile || SwitchFile=$IndexeFile
    [ x$English != x ] && SwitchImg=$SwitchImgRu || SwitchImg=$SwitchImgEn
    Switch="<a href=\"../../$SwitchFile\" target=\"_top\"><img src=\"../$SwitchImg\" border=\"1\"></a>"
fi

[ x$Counter != x ] && Counter="<img src=\"http://wwwinfo.jinr.ru/~gvv/php/counter/counter.php?counter=$Counter&bg=white&fg=red\" border=\"1\">"

# Decoration:

About="<li>$About"
[ -n "$Docs" ] && Docs="<li>$Docs"
Download="<li>$Download"

Up="<h4 align=\"left\">$Up</h4>"

[ -n "$Switch" ] && Switch="<h4 align=\"left\">$Switch</h4>"

[ -n "$Counter" ] && Counter=\
"<h4 align=\"left\">
	    $YouWord<br>
	    $Counter<br>
	    $VisitorWord.
	</h4>"

# Writing:

cat > site/$Site/menu.html << MENU
<html>
    <head>
	$Meta
	<title>JINRLIB: $Name, menu</title>
    </head>
    $Style
    $BodySide
	$Image
	<h2 align="left">$Name</h2>
	<ul>
	    $About
	    $Docs
	    $Download
	</ul>
	$Up
	$Switch
	$Counter
    </body>
</html>
MENU


#############################################################
# MAIN Frame Creation
#############################################################

# Decoration

[ -n "$Mail" ] && Author="<a href=\"mailto: $Mail\">$Author</a>"
Author="$AuthorWord: $Author"
[ -n "$CoAuthors" ] && Author="$Author, $CoAuthors"

if [ -n "$Version" ]; then
    Version="$VersionWord $Version"
    [ -n "$Date" ] && Version="$Version ($Date)"
fi

[ -n "$Language" ] && Language="$LanguageWord: $Language"
[ -n "$Platforms" ] && Platforms="$PlatformsWord: $Platforms"

[ -n "$Index" ] && Index="$IndexWord: $Index"
[ -n "$Keywords" ] && Keywords="$KeywordsWord: $Keywords"

if [ -z "$Description" ]; then
    if [ x$English != x ]; then
	Description="`cat README 2>/dev/null`"
    else
	Description="`cat README.ru 2>/dev/null`"
    fi
fi
if [ -n "$Description" ]; then
    Description=\
"	<p align=\"justify\"><b>
$Description
	</b></p>"
fi

if [ -n "$MainComments" ]; then
    MainComments=\
"	<p align=\"justify\"><b><i>
$MainComments
	</i></b></p>"
fi

# Writing:

cat > site/$Site/about.html << MAIN
<html>
    <head>
	$Meta
	<title>JINRLIB: $Name, main</title>
    </head>
    $Style
    $BodyMain
	<h4 align="center">$JINRLIBWord "JINRLIB"</h4>
	<h1 align="left">$Name</h1>
	<h4 align="left">$Author</h4>
	<h3 align="center">$Summary</h3>
	<h5 align="left">
	    $Version<br>
	    $Language<br>
	    $Platforms<br>
	</h5>
	<h5 align="left">
	    $Index<br>
	    $Keywords<br>
	</h5>
$Description
$MainComments
    </body>
</html>
MAIN


#############################################################
# DOWNLOAD Frame Creation
#############################################################

# References:

#SRC=`ls *.tar.gz | grep -iv sparc 2>/dev/null`
#[ -n "$SRC" ] && SRC="<a href=\"../../$SRC\"><code>$SRC</code></a>"
for src in `ls *.tar.gz | grep -iv sparc 2>/dev/null`; do
    if [ -n "$src" ]; then
	[ -n "$SRC" ] && SRC="$SRC,"
	SRC="$SRC <a href=\"../../$src\"><code>$src</code></a>"
    fi
done
#RPM=`ls *.i?86.rpm 2>/dev/null`
#[ -n "$RPM" ] && RPM="<a href=\"../../$RPM\"><code>$RPM</code></a>"
for rpm in `ls *.i?86.rpm 2>/dev/null`; do
    if [ -n "$rpm" ]; then
	[ -n "$RPM" ] && RPM="$RPM,"
	RPM="$RPM <a href=\"../../$rpm\"><code>$rpm</code></a>"
    fi
done
#SRPM=`ls *.src.rpm 2>/dev/null`
#[ -n "$SRPM" ] && SRPM="<a href=\"../../$SRPM\"><code>$SRPM</code></a>"
for srpm in `ls *.src.rpm 2>/dev/null`; do
    if [ -n "$srpm" ]; then
	[ -n "$SRPM" ] && SRPM="$SRPM,"
	SRPM="$SRPM <a href=\"../../$srpm\"><code>$srpm</code></a>"
    fi
done
#PKG=`ls *.pkg* 2>/dev/null`
#[ -n "$PKG" ] && PKG="<a href=\"../../$PKG\"><code>$PKG</code></a>"
for pkg in `ls *.pkg* 2>/dev/null`; do
    if [ -n "$pkg" ]; then
	[ -n "$PKG" ] && PKG="$PKG,"
	PKG="$PKG <a href=\"../../$pkg\"><code>$pkg</code></a>"
    fi
done
#EXE=`ls *.exe 2>/dev/null`
#[ -n "$EXE" ] && EXE="<a href=\"../../$EXE\"><code>$EXE</code></a>"
for exe in `ls *.exe 2>/dev/null`; do
    if [ -n "$exe" ]; then
	[ -n "$EXE" ] && EXE="$EXE,"
	EXE="$EXE <a href=\"../../$exe\"><code>$exe</code></a>"
    fi
done

SampleData=`ls test.* 2>/dev/null`
[ -n "$SampleData" ] && SampleData="<a href=\"../../$SampleData\"><code>$SampleData</code></a>"

PS=`ls docs/*.ps 2>/dev/null`
[ -n "$PS" ] && PS="<a href=\"../../$PS\"><code>`basename $PS`</code></a>"

PSGZ=`ls docs/*.ps.gz 2>/dev/null`
[ -n "$PSGZ" ] && PSGZ="<a href=\"../../$PSGZ\"><code>`basename $PSGZ`</code></a>"

# Decoration

if [ -n "$DownloadVersion" ]; then
    DownloadVersion="$VersionWord $DownloadVersion"
else
    DownloadVersion="$Version"
fi

[ -n "$SRC" ] && SRC="<li><b>$SRCWord</b>: $SRC"
[ -n "$RPM" ] && RPM="<li><b>$RPMWord</b>: $RPM"
[ -n "$SRPM" ] && SRPM="<li><b>$SRPMWord</b>: $SRPM"
[ -n "$PKG" ] && PKG="<li><b>$PKGWord</b>: $PKG"
[ -n "$EXE" ] && EXE="<li><b>$EXEWord</b>: $EXE"

if [ -n "$SampleData" ];then
    SampleData="<h2 align=\"left\">$SampleDataWord</h2>
	<ul>
	    <li>$SampleData
	</ul>"
fi

[ -n "$PS" ] && PS="<li>$PS"
[ -n "$PSGZ" ] && PSGZ="<li>$PSGZ"

if [ -n "$PS" -o -n "$PSGZ" ];then
    PrintedDocs="<h2 align=\"left\">$PrintedDocsWord</h2>
	<ul>
	    $PS
	    $PSGZ
	</ul>"
fi

if [ -n "$DownloadComments" ];then
    DownloadComments="<p align=\"justify\"><b><i>
$DownloadComments
	</i></b></p>"
fi

# Writing:

cat > site/$Site/download.html << DOWNLOAD
<html>
    <head>
	$Meta
	<title>JINRLIB: $Name, download</title>
    </head>
    $Style
    $BodyMain
	<h4 align="center">$JINRLIBWord "JINRLIB"</h4>
	<h1 align="left">$Name</h1>
	<h2 align="left">$DownloadVersion</h2>
	<ul>
	    $SRC
	    $RPM
	    $SRPM
	    $PKG
	    $EXE
	</ul>
	$SampleData
	$PrintedDocs
	$DownloadComments
    </body>
</html>
DOWNLOAD
