#!/bin/bash

### variables
# --------------------------------

# this directory
TLSRCDIR=/opt/home-supp/tl11

# temporaly working directory
TMPDIR=/tmp
#TMPDIR=/var/tmp

# the place of installed TeX Live
TLINSTDIR=/opt/texlive/2011
#TLINSTDIR=/mnt/cd0a

# install directory
PREFIX=/opt/texlive/p2011

# toolkit for [p]xdvi
#TL_CONF_XDVI_TOOLKIT=--with-xdvi-x-toolkit=motif
TL_CONF_XDVI_TOOLKIT=--with-xdvi-x-toolkit=xaw

TL_MAKE_FLAGS=-j3

TL_CONFIGURE_ARGS=
## The following args are for generating binaries for tlptexlive
#TL_CONFIGURE_ARGS="--disable-all-pkgs --enable-web2c \
#                   --disable-xdvipdfmx --disable-xetex \
#                   --enable-web-progs --enable-dvipdfmx --enable-mendexk \
#                   --enable-makejvf --enable-pxdvik --enable-dvipsk \
#                   --enable-tetex --enable-texlive "

# for Ubuntu?
# CONFIG_SHELL=$BASH

### do not edit
# --------------------------------
unset TEXINPUTS
unset PTEX_KANJI_ENC
TLV=`basename $TLSRCDIR/texlive-*-source.tar.xz |sed "s/texlive-\(........\).*/\1/"`
SRCDIR=$TMPDIR/texlive-$TLV-source
echo -e "\e[0;32m *** TeX Live source: texlive-$TLV-source.tar.xz ***\e[0m"
ARCH=`ls $SRCDIR/inst/bin/ 2> /dev/null`
PTEX_IN_FILTER=no
TEXMF=$SRCDIR/inst/texmf
XMAP=$TEXMF/fonts/map/dvipdfmx
export TEXMFMAIN=$TEXMF
export ORIGPATH=$PATH
export PATH=$SRCDIR/inst/bin/$ARCH:$PATH
export TL_CONF_XDVI_TOOLKIT TL_CONFIGURE_ARGS TL_MAKE_FLAGS

sedwrite(){
  echo -e "\e[0;33m[rewriting]\e[0m  `basename $1` by \"$2\""
  if [ ! -f "$1" ]; then 
    echo -e "'$1' does not exist"; exit 1
  fi
  perl -pi -e  "$2" "$1" || exit 1
}


zpatch() {
    echo -e "\e[0;33m[patching]\e[0m   `basename $2`"
    if [ ! -f "$2" ]; then 
      echo -e "\e[0;31m`basename %2` does not exist.\e[0m"
      exit 1
    fi
    $PATCH -p$1 -s < $2
    if [ $? -ne 0 ]; then
      if [ "x$PMODE" = "x" ]; then
        echo -e "  \e[0;31mError(s) have occured.\e[0m"
        exit 1
      else 
        echo -e "  \e[0;33mDon't worry about these errors.\e[0m"
      fi
      PMODE=
    fi
}

# simplified tarx()
tarz() {
    echo -e "\e[0;33m[extracting]\e[0m `basename $1`"
    if test ! -f "$1"; then echo "'$1' doesn't exist"; exit 1; fi
    case "$1" in
        *.zip)  unzip -qo "$1" || exit 1;;
        *.xz)   xz    -cd  "$1" | (umask 0; tar xf -) || exit 1;;
        *.gz)   gzip  -cd  "$1" | (umask 0; tar xf -) || exit 1;;
        *.bz2)  bzip2 -cd  "$1" | (umask 0; tar xf -) || exit 1;;
        *.tar)  cat        "$1" | (umask 0; tar xf -) || exit 1;;
    esac
}

while getopts mro OPT
do
  case $OPT in
    "m" ) FLG_PMPOST="TRUE" ;;
    "r" ) FLG_RP="TRUE" ;;
    "o" ) FLG_NOOTF="TRUE" ;;
       * ) echo "Usage: $0 [-m] [-r] [-o]" 1>&2
	  echo "-m: include pMetaPost (experimental)" 1>&2
          echo "-r: parallelize reautoconf" 1>&2
          echo "-o: do not install japanese and otf packages" 1>&2
          exit 1 ;;
  esac
done

MAKE=make
PATCH=patch
gmake   --version > /dev/null 2>&1 && MAKE=gmake ||:
gpatch  --version > /dev/null 2>&1 && PATCH=gpatch ||:


