#!/bin/bash
# Required system installs:
# subversion command line client - look for package named svn or subversion
# gtk2 development libraries - look for packages named gtk2-devel or libgtk2.0-dev
#
# Assumtions
# pgopher source zip file in home directory
#
# Creates the following directories:
#   pgopher_source - expanded pgopher source files
#   usr/bin - symbolic links to the newly compiled programs
#   lazarus - source for development version of Lazarus
#   compiled - compiled versions of PGOPHER
#
# Symbolic links to the programs are made to ~/usr/bin, so this should be added to your to your PATH.
# 
# Modifications and subsequent compilations can be made through the Lazarus IDE; use startlazarus to launch it.
# On first starting it you may receive a message about ugrading configurations on starting the IDE - just 
# accept the upgrade. You may also see a message about configuration, which again can be accepted.
# On opening the PGOPHER project or an individual form, you may see the message:
#   "Unable to find the component class "TSettingsForm"" - select "Ignore, use TForm as ancestor".
#
# If you want to update the compiler as well make sure COMPILECOMPILER=True is set below. This may not be necessary,
# but may be needed if the compiler version is too old for the current lazarus, or there is an issue with the system 
# supplied files. This requires a bootstrap compiler to be present in your home directory:
# This is  ppcx64 for 64 bit systems, ppc386 for 32 bit systems.
# The simplest way of obtaining this bootstrap compiler is probably to install the version
# of Lazarus using your distributions package system, copy the core compiler (ppc386 or ppcx64), which
# will typically be in /usr/lib/fpc/<version> to your home directory and then uninstall the system 
# Lazarus and fpc (to avoid confusion between versions).
# Alternatively, extract the file from a download for your system from http://www.freepascal.org.
# This will create the following additional directories:
#   pgopher_source - expanded pgopher source files
#   fpc - source for development version of the free Pascal compiler
#   usr - compiled version of free Pascal compiler
#
# Comment out the following line to stop updating the compiler
COMPILECOMPILER=True
set -x
set -e
# Clear out old lazarus settings
rm -rf ~/.lazarus
if [ -z "$ZIP" ] ; then
  ZIP=~/pgopher_source*.zip # zip file containing PGOPHER source
fi
if [ -z "$SOURCE" ] ; then
  SOURCE=~/pgopher_source # Directory containing extracted sources
fi
if [ -z "$FPCSOURCE" ] ; then
  FPCSOURCE=~/fpc # Directory with source for development version of the free Pascal compiler
fi
if [ -z "$FPCCOMPILED" ] ; then
  FPCCOMPILED=~/usr # compiled version of free Pascal compiler
fi
if [ -z "$LAZARUSSOURCE" ] ; then
  LAZARUSSOURCE=~/lazarus # source for development version of Lazarus
fi
if [ -z "$FPSPREADSSOURCE" ] ; then
  FPSPREADSSOURCE=~/fpspreadsheet # source for development version of fpSpreadSheet
fi

# Expand source (if not already present)
if [ ! -d $SOURCE ] ; then 
  cd
  mkdir -p $SOURCE
  cd $SOURCE
  unzip -q $ZIP
  cd
fi

if [ -n "$COMPILECOMPILER" ] ; then
  # compile compiler (and associated libraries)
  if [ -z "$BOOTSTRAP" ] ; then
    # bootstrap version of free Pascal compiler; ppc386 for 32 bit systems
    if [ -f ~/ppcx64 ] ; then
      BOOTSTRAP=~/ppcx64 
    elif [ -f ~/ppc386 ] ; then
      BOOTSTRAP=~/ppc386
    else
      echo "Please set BOOTSTRAP"
    fi
  fi
  if [ ! -e $FPCSOURCE ] ; then
    # Check out development version of fpc
    svn co https://svn.freepascal.org/svn/fpc/trunk $FPCSOURCE
  else
    # if it already exixts, just clean up and update it
    svn cleanup --remove-unversioned --remove-ignored $FPCSOURCE
    svn update $FPCSOURCE
    svn revert -R $FPCSOURCE
  fi
  # Actual compilation
  cd $FPCSOURCE
  make DEBUG=1 install NOGDB=1 PREFIX=$FPCCOMPILED FPC=$BOOTSTRAP
  cd

  # Extract current fpc version
  FPCCOMPILER=`basename $BOOTSTRAP`
  FPCVER=`$FPCSOURCE/compiler/$FPCCOMPILER -iV`

  # Complete install process
  cd $FPCCOMPILED/bin
  if [ ! -L $FPCCOMPILER ] ; then
    ln -s ../lib/fpc/$FPCVER/$FPCCOMPILER
  fi
  cd

  # Generate initial configuration file (if not already present)
  if [ ! -f $FPCCOMPILED/lib/fpc/etc/fpc.cfg ] ; then
    $FPCCOMPILED/lib/fpc/$FPCVER/samplecfg ~/usr/lib/fpc/$FPCVER $FPCCOMPILED/lib/fpc/etc
  fi
