#!/bin/sh
# Functions used by tkcdlayout

function GetFiles {
 # List files ($maxdepth levels from $cddir - directories only if $dironly=yes)

 if [ -z "$exclude" ]; then exclude="%@%"; fi

 if [ -z "$cddir" ]; then rm -f $lines; else

   (cd $cddir &&
    ( 
      maxdepthOPT=
      typeOPT=
      if [ "x$dironly" = "xyes" ]; then
        typeOPT="-type d"
      fi
      if [ -n "$maxdepth" ]; then
        maxdepthOPT="-maxdepth $maxdepth"
      fi
      find . $maxdepthOPT $typeOPT
    ) | sed -e 's,^./,,' -e 's,_,\\\\_,g' |  \
      grep -v $exclude | grep -v '^.$' | sort ) > $lines
 fi
} 

function DoLayout {
   cat $libdir/CD.latex.begin.$page ${mid} $libdir/CD.latex.end >${lay}/${cdn}.latex &&
   cd ${lay} &&
   latex ${cdn}.latex &&
   dvips ${cdn}.dvi -t a4 -t landscape -o &&
   gv -seascape -media A4 ${cdn}.ps 
}

function Title {
   if [ -n "$Title" ]; then
     if [ -l "$Title" -gt 29 ];then
	if [ -l "$Title" -gt 40 ];then
	    echo "\put(3241,-126){\makebox(0,0)[b]{\Large{$Title}}}" >${mid}
	else
	  echo "\put(3241,-126){\makebox(0,0)[b]{\huge{$Title}}}" >${mid}
	fi
     else
        echo "\put(3241,-126){\makebox(0,0)[b]{\smash{\SetFigFont{34}{40.8}{rm}$Title}}}" >${mid}
     fi
   fi
}

function BigTitle {
   if [ -n "$Title" ]; then
      if [ -l "$Title" -gt 14 ];then
        if [ -l "$Title" -gt 17 ];then
         if [ -l "$Title" -gt 23 ]; then
           if [ -l "$Title" -gt 36 ]; then # tested with 64
             echo "\put(3500,-1726){\makebox(0,0)[b]{\textbf{$Title}}}"  >${mid}
           else # Between 24 and 50 chars => LARGE without scalebox
             echo "\put(3500,-1726){\makebox(0,0)[b]{\LARGE{\textbf{$Title}}}}"  >${mid}
           fi
         else # Between 18 and 23 chars => Large
          echo "\put(3241,-1726){\makebox(0,0)[b]{\scalebox{2}{\large{\textbf{$Title}}}}}"  >${mid}
         fi
        else # Between 14 and 17 chars => LARGE
          echo "\put(3300,-1726){\makebox(0,0)[b]{\scalebox{2}{\LARGE{\textbf{$Title}}}}}"  >${mid}
        fi
      else # Few chars => VERY big
        echo "\put(3241,-1726){\makebox(0,0)[b]{\scalebox{2}{\huge{\textbf{$Title}}}}}"  >${mid}
      fi
   fi
}

function Subtitle {
     if [ -n "$subtitle" ]; then
      if [ -l "$subtitle" -gt 20 ];then
        if [ -l "$subtTitle" -gt 41 ];then
         echo "\put(2000,-3346){\makebox(0,0)[lb]{\smash{$subtitle}}}" >>${mid}
        else
         echo "\put(2566,-3346){\makebox(0,0)[lb]{\smash{\large{$subtitle}}}}" >>${mid}
        fi
      else
       echo "\put(2566,-3346){\makebox(0,0)[lb]{\smash{\SetFigFont{20}{24.0}{rm}$subtitle}}}" >>${mid}
      fi
     fi 
}

function MoveTitle { 
# This function moves up the title, because we need some more space below it
    cat ${mid} | sed -e 's/-126/224/' >/tmp/tmpxxxx
    mv -f /tmp/tmpxxxx ${mid}
}

function WriteLines {
# This function reads lines from $lines and writes them, formatted, to 
#    $mid
  # x values (for each page)
  x1=900
  x2=7000
  
  [ -n "$lines" -a -f "$lines" ] || exit 1;
  
  # Number of lines
  nbl=$(cat $lines | wc -l)
   
  
  if [ $nbl -lt 13 ]; then     # very short ( < 13 )
    y=1400
    interv=270
    font="\SetFigFont{14}{16.8}{rm}" ; pf=""

  elif [ $nbl -lt 16 ]; then     # between 13 and 16
    y=646
    interv=270
    font="\SetFigFont{14}{16.8}{rm}" ; pf=""

  elif [ $nbl -lt 20 ]; then     # between 16 and 20
    y=646
    interv=200
    font="" ; pf=""

  elif [ $nbl -lt 26 ]; then   # between 20 and 26
    y=646
    interv=200
    font="" ; pf=""
    if [ $nbl -gt 21 ]; then y=106 ; MoveTitle; fi

  elif [ $nbl -lt 51 ]; then   # between 26 and 51
    y=376
    interv=200
    font=

  else                         # very long ( > 51 )
    MoveTitle
    y=106
    interv=175
    font="\small{"
    pf="}"
  fi
  
  x=$x1        # First page
  cat $lines | while read a; do
    echo "\put($x,-$y){\makebox(0,0)[lb]{\smash{$font$a$pf}}}" >>${mid}
    y=$[$y+$interv]
    if [ $y -gt 4950 ]; then   # Next page
      y=$[106-3*$interv]
      x=$x2    # Second page
    fi
  done
}

function Warning {  
# This function warns the user if $mid exists and if Warn is set to yes
  if [ -f ${mid} -a "x$Warn" != "xno" ]; then
    echo -n "Warning : ${mid} exists ! Do you want to erase it [y/n] :"
    read answ
    if [ "$answ" = "y" ]; then
	rm -f ${mid}
    else
    exit 1;
    fi
  fi
}

function BackBorders {
# This function draws the vertical titles on the back page
   if [ -n "$Title" ]; then
     echo "\put(270,-4750){\myrotatel{$Title}}" >>${mid}
     echo "\put(7140,380){\myrotater{$Title}}" >>${mid}
   fi
}
