Monday, October 19, 2009

A look at a *good* PBI install script from 2007.

In looking around my ~/Projects folder, I found an old PBI install script I wrote a couple years back. When I was working on a TexLive PBI (only me and Oko were interested), I wrote a very robust script. Most PBI that use install scripts should be nearly like this but normally, they only handle the GUI case and screw you if you use text mode or have special need.


This script was written to the published API at the time, which basically amounted to $INSTALLMODE being set to tell you if you were in a GUI or not; a variable to tell you what name your PBI would have inside /Programs. This was an improvement over using "$1" everywhere, as was required in previous versions of the PBI API.

Here is my old script:

#!/bin/sh

# the year of this release, e.g. 2007, 2008 e.t.c. -> This should be equal to
# the Program Version we set in PBC.
YEAR="2007"

# the size of all our installed files in $PBI_BASE/texlive/$YEAR/
TEXSIZE="1.1GB" # as a string

# compat check.. should be unecessary but I don't trust PC-BSD. Since users can
# now override our install location and documentation does not talk to us about
# coping with it not being /Programs/$PROGDIR/, abort install rather then risk
# performing undefined/undocumented behavior should /Programs/$PROGDIR be
# overrided.

if [ -d "/usr/Programs" ]; then
    PBI_BASE="/usr/Programs"
elif [ -d "/Programs" ]; then
    PBI_BASE="/Programs"
elif [ -d "/usr/local/MyPrograms" ]; then
    PBI_BASE="/usr/local/MyPrograms"
else
    if [ $INSTALLMODE = "GUI" ]; then
        kdialog --sorry "Can't find PBI Installation Directory... aborting"
    else
        echo "Can't find PBI Installation Directory... aborting"
    fi
    exit 255
fi

# set the path
MY_TEX_PATH="$PBI_BASE/texlive/$YEAR/bin/i386-freebsd/"


# XXX check if we are already installed or improperly installed

if [ -d "$PBI_BASE/texlive/$YEAR" ]; then
    if [ $INSTALLMODE = "GUI" ]; then
        kdialog --sorry "$PROGDIR appears to be already installed, aborting"
    else
        echo "$PROGDIR appears to be already installed, aborting"
    fi
    exit 255
fi

# give the user a chance to abort the installation
if [ $INSTALLMODE = "GUI" ]; then 
    kdialog --warningyesno \
    "The installation requires approximatly $TEXSIZE of disk space Continue?"
    if [ $? -gt 0 ]; then
        exit 1
    fi
else
    echo "The installation requires approximatly $TEXSIZE of disk space"
    echo -n "Continue?[yes/no]"; read GO
    echo $GO | grep -i "no"
    if [ $? = 0 ]; then
        exit 1
    fi
fi


# Do installation
echo 'MSG: Setting up TeX Live directory'
mkdir -p $PBI_BASE/texlive/$YEAR
ln -s $PBI_BASE/texlive/$YEAR $PBI_BASE/$PROGDIR/TEXLIVE_$YEAR

echo "MSG: Installing TeX Live files..."

# extract our texlive installation
cd /$PBI_BASE/texlive/$YEAR/ && \
    lzma d texlive${YEAR}.tar.lzma -so | tar -xpf -

# prompt for a default paper size
if [ $INSTALLMODE = "GUI" ]; then 
    PAPER=`kdialog --combobox "Select default paper size" \
            "A4 Paper" "US Letter"`
else
    echo -n "default paper size [A4 Paper, US Letter]: "; read PAPER
fi

echo $PAPER | grep -i "Letter"
if [ $? -eq 0 ]; then
    texconfig-sys paper letter
fi

echo $PAPER | grep -i "A4"
if [ $? -eq 0 ]; then
    texconfig-sys paper a4
fi


echo "MSG: Updating TeX filename databases"
texconfig-sys rehash


if [ $INSTALLMODE = "GUI" ]; then 
    kdialog --yesno \
    "Congratulations! Installation complete -- test installation?"
    if [ $? = 0 ];then
        cd /tmp/
        $MY_TEX_PATH/pdflatex sample2e.tex && kpdf /tmp/sample2e.pdf
        if [ $? -gt 0 ]; then
            kdialog --error \
        "The test may have failed to load, please report any errors: $?"
        fi
    fi
    echo "MSG: Displaying readme file"
    kwrite $PBI_BASE/$PROGDIR/PBI_README.txt 
else
    # I don't know a programa off hand to use for console output.
    more $PBI_BASE/$PROGDIR/PBI_README.txt
fi


What is wrong with that script? Nothing compared to what is wrong with the PBI script API.


$PROGDIR should expand to /Programs/ProgNameVer instead of ProgNameVer.

The maintainer shouldn't have to treat GUI and TEXT mode differently - the API should do it. In fact, that was the main impetus for the EPI spec creating platform independence. In an EPI, people would have said ask the user a question. Not have to understand shell script and massive toggles for GUI/TEXT mode install.


I believe PBI now also do similiar idiocy in regards to fonts handling that should be done at a higher level. However, according to the officially published documentation, my old script is still the correct way to go with a PBI.


One of the reasons I gave up on Kris Moores PBI, was the brain damaged design, the other reasons were the total miss management of PBI...



Under the draft spec's I wrote for EPI, the above script would have become something like this:

# optional EPIL script for EPI

define question dialog with
        title = "TextLive EPI"
        message = "Please set a default paper size"
        default = "A4 Paper"
        options = ['A4 Paper', 'US Letter']
launch as paper_size

if paper_size == "A4 Paper" then do
        execute "texconfig-sys paper a4"
else
        execute "texconfig-sys paper letter



Now I ask you, if you were not a programmer, which one do you understand? Then let me ask you, if you just want to get on with your life, which would you rather write?


Both the PBI sh and EPI EPIL scripts would result in a simple dialog asking you to choose from one of two options. If you know how to use TexLive, the EPIL script would be totally unnecessary because you already know how to change paper sizes. That means you don't even have to write a script at all in a normal case.



End result? Oh so simpler.

No comments:

Post a Comment