#!/bin/bash

if [ ! -d "libraries" ] ; then
    echo "Error: Create a symlink named \"libraries\" to the \"libraries\" directory of a precompiled GHC" 
    exit
fi

if [ ! -d "current" ] ; then
    echo "Error: Create a symlink named \"current\" to the target version directory" 
    exit
fi


function concat() { 
	echo $*
	# sed -e ':a;$!N;s/ *\n *//g;ta' <<< "$1"
}

integer_simple=$(concat \
	GHC/Integer/Type.hs \
	GHC/Integer.hs \
	GHC/Integer/Logarithms.hs \
	GHC/Integer/Logarithms/Internals.hs \
	GHC/Integer/Simple/Internals.hs \
	)

#for f in $integer_simple
#do
#	./gen-sapl-dir integer-simple "$f"
#done

integer_gmp=$(concat \
	GHC/Integer/GMP/Prim.hs \
	GHC/Integer/Type.lhs \
	GHC/Integer.lhs \
	GHC/Integer/Logarithms/Internals.hs \
	GHC/Integer/Logarithms.hs \
	)

for f in $integer_gmp
do
	./gen-sapl-dir integer-gmp "$f"
done

prim=$(concat \
	GHC/Magic.hs \
	GHC/IntWord64.hs \
	GHC/Classes.hs \
	GHC/CString.hs \
	GHC/Tuple.hs \
	GHC/Types.hs \
	)

for f in $prim 
do
    ./gen-sapl-prim ghc-prim $f
done

baseghc=$(concat \
	GHC/Base.lhs \
	GHC/Show.lhs \
	Numeric.hs \
	GHC/ST.lhs \
	GHC/Exception.lhs \
	GHC/Read.lhs \
	GHC/Err.lhs \
	GHC/Int.hs \
	GHC/Real.lhs \
	GHC/Num.lhs \
	GHC/Fingerprint/Type.hs \
	GHC/TypeLits.hs \
	GHC/Float.lhs \
	GHC/Arr.lhs \
	GHC/IOArray.hs \
	GHC/Enum.lhs \
	GHC/List.lhs \
	GHC/Char.hs \
	GHC/Fingerprint.hs \
	GHC/Word.hs \
	GHC/IO.hs \
	GHC/Ptr.lhs \
	GHC/Foreign.hs \
	GHC/ForeignPtr.hs \
	GHC/Generics.hs \
	GHC/IORef.hs \
	GHC/STRef.lhs \
	GHC/IOArray.hs \
	GHC/Weak.lhs \
	GHC/IP.hs \
	GHC/Unicode.hs \
	GHC/Constants.hs \
	GHC/Desugar.hs \
	GHC/Pack.lhs \
	GHC/Storable.lhs \
	GHC/MVar.hs \
	Prelude.hs \
	)

for f in $baseghc
do
    	./gen-sapl-dir base "$f"
done

deepseq=$(concat \
	Control/DeepSeq.hs \
	)

for f in $deepseq
do
 	./gen-sapl-dir deepseq "$f"
done

array=$(concat \
	Data/Array.hs \
	Data/Array/Base.hs \
	Data/Array/IArray.hs \
	Data/Array/MArray.hs \
	Data/Array/IO/Internals.hs \
	Data/Array/MArray/Safe.hs \
	Data/Array/Storable/Internals.hs \
	Data/Array/Unsafe.hs \
	Data/Array/IO.hs \
	Data/Array/IO/Safe.hs \
	Data/Array/ST.hs \
	Data/Array/ST/Safe.hs \
	Data/Array/Storable.hs \
	Data/Array/Storable/Safe.hs \
	Data/Array/Unboxed.hs \
	)

for f in $array
do
	./gen-sapl-dir array "$f"
done

