#!/bin/sh
#Author: KGussan
#$RCSfile: wpgen.sh,v $ $Revision: 2.5 $ $Date: 2006/09/06 11:42:49 $
#notice:
#-	cygwin requires shift-jis encoding in shell script.
#-	cygwin requires \\ as escape sequence instead of \.
#================================================================
target_dir=""
templete_dir=cvshtml
release_dir="release"
time_start=`date +%s`
target_dir="pc"


if [ $# -lt 1 ]; then
cat <<-END
wpgen
-----
usage: command [-a|m|d|w|i|v] filename

-a : generate all pages
-m : generate pages which modified within a month.
-w : generate pages which modified within a week.
-d : generate pages which modified within a day.
-i : generate index page only.
-v : display version.

END
exit
fi

cat <<-END
wpgen $Revision: 2.5 $
END

# backup
#	echo "# debug : $1 "

if [ $1 = "-a" ]; then
echo "$0: all mode"
# refresh release directory
echo "$0: refreshing release directory start"
rm -rf ${release_dir}
mkdir ${release_dir}
echo "$0: refreshing release directory end"
src_files=`find ./src/ -type f`

elif [ $1 = "-m" ]; then
echo "$0: month mode"
src_files=`find ./src/ -mtime -30 -type f`

elif [ $1 = "-w" ]; then
echo "$0: week mode"
src_files=`find ./src/ -mtime -7 -type f`

elif [ $1 = "-d" ]; then
echo "$0: day mode"
src_files=`find ./src/ -mtime -1 -type f`

elif [ $1 = "-i" ]; then
echo "$0: only index generation mode"
src_files=""

elif [ $1 = "-v" ]; then
echo "$0: "'$Revision: 2.5 $'
exit

else
echo "illegal option"
echo "enter \"command\""
exit

fi

# generate pc webpage
#--------------------
#	3line page, no frame
target_dir="pc"		# working dir name 

rm -rf ${target_dir}
mkdir ${target_dir}

echo "$0: generating ${target_dir} start"

# generate index.html
echo "$0: generating index.html"
cat ${templete_dir}/pc-header.html		>	${target_dir}/index.html
echo "<!-- Generator: wpgen.sh $Revision: 2.5 $ -->" >> ${target_dir}/index.html
cat ${templete_dir}/menu.html			>>	${target_dir}/index.html
cat ${templete_dir}/pc-left.html		>>	${target_dir}/index.html
cat ${templete_dir}/top.html			>>	${target_dir}/index.html
cat ${templete_dir}/pc-footer.html		>>	${target_dir}/index.html


# generate content's html
# backup
#	src_files=`find ./src/ -mtime -2 -type f`

# echo "debug: ${src_files}"
# backup
#	for filename in src/*; do

count_of_filename=0

for filename in ${src_files}; do
	count_of_filename=`expr $count_of_filename + 1`
	filename_basename=`basename ${filename}`
	# backup
	#	filename_out=${filename#src/}
	filename_out=${filename_basename#src/}
	echo "$0: [$count_of_filename/"`echo ${src_files} | wc -w `"] generating ${target_dir}/${filename_out}"
	cat ${templete_dir}/pc-header.html		>	${target_dir}/${filename_out}
	echo "<!-- Generator: wpgen.sh $Revision: 2.5 $ -->" >> ${target_dir}/${filename_out}
	cat ${templete_dir}/menu.html			>>	${target_dir}/${filename_out}
	cat ${templete_dir}/pc-left.html		>>	${target_dir}/${filename_out}
	cat ${filename}							>>	${target_dir}/${filename_out}
	cat ${templete_dir}/pc-footer.html		>>	${target_dir}/${filename_out}
done

mv ${target_dir}/* ${release_dir}

echo "$0: generating ${target_dir} end"


# backup
#-------
# generate frame and mobile webpage
#----------------------------------
#	2line page, frame, requires menu.html
#target_dir="frame"
##reformat=`sed "s/\.\/data/\.\.\/data/"`
#
#rm -rf ${target_dir}
#mkdir ${target_dir}
#
#echo "# $0: start generating ${target_dir}"
#
#echo "# $0:generating index.html"
#cat ${templete_dir}/index-frame.html > ${target_dir}/index.html
#echo "# $0:generating menu.html"
#cat ${templete_dir}/menu-header			>	${target_dir}/menu.html
#echo "<!-- Generator: wpgen.sh $Revision: 2.5 $ -->" >> ${target_dir}/menu.html
#echo "<base target=\"main_frame\">"		>>	${target_dir}/menu.html
#echo "<H2> <small>menu</small> </H2>"	>>	${target_dir}/menu.html
#echo "<small>"							>>	${target_dir}/menu.html
#cat ${templete_dir}/menu.html			>>	${target_dir}/menu.html
#echo "</small>"							>>	${target_dir}/menu.html
#cat ${templete_dir}/menu-footer			>>	${target_dir}/menu.html
#echo "# $0:generating top.html"
#cat ${templete_dir}/frame-header		>	${target_dir}/top.html
#cat ${templete_dir}/top.html			>>	${target_dir}/top.html
#cat ${templete_dir}/pc-footer			>>	${target_dir}/top.html
#
#for filename in src/*; do
#filename_out=${filename#src/}
#echo "# $0:generating ${target_dir}/${filename_out}"
#cat ${templete_dir}/frame-header				>	${target_dir}/${filename_out}
#echo "<!-- Generator: wpgen.sh $Revision: 2.5 $ -->" >> ${target_dir}/${filename_out}
#cat ${filename}| sed "s/\.\/data/http:\/\/kgussan\.ojaru\.jp\/data/"	>>	${target_dir}/${filename_out}
#cat ${templete_dir}/pc-footer					>>	${target_dir}/${filename_out}
#done
#mv ${target_dir} ${release_dir}
#

# generate mobile webpage
#------------------------
## 1line page, frame, requires menu.html, it's own prefix and suffix.
#
#target_dir="mobile"
#echo "# $0: start generating ${target_dir}"
#for filename in src/*; do
#filename_out=${filename#src/}
#echo "# $0:generating ${target_dir}/${filename_out}"
#cat ${templete_dir}/prefix_${target_dir}	>	${target_dir}/$filename_out
#cat ${filename}							>>	${target_dir}/$filename_out
#cat ${templete_dir}/suffix_${target_dir}	>>	${target_dir}/$filename_out
#done
## generate menu.html
##-------------------
#cat ${templete_dir}/prefix_menu			>	${target_dir}/menu.html
#cat ${templete_dir}/menu.html						>>	${target_dir}/menu.html
#cat ${templete_dir}/suffix_menu			>>	${target_dir}/menu.html
## generate index.html
##--------------------
#cat ${templete_dir}/prefix_${target_dir}	>	${target_dir}/index.html
#cat ${filename}							>>	${target_dir}/index.html
#cat ${templete_dir}/suffix_${target_dir}	>>	${target_dir}/index.html
#echo "# $0: end generating ${target_dir}"


# generating page-moved-notification
#-----------------------------------
echo "$0: generating page-moved-notification start"
mkdir ${release_dir}/pc
cp -f ${templete_dir}/page-moved-notification.html ${release_dir}/pc/index.html
echo "$0: generating page-moved-notification end"


# generate base data
#-------------------
echo "$0: refreshing data files start"
#### default page ####
cp ${templete_dir}/config.xml ${release_dir}
cp ${templete_dir}/favicon.ico ${release_dir}
cp ${templete_dir}/kg.css ${release_dir}
cp -rf data_org ${release_dir}/data
#### frame page ####
#cp ${templete_dir}/config.xml ${release_dir}/frame/
#cp ${templete_dir}/favicon.ico ${release_dir}/frame/
#cp ${templete_dir}/kg.css ${release_dir}/frame/
#cp -rf data_org ${release_dir}/frame/data
echo "$0: refreshing data files end"



# end. display date and time
#---------------------------
echo "$0: "
echo "$0: summary report"
echo "$0: --------------"
time_end=`date +%s`
time_taken=`expr ${time_end} - ${time_start}`
date_current=`date +%Y%m%d_%R`
echo "$0:   time: ${time_taken} seconds"
echo "$0:   date: ${date_current}"

#================================================================

