#!/bin/bash

TMP_TEX_FILE=tmptex.tex
rm -f $TMP_TEX_FILE

#SRCDIR="testpic2"
#DSTDIR="testpic3"
DSTDIR="testpic5"

TARGET_MAX_WIDTH=520
TARGET_MAX_HEIGHT=720

TMPFILE=tmppiclist.txt

ls $DSTDIR  >  $TMPFILE

TOTALNUM=`wc -l $TMPFILE | awk '{print $1}'`

NUM=0

echo "$TOTALNUM files"


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="\\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 ""





while [ $NUM -lt $TOTALNUM ] ; do
    let NUM+=1

    FILENAME=`cat $TMPFILE | 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



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

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