#!/bin/bash

TMP_TEX_FILE=tmptex.tex
rm -f $TMP_TEX_FILE

IMG_LIST_FOR_PDF=image_list_for_pdf.txt



PIC_DIR=pic

TARGET_MAX_WIDTH=520
TARGET_MAX_HEIGHT=720




function write_to_tex_file() {
   echo "$1"   >>   ${TMP_TEX_FILE}
}



STR="\\documentclass[nofonts]{ctexbook}"
write_to_tex_file "$STR"

TEXT_HEIGHT_IN_PT=597 # below layout setting \textheight 210mm is 597pt
STR="%\\usepackage[paperwidth=185mm,paperheight=260mm,text={148mm,210mm},left=21mm,includehead,vmarginratio=1:1]{geometry}  %《LaTeX2e完全手册》版面设置"
write_to_tex_file "$STR"
STR="\\usepackage[paperwidth=185mm,paperheight=260mm,text={148mm,210mm},left=21mm,includehead,vmarginratio=1:1,bottom=25mm]{geometry}"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="\\usepackage{fontspec}"
write_to_tex_file "$STR"
STR="\\usepackage{xeCJK}"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="\\usepackage{layout}"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="\\usepackage{graphicx}"
write_to_tex_file "$STR"
STR="%\\usepackage[space,multidot]{grffile}"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="%\\usepackage{tikz,ifthen}"
write_to_tex_file "$STR"
STR="%\\usetikzlibrary{calc}"
write_to_tex_file "$STR"
write_to_tex_file ""


STR="\\usepackage{hyperref}"
write_to_tex_file "$STR"
STR="\\hypersetup{hidelinks}"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="\\usepackage{tabularx}"
write_to_tex_file "$STR"
write_to_tex_file ""


STR="\\setCJKmainfont{FZWBK.TTF}"
write_to_tex_file "$STR"

STR="\\setCJKfamilyfont{fzkai}{FZKTK.TTF}"
write_to_tex_file "$STR"
STR="%\\newcommand*{\\fzkaiti}{\\CJKfamily{fzkai}}"
write_to_tex_file "$STR"
write_to_tex_file ""


STR="\\begin{document}"
write_to_tex_file "$STR"
STR="\\pagestyle{empty}"
write_to_tex_file "$STR"
write_to_tex_file ""
STR="%\\layout"
write_to_tex_file "$STR"
write_to_tex_file ""
write_to_tex_file ""

COVER_TEXT_SKIP=100mm
DIR_TEXT1_SKIP=80pt
DIR_TEXT2_SKIP=20pt


STR="\\hrule width0mm"
write_to_tex_file "$STR"
STR="\\vskip $COVER_TEXT_SKIP"
write_to_tex_file "$STR"
STR="\\begin{center}"
write_to_tex_file "$STR"
STR="\\Large{明\\mbox{  }星\\mbox{  }像\\mbox{  }集}"
write_to_tex_file "$STR"
STR="\\end{center}"
write_to_tex_file "$STR"
write_to_tex_file ""

NUM4=0
while [ $NUM4 -lt 2 ] ; do
STR="\\newpage"
write_to_tex_file "$STR"
STR="\\hrule width0mm"
write_to_tex_file "$STR"
STR="\\vskip $DIR_TEXT1_SKIP"
write_to_tex_file "$STR"
STR="\\begin{center}"
write_to_tex_file "$STR"
STR="\\Large{目\\mbox{   }录}"
write_to_tex_file "$STR"
STR="\\end{center}"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="\\vskip $DIR_TEXT2_SKIP"
write_to_tex_file "$STR"

STR="\\begin{tabularx}{135mm}{Xp{90mm}r}"
write_to_tex_file "$STR"

NUM3=0
CURRENT_PAGE_NUM=3
let CURRENT_PAGE_NUM+=1
for i in $*  ;  do
   let NUM3+=1
   TOTALNUM=`wc -l $i/$IMG_LIST_FOR_PDF | awk '{print $1}'`
   STR="\\hyperlink{label${NUM3}}{$i}  & \\makebox[85mm][s]{$\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot\\cdot$} &  \\hyperpage{$CURRENT_PAGE_NUM}\\\\"
   write_to_tex_file "$STR"
#	\hyperlink{label2}{vivian2}  & $\cdot\cdot$ &  \hyperpage{3}\\
   let CURRENT_PAGE_NUM+=$TOTALNUM
   let CURRENT_PAGE_NUM+=1
done
STR="\\end{tabularx}"
write_to_tex_file "$STR"
write_to_tex_file ""

let NUM4+=1
done