control=$(concat \
	Unsafe/Coerce.hs \
	Control/Category.hs \
	Control/Monad.hs \
	Control/Exception/Base.hs \
	Control/Applicative.hs \
	Control/Monad/Fix.hs \
	Control/Monad/ST/Imp.hs \
	Control/Monad/ST/Strict.hs \
	Control/Monad/ST/Lazy/Imp.hs \
	Control/Monad/ST/Lazy/Safe.hs \
	Control/Monad/ST/Lazy/Unsafe.hs \
	Control/Monad/ST/Lazy.hs \
	Control/Monad/ST/Safe.hs \
	Control/Monad/ST/Unsafe.hs \
	Control/Monad/ST.hs \
	Control/Monad/Instances.hs \
	Control/Monad/Zip.hs \
	Control/Arrow.hs \
	Control/Exception.hs \
	Control/Concurrent/Chan.hs \
	Control/Concurrent/MVar.hs \
	Control/Concurrent/QSem.hs \
	Control/Concurrent/QSemN.hs \
	Control/Concurrent/SampleVar.hs \
	Text/ParserCombinators/ReadP.hs \
	Text/ParserCombinators/ReadPrec.hs \
	Text/Read/Lex.hs \
	Text/Read.hs \
	Text/Show/Functions.hs \
	Text/Show.hs \
	Text/Printf.hs \
	GHC/IO/Exception.hs \
	GHC/IO/Buffer.hs \
	GHC/IO/Encoding/Failure.hs \
	GHC/IO/Encoding/Types.hs \
	GHC/IO/Encoding/Latin1.hs \
	GHC/IO/Encoding/UTF8.hs \
	GHC/IO/Encoding/UTF16.hs \
	GHC/IO/Encoding/UTF32.hs \
	GHC/IO/Encoding.hs \
	GHC/Float/ConversionUtils.hs \
	GHC/Float/RealFracMethods.hs \
	Foreign/Ptr.hs \
	Foreign/Storable.hs \
	Foreign/C/Types.hs \
	Foreign/C/String.hs \
	Foreign/C.hs \
	Foreign/Marshal/Utils.hs \
	Foreign/Marshal/Error.hs \
	Foreign/Marshal/Alloc.hs \
	Foreign/Marshal/Array.hs \
	Foreign/Marshal/Safe.hs \
	Foreign/Marshal/Unsafe.hs \
	Foreign/Marshal/Pool.hs \
	Foreign/ForeignPtr/Imp.hs \
	Foreign/ForeignPtr/Safe.hs \
	Foreign/ForeignPtr/Unsafe.hs \
	Foreign/ForeignPtr.hs \
	Foreign/Marshal.hs \
	Foreign/Safe.hs \
	Data/Typeable.hs \
	Data/Ord.hs \
	Data/Int.hs \
	Data/Complex.hs \
	Data/Tuple.hs \
	Data/Ratio.hs \
	Data/List.hs \
	Data/Functor.hs \
	Data/Typeable/Internal.hs \
	Data/String.hs \
	Data/Bits.hs \
	Data/Either.hs \
	Data/Data.hs \
	Data/Monoid.hs \
	Data/Function.hs \
	Data/Bool.hs \
	Data/Maybe.hs \
	Data/Char.hs \
	Data/Eq.hs \
	Data/Foldable.hs \
	Data/Word.hs \
	Data/Traversable.hs \
	Data/Ix.hs \
	Data/Version.hs \
	Data/IORef.hs \
	Data/STRef.hs \
	Data/STRef/Lazy.hs \
	Data/STRef/Strict.hs \
	Data/Fixed.hs \
	Data/Unique.hs \
	Data/Dynamic.hs \
	System/Exit.hs \
	System/Info.hs \
	System/IO/Error.hs \
	System/IO/Unsafe.hs \
	System/Mem/StableName.hs \
	System/Mem/Weak.hs \
	)

for f in $control
do
 	./gen-sapl-dir base "$f"
done

bytestring=$(concat \
	Data/ByteString/Internal.hs \
	Data/ByteString/Unsafe.hs \
	Data/ByteString.hs \
	Data/ByteString/Char8.hs \
	Data/ByteString/Lazy/Internal.hs \
	Data/ByteString/Lazy.hs \
	Data/ByteString/Lazy/Char8.hs \
	)

# not supported
#	Data/ByteString/Lazy/Builder/BasicEncoding/Internal/UncheckedShifts.hs \
#	Data/ByteString/Lazy/Builder/BasicEncoding/Internal.hs \
#	Data/ByteString/Lazy/Builder/BasicEncoding/Internal/Floating.hs \
#	Data/ByteString/Lazy/Builder/BasicEncoding/Binary.hs \
#...
#	Data/ByteString/Lazy/Builder/BasicEncoding/Internal/Base16.hs \
#	Data/ByteString/Lazy/Builder/BasicEncoding/ASCII.hs \
#	Data/ByteString/Lazy/Builder/Internal.hs \
#	Data/ByteString/Lazy/Builder/BasicEncoding.hs \
#	Data/ByteString/Lazy/Builder/ASCII.hs \
#	Data/ByteString/Lazy/Builder.hs \
#	Data/ByteString/Lazy/Builder/Extras.hs \
#	Data/ByteString/Lazy/Builder/BasicEncoding/Extras.hs \

for f in $bytestring
do
	./gen-sapl-dir bytestring "$f"
done

containers=$(concat \
	Data/Set/Base.hs \
	Data/Set.hs \
	Data/StrictPair.hs \
	Data/Map/Base.hs \
	Data/Map/Lazy.hs \
	Data/Map/Strict.hs \
	Data/Map.hs \
	Data/IntSet/Base.hs \
	Data/IntSet.hs \
	Data/IntMap/Base.hs \
	Data/IntMap/Strict.hs \
	Data/IntMap/Lazy.hs \
	Data/IntMap.hs \
	Data/Sequence.hs \
	Data/Tree.hs \
	Data/Graph.hs \
	)