else
  if [ ! -e $FPCCOMPILED/bin ] ; then
    mkdir -p $FPCCOMPILED/bin
  fi
fi

# Add ~/usr/bin directory to search PATH
PATH=$FPCCOMPILED/bin:$PATH

if [ ! -e $LAZARUSSOURCE ] ; then
  # Check out development version of lazarus
  svn co https://svn.freepascal.org/svn/lazarus/trunk $LAZARUSSOURCE
else
  # if it already exixts, just clean up and update it
  svn cleanup --remove-unversioned --remove-ignored $LAZARUSSOURCE
  svn update $LAZARUSSOURCE
  svn revert -R $LAZARUSSOURCE
fi

if [ ! -e $FPSPREADSSOURCE ] ; then
  # Check out development version of fpSpreadSheet
  svn co https://svn.code.sf.net/p/lazarus-ccr/svn/components/fpspreadsheet $FPSPREADSSOURCE
else
  # if it already exixts, just update it
  svn cleanup --remove-unversioned --remove-ignored $FPSPREADSSOURCE
  svn update $FPSPREADSSOURCE
  svn revert -R $FPSPREADSSOURCE
fi

# Apply PGOPHER specific patches to lazarus (if any present)
cd $LAZARUSSOURCE
if [ -f $SOURCE/pgopher/patches.diff ] ; then
  patch -p1 <$SOURCE/pgopher/patches.diff
fi

# build lazarus
make

# Add compiled components to path
cd $FPCCOMPILED/bin
if [ ! -L lazarus ] ; then
  ln -s $LAZARUSSOURCE/lazarus
  ln -s $LAZARUSSOURCE/startlazarus
  ln -s $LAZARUSSOURCE/lazbuild
  ln -s $LAZARUSSOURCE/lazres
  ln -s $LAZARUSSOURCE/lrstolfm
fi

cd $SOURCE

# Build pgopher, pgo and tabslave editors
lazbuild --lazarusdir=~/lazarus $FPSPREADSSOURCE/laz_fpspreadsheet.lpk
lazbuild utils/Putils.lpk
lazbuild wutils/gutils.lpk
lazbuild lutils/lutils.lpk 
lazbuild pgopher/pgo.lpi
lazbuild --add-package wutils/Pcontrols.lpk
lazbuild wutils/Pcontrols.lpk
lazbuild lforms/lforms.lpk
lazbuild pgopher/pgopher.lpi
#lazbuild slave/slave.lpi
lazbuild slave/tabslave.lpi
# Add pgopher to path
cd $FPCCOMPILED/bin
ARCH=`fpc -iTP`
OS=`fpc -iTO`
if [ "$OS" == "darwin" ] ; then
  LCL=carbon
else
  LCL=gtk2
fi
if [ ! -L pgopher ] ; then
  ln -s ~/compiled/pgopher/$ARCH-$OS-$LCL/pgopher
  ln -s ~/compiled/pgo/$ARCH-$OS/pgo
  #ln -s ~/compiled/slave/$ARCH-$OS-$LCL/slave
  ln -s ~/compiled/tabslave/$ARCH-$OS-$LCL/tabslave
fi
cd

# Compile lazarus with PGOPHER specific add-ons
cd $LAZARUSSOURCE
./lazbuild --lazarusdir=. --build-ide=
cd