NUM2=0
for i in $*  ;  do
    if ! [ -d $i ] ; then
	    echo "dir:$i does not exist, exit"
	    exit
    else
            echo "handle $i"	    
    fi
    if ! [ -e "$i/$IMG_LIST_FOR_PDF"  ] ; then
	    echo "list:$i/$IMG_LIST_FOR_PDF   does not exist, exit"
	    exit
    fi
    DSTDIR="$i/$PIC_DIR"
    if ! [ -e "$DSTDIR"  ] ; then
	    echo "pic storage:$DSTDIR  does not exist, exit"
	    exit
    fi

    let NUM2+=1
    STR="\\newpage"
    write_to_tex_file "$STR"
    STR="\\hrule width0mm"
    write_to_tex_file "$STR"
    STR="\\vskip $COVER_TEXT_SKIP"
    write_to_tex_file "$STR"
    STR="\\begin{center}"
    write_to_tex_file "$STR"
    STR="\\hypertarget{label${NUM2}}{\\Large{$i}}"
    write_to_tex_file "$STR"
    STR="\\end{center}"
    write_to_tex_file "$STR"
    write_to_tex_file ""
    write_to_tex_file ""


TOTALNUM=`wc -l $i/$IMG_LIST_FOR_PDF | awk '{print $1}'`
echo "$TOTALNUM files"
NUM=0
while [ $NUM -lt $TOTALNUM ] ; do
    let NUM+=1

    FILENAME=`cat $i/$IMG_LIST_FOR_PDF | sed -n ''$NUM','$NUM' p'`
    echo "${FILENAME}"

    FILE_WH=`identify -format "%wx%h" $DSTDIR/"${FILENAME}"`
    X_POS=`expr index $FILE_WH "x"`
    FILE_W=${FILE_WH:0:$X_POS-1}
    FILE_H=${FILE_WH:$X_POS}
    TMP1=`echo "$FILE_H * 25.4" | bc`
    TMP2=`echo "scale=3;$TMP1 / 96" | bc`
    TMP3=`echo "scale=3;$TMP2 / 0.351" | bc`
    FILE_H_IN_PT=`echo "scale=0;$TMP3 / 1.0" | bc`

    echo "  $FILE_W x $FILE_H($FILE_H_IN_PT pt)"
    DST_PATH=$DSTDIR/"${FILENAME}"


    STR="\\newpage"
    write_to_tex_file "$STR"

    STR="\\hrule width0mm"
    write_to_tex_file "$STR"

    #STR="\\vskip xxx"
    #write_to_tex_file "$STR"
#    if  [ $FILE_H -lt $TARGET_MAX_HEIGHT ] ; then
     # will we use the above ?
     if  ! [ $FILE_H -gt $TARGET_MAX_HEIGHT ] ; then 
        TMP4=`expr $TEXT_HEIGHT_IN_PT - $FILE_H_IN_PT`
        LOWERING=`expr $TMP4 / 2`
        LOWERING_LESS=`expr $LOWERING - 20`
        #STR="\\vskip ${LOWERING}pt"      # vertically centering
        STR="\\vskip ${LOWERING_LESS}pt"  # raise a little
        write_to_tex_file "$STR"
    else
        echo "why? the pic height is larger than maximum, exit."
        exit	    
    fi

    STR="\\vskip -10pt"
    write_to_tex_file "$STR"

    STR="\\mbox{}"
    write_to_tex_file "$STR"

    STR="\\begin{center}"
    write_to_tex_file "$STR"

    #STR="\\includegraphics[width=$FILE_W,height=$FILE_H]{$DST_PATH}"
    #write_to_tex_file "$STR"
    if  [ $FILE_W -eq $TARGET_MAX_WIDTH ] ; then
       STR="\\includegraphics[width=392pt]{$DST_PATH}"
    elif  [ $FILE_H -eq $TARGET_MAX_HEIGHT ] ; then
       STR="\\includegraphics[height=542pt]{$DST_PATH}"
    else
      #STR="\\includegraphics[width=$FILE_W,height=$FILE_H]{$DST_PATH}"
       STR="\\includegraphics[height=${FILE_H_IN_PT}pt]{$DST_PATH}"
    fi
    write_to_tex_file "$STR"

    STR="\\end{center}"
    write_to_tex_file "$STR"


    write_to_tex_file ""
    write_to_tex_file ""
done

done


STR="\\vfill\\eject"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="\\newpage"
write_to_tex_file "$STR"
STR="\\mbox{}"
write_to_tex_file "$STR"
write_to_tex_file ""

STR="\\end{document}"
write_to_tex_file "$STR"