for f in $containers
do
	./gen-sapl-dir containers "$f"
done

pretty=$(concat \
	Text/PrettyPrint/HughesPJ.hs \
	Text/PrettyPrint.hs \
	)

for f in $pretty
do
	./gen-sapl-dir pretty "$f"
done

transformers=$(concat \
	Data/Functor/Product.hs \
	Data/Functor/Compose.hs \
	Data/Functor/Identity.hs \
	Control/Monad/Trans/Class.hs \
	Control/Monad/IO/Class.hs \
	Control/Monad/Trans/Cont.hs \
	Control/Monad/Trans/Error.hs \
	Control/Monad/Trans/Identity.hs \
	Control/Monad/Trans/List.hs \
	Control/Monad/Trans/Maybe.hs \
	Control/Monad/Trans/Reader.hs \
	Control/Monad/Trans/RWS/Lazy.hs \
	Control/Monad/Trans/RWS.hs \
	Control/Monad/Trans/RWS/Strict.hs \
	Control/Monad/Trans/State/Lazy.hs \
	Control/Monad/Trans/State.hs \
	Control/Monad/Trans/State/Strict.hs \
	Control/Monad/Trans/Writer/Lazy.hs \
	Control/Monad/Trans/Writer.hs \
	Control/Monad/Trans/Writer/Strict.hs \
	Data/Functor/Constant.hs \
	Control/Applicative/Lift.hs \
	Control/Applicative/Backwards.hs \
	Data/Functor/Reverse.hs \
	)

for f in $transformers
do
	./gen-sapl-dir transformers "$f"
done

utf8=$(concat \
	Codec/Binary/UTF8/String.hs \
	Codec/Binary/UTF8/Generic.hs \
	Data/String/UTF8.hs \
	Data/ByteString/UTF8.hs \
	Data/ByteString/Lazy/UTF8.hs \
	)

# not supported
#	System/Environment/UTF8.hs
#	System/IO/UTF8.hs

for f in $utf8
do
	./gen-sapl-dir utf8-string "$f"
done

xhtml=$(concat \
	Text/XHtml/BlockTable.hs \
	Text/XHtml/Internals.hs \
	Text/XHtml/Transitional/Elements.hs \
	Text/XHtml/Transitional/Attributes.hs \
	Text/XHtml/Frameset/Attributes.hs \
	Text/XHtml/Strict/Attributes.hs \
	Text/XHtml/Frameset/Elements.hs \
	Text/XHtml/Strict/Elements.hs \
	Text/XHtml/Extras.hs \
	Text/XHtml/Strict.hs \
	Text/XHtml/Frameset.hs \
	Text/XHtml/Table.hs \
	Text/XHtml/Debug.hs \
	Text/XHtml/Transitional.hs \
	Text/XHtml.hs \
	)

for f in $xhtml
do
	./gen-sapl-dir xhtml "$f"
done

ln src/Data -s libraries/binary/Data

binary=$(concat \
	Data/Binary/Builder/Base.hs \
	Data/Binary/Builder/Internal.hs \
	Data/Binary/Builder.hs \
	Data/Binary/Get.hs \
	Data/Binary/Put.hs \
	Data/Binary.hs \
	)

for f in $binary
do
	./gen-sapl-dir binary "$f"
done

locale=$(concat \
	System/Locale.hs \
	)

for f in $locale
do
	./gen-sapl-dir old-locale "$f"
done

time=$(concat \
	Data/Time/Clock/CTimeval.hs \
	Data/Time/Clock/Scale.hs \
	Data/Time/Calendar/Private.hs \
	Data/Time/Calendar/MonthDay.hs \
	Data/Time/Calendar/Days.hs \
	Data/Time/Calendar/OrdinalDate.hs \
	Data/Time/Calendar/Gregorian.hs \
	Data/Time/Calendar/WeekDate.hs \
	Data/Time/Calendar/JulianYearDay.hs \
	Data/Time/Calendar/Julian.hs \
	Data/Time/Clock/UTC.hs \
	Data/Time/Clock/POSIX.hs \
	Data/Time/Clock/UTCDiff.hs \
	Data/Time/Clock.hs \
	Data/Time/Calendar.hs \
	Data/Time/Calendar/Easter.hs \
	Data/Time/Clock/TAI.hs \
	Data/Time/Format/Parse.hs \
	Data/Time/Format.hs \
	Data/Time.hs \
	)

# not supported
#	Data/Time/LocalTime/TimeZone.hs \
#	Data/Time/LocalTime/TimeOfDay.hs \
#	Data/Time/LocalTime/LocalTime.hs \
#	Data/Time/LocalTime.hs \

for f in $time
do
	./gen-sapl-dir time "$f"
done