#!/bin/sh

set -e

export HOME="${AUTOPKGTEST_TMP:-/tmp}"

# We want to test if the user can build all the packages and applications that
# we ship in Debian.

# Unfortunately one example doesn't work:
# paul@testavoira ~ $ HOME=/tmp lazbuild -B /usr/lib/lazarus/1.6/examples/dockmanager/design/easydockmgrdsgn.lpk
# <snip>...
# /usr/lib/lazarus/1.6/examples/dockmanager/design/registereasydockmgr.pas(44,3) Fatal: (10022) Can't find unit uMakeSite used by RegisterEasyDockMgr
# Fatal: (1018) Compilation aborted

# Also, current (Nov, 2016) I (elbrus) don't know how to build the applications
# with the code inside of the system directory and the result outside of it.

# Quite a lot of the packages also fail, most (if not all) on broken
# dependencies (that are not even in the lazarus source). Ignoring them for
# now, but it needs investigating why.
# paul@testavoira ~ $ HOME=/tmp lazbuild -B /usr/lib/lazarus/1.6/components/tachart/tachartbgra.lpk 
# ERROR: Broken dependency: TAChartBgra 1.0->bgrabitmappack


#FILES="$(find /usr/lib/lazarus/ -name "*.lpk" -or -name "*.lpi")"
FILES="$(find /usr/lib/lazarus/ -name "*.lpk" \
    -and -not -name "easydockmgrdsgn.lpk" \
    -and -not -name "lazdebuggerfp.lpk" \
    -and -not -name "lazdebuggerfpgdbmi.lpk" \
    -and -not -name "lazgoogleapis.lpk" \
    -and -not -name "tachartwmf.lpk" \
    -and -not -name "tachartaggpas.lpk" \
    -and -not -name "tachartfpvectorial.lpk" \
    -and -not -name "tachartbgra.lpk" \
    -and -not -name "anchordockingdsgn.lpk" \
    -and -not -name "lr_codereport_pkg.lpk" \
    -and -not -name "lazreportpdfexport.lpk" \
    -and -not -name "lr_officeimport.lpk" \
    -and -not -name "lr_sqldb.lpk" \
    -and -not -name "lr_extexp.lpk" \
    -and -not -name "lrcairoexport.lpk" \
    -and -not -name "lremailexport.lpk" \
    -and -not -name "lr_dialogdesign.lpk" \
    -and -not -name "lr_add_function.lpk" \
    -and -not -name "lr_zeosdb.lpk" \
    -and -not -name "lr_tdbf.lpk" \
    -and -not -name "lrspreadsheetexport.lpk" \
    -and -not -name "gdbmitestutils.lpk" \
    -and -not -name "lazprojectgroups.lpk" \
    -and -not -name "lazwebextra.lpk" \
    -and -not -name "appforms.lpk" \
    -and -not -name "LazActiveX.lpk" \
    -and -not -name "pascalscriptfcl.lpk" \
    -and -not -name "lazvlc.lpk" \
)"

for FILE in $FILES ; do
    echo
    echo "CI building $FILE"
    lazbuild -B --verbose $FILE
done

