;; -*- mode: Scheme; tab-width: 2; -*- ;;
(cond-expand
 [no-library-checks
	(define-syntax check-libraries
		(syntax-rules ()
			[(check-libraries [lib fun] ...)
			 #t]))]
 [else
	(define-syntax check-libraries
		(syntax-rules ()
			[(check-libraries [lib fun] ...)
			 (and (find-library lib fun) ...)]))])

(if (check-libraries
		  ["callback" "alloc_trampoline_r"]
			["iup" "IupOpen"]
			["iupim" "IupLoadImage"]
			["iupimglib" "IupImageLibOpen"])
	(begin
		(compile -s -O2 -d1 "iup-base.scm" -j iup-base "-lcallback -liup -liupim -liupimglib")
		(compile -c -O2 -d1 "iup-base.scm" -j iup-base -unit iup-base)
		(compile -s -O2 -d0 "iup-base.import.scm")
		
		(install-extension
		 'iup-base
		 '("iup-base.so" "iup-base.o" "iup-base.import.so" "iup-types.scm")
		 '((version 1.0.2)
			 (static "iup-base.o")
			 (static-options "-lcallback -liup -liupim -liupimglib")))
		
		(compile -s -O2 -d1 "iup-controls.scm" -j iup-controls "-liup -liupcontrols")
		(compile -c -O2 -d1 "iup-controls.scm" -j iup-controls -unit iup-controls)
		(compile -s -O2 -d0 "iup-controls.import.scm")
		
		(install-extension
		 'iup-controls
		 '("iup-controls.so" "iup-controls.o" "iup-controls.import.so")
		 '((version 1.0.2)
			 (static "iup-controls.o")
			 (static-options "-liup -liupcontrols")))
		
		(compile -s -O2 -d1 "iup-dialogs.scm" -j iup-dialogs "-liup")
		(compile -c -O2 -d1 "iup-dialogs.scm" -j iup-dialogs -unit iup-dialogs)
		(compile -s -O2 -d0 "iup-dialogs.import.scm")
		
		(install-extension
		 'iup-dialogs
		 '("iup-dialogs.so" "iup-dialogs.o" "iup-dialogs.import.so")
		 '((version 1.0.2)
			 (static "iup-dialogs.o")
			 (static-options "-liup")))
		
		(if (check-libraries ["iupgl" "IupGLCanvasOpen"])
			(begin
				(compile -s -O2 -d1 "iup-glcanvas.scm" -j iup-glcanvas "-liup -liupgl")
				(compile -c -O2 -d1 "iup-glcanvas.scm" -j iup-glcanvas -unit iup-glcanvas)
				(compile -s -O2 -d0 "iup-glcanvas.import.scm")
				
				(install-extension
				 'iup-glcanvas
				 '("iup-glcanvas.so" "iup-glcanvas.o" "iup-glcanvas.import.so")
				 '((version 1.0.2)
					 (static "iup-glcanvas.o")
					 (static-options "-liup -liupgl"))))
			(warning "IUP GLCanvas not found, some bindings cannot be compiled"))
		
		(if (check-libraries ["iup_pplot" "IupPPlotOpen"])
			(begin
				(compile -s -O2 -d1 "iup-pplot.scm" -j iup-pplot "-liup -liup_pplot")
				(compile -c -O2 -d1 "iup-pplot.scm" -j iup-pplot -unit iup-pplot)
				(compile -s -O2 -d0 "iup-pplot.import.scm")
				
				(install-extension
				 'iup-pplot
				 '("iup-pplot.so" "iup-pplot.o" "iup-pplot.import.so")
				 '((version 1.0.2)
					 (static "iup-pplot.o")
					 (static-options "-liup -liup_pplot"))))
			(warning "IUP PPlot not found, some bindings cannot be compiled"))
		
		(compile -s -O2 -d1 "iup.scm" -j iup)
		(compile -c -O2 -d1 "iup.scm" -j iup -unit iup)
		(compile -s -O2 -d0 "iup.import.scm")
		
		(install-extension
		 'iup
		 '("iup.so" "iup.o" "iup.import.so")
		 '((version 1.0.2)
			 (static "iup.o"))))
	(warning "IUP or ffcall not found, none of the bindings can be compiled"))

(compile -s -O2 -d1 "iup-dynamic.scm" -j iup-dynamic)
(compile -c -O2 -d1 "iup-dynamic.scm" -j iup-dynamic -unit iup-dynamic)
(compile -s -O2 -d0 "iup-dynamic.import.scm")

(install-extension
 'iup-dynamic
 '("iup-dynamic.so" "iup-dynamic.o" "iup-dynamic.import.so")
 '((version 1.0.2)
   (static "iup-dynamic.o")))