# Keeping track of the error message (in order of appearance)
# /usr/lib/lazarus/1.6/examples/dockmanager/design/registereasydockmgr.pas(44,3) Fatal: (10022) Can't find unit uMakeSite used by RegisterEasyDockMgr
## uMakeSite is one directory higher, but not build/installed as unit. Probably the issue.
# /usr/lib/lazarus/1.6/components/lazdebuggers/lazdebuggerfp/fpdebugdebugger.pas(15,3) Fatal: (10022) Can't find unit FpDbgClasses used by FpDebugDebugger
## FpDbgClasses is defined in component/fpdebug on which this package depends. Question put to the list.
# /usr/lib/lazarus/1.6/components/lazdebuggers/lazdebuggerfpgdbmi/fpgdbmidebugger.pp(15,28) Fatal: (10022) Can't find unit FpdMemoryTools used by FpGdbmiDebugger
## Probably similar to the issue above.
# /usr/lib/lazarus/1.6/components/googleapis/reggoogleapi.pp(15,3) Fatal: (10022) Can't find unit googleservice used by reggoogleapi
## googleservice didn't make it to fpc/3.0.0
# /usr/lib/lazarus/1.6/components/tachart/tadrawerwmf.pas(18,3) Fatal: (10022) Can't find unit Windows used by TADrawerWMF
## Sounds like it shouldn't work on non-Windows
# ERROR: Broken dependency: TAChartAggPas 1.0->AggPasLCL
## Debian stripped aggpass, so no suprise
# /usr/lib/lazarus/1.6/components/tachart/tadrawerfpvectorial.pas(17,41) Fatal: (10022) Can't find unit FPVectorial used by TADrawerFPVectorial
## in declared dependency fpvectorialpkg (at least so it seems)
# ERROR: Broken dependency: TAChartBgra 1.0->bgrabitmappack
## Not in lazarus nor in fpc source
# /usr/lib/lazarus/1.6/components/anchordocking/design/registeranchordocking.pas(47,3) Fatal: (10022) Can't find unit AnchorDockStr used by RegisterAnchorDocking
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/lrcodereport/lr_codereport.pas(22,44) Fatal: (10022) Can't find unit LR_Const used by LR_CodeReport
##
# ERROR: Broken dependency: lazreportpdfexport 0.8->pack_powerpdf (>=0.9.10)
##
# ERROR: Broken dependency: lr_OfficeImport 0.0->laz_fpspreadsheet
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/SqlDB/lr_pqconnection.pas(8,32) Fatal: (10022) Can't find unit LR_Class used by LR_PQConnection
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/cairoexport/lr_e_cairo.pas(9,3) Fatal: (10022) Can't find unit Barcode used by lr_e_cairo
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/lrEmailExport/lrEmailExportFilter.pas(12,3) Fatal: (10022) Can't find unit LR_Class used by lrEmailExportFilter
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/DialogControls/lrdialogcontrols.pas(38,36) Fatal: (10022) Can't find unit LR_Class used by LRDialogControls
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/addfunction/lrAddFunctionLibrary.pas(9,3) Fatal: (10022) Can't find unit LR_Class used by lrAddFunctionLibrary
##
# ERROR: Broken dependency: LR_ZeosDB 0.2.2->zcomponent
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/imgexport/lr_e_img.pas(15,22) Fatal: (10022) Can't find unit LR_Class used by LR_e_img
##
# /usr/lib/lazarus/1.6/components/lazreport/source/addons/TDbfDB/lrtdbfdata.pas(8,32) Fatal: (10022) Can't find unit LR_Class used by lrTDbfData
##
# ERROR: Broken dependency: lrSpreadSheetExport 0.9->laz_fpspreadsheet
##
# Write error: Unable to create file "/usr/lib/lazarus/1.6/components/lazdebuggergdbmi/test/gdbmitestutils/gdbmitestutils.pas"
## Writting not allowed...
# Write error: Unable to create file "/usr/lib/lazarus/1.6/components/projectgroups/lazprojectgroups.pas"
## Writting not allowed...
# Write error: Unable to create file "/usr/lib/lazarus/1.6/components/fpweb/lazwebextra.pas"
## Writting not allowed...
# /usr/lib/lazarus/1.6/components/customform/demo/appform.pas(56,6) Fatal: (10022) Can't find unit custforms used by AppForm
##
# /usr/lib/lazarus/1.6/components/activex/activexcontainer.pas(38,32) Fatal: (10022) Can't find unit Windows used by activexcontainer
## Sounds like it shouldn't work on non-Windows
# Write error: Unable to create file "/usr/lib/lazarus/1.6/components/PascalScript/Source/pascalscriptfcl.pas"
## Writting not allowed...
# /usr/lib/lazarus/1.6/components/vlc/lclvlc.pp(90,47) Error: (5000) Identifier not found "GetXHandle"
##
