# Make source distributions
# (Must be run using bash...)

################################################################
# Check we have Hugs and Hugs docs
# (Hugs should be a fresh copy - but we don't check that)
################################################################

if [ ! -d hugs -o ! -e win32.tar ]; then
  echo "Not building distribution because hugs and win32.tar aren't available"
  exit 1
fi

################################################################
# Make build directory
################################################################

if [ x$1 = x--fast ]; then

  cd build
  echo "Using existing build directory"

else

  if [ -e build ]; then
    echo "Not building distribution because build already exists"
    exit 1
  fi
  mkdir build
  cd build

  # echo "Copying Hugs to build tree"
  # cp -r ../hugs .

  echo "Getting fresh copy of Hugs from repository"
  cvs checkout -P hugs

fi

################################################################
# Add documentation and binary files
################################################################

echo Copying hugs documentation into source tree

tar xf ../win32.tar

cp win32/hugs.hlp hugs/docs

if [ ! -d hugs/docs/manual-html ]; then
  mkdir hugs/docs/manual-html
fi
cp win32/*.{html,gif} hugs/docs/manual-html

cp    ~hcompile/doc/html/graphics.html              hugs/docs
cp    ~hcompile/doc/html/server.html                hugs/docs
cp -r ~hcompile/doc/html/haskore-html               hugs/docs
cp -r ~hcompile/doc/html/tutorial-1.4-html          hugs/docs/tutorial
cp -r ~hcompile/doc/html/haskell-library-1.4-html   hugs/docs/library
cp -r ~hcompile/doc/html/haskell-report-1.4-html    hugs/docs/report

if [ ! -d hugs/docs/libs-html ]; then
  mkdir hugs/docs/libs-html
fi
cp    ../fptools/ghc/docs/libraries/libs*.html    hugs/docs/libs-html
#cp    ../fptools/ghc/docs/libraries/libs.sgml     hugs/docs
#cp    ../fptools/ghc/docs/libraries/libs.dvi      hugs/docs
#cp    ../fptools/ghc/docs/libraries/libs.ps       hugs/docs

################################################################
# Build lists of files to include in tars
################################################################

# Generate lists of files for inclusion in distributions
# (To be run on a clean set of sources)

echo Adding Win32 stuff
cp -r win32/Win32 hugs/lib
mv hugs/lib/Win32 hugs/lib/win32

echo Adding Fran stuff
cp -r win32/Fran hugs/lib
mv hugs/lib/Fran/SpriteLib.dll hugs

echo Making dev_files small_files bin32_files bin16_files ishield_files

find hugs \
  -name CVS -prune -o \
  \( -type d -a -empty -a -prune \) -o \
  \( ! -name ".cvsignore" -a ! -type d -a -print \) \
> all_files

# The "full release" is everything except CVS files, Win32 files and Fran files
cat all_files \
| grep -v "hugs/lib/win32" \
| grep -v "hugs/lib/Fran" \
| grep -v "hugs/SpriteLib.dll" \
> dev_files

# The "small release" excludes:
# o machine-specific files (icons, resources, lib/win32)
# o machine-specific demos (Animation)
# o large demos (Haskore)
# o all documentation (this seems a bit extreme)

cat all_files \
| grep -v "hugs/icons" \
| grep -v "hugs/docs" \
| grep -v "hugs/src/Tests" \
| grep -v "hugs/src/Expt" \
| grep -v "hugs/src/distrib" \
| grep -v "hugs/src/resource" \
| grep -v "hugs/src/win16" \
| grep -v "hugs/src/win32" \
| grep -v "hugs/lib/win32" \
| grep -v "hugs/lib/Fran" \
| grep -v "hugs/SpriteLib.dll" \
| grep -v "hugs/lib/Graphics" \
| grep -v "hugs/lib/hugs/Graphics.hs" \
| grep -v "hugs/lib/hugs/RandomIO.hs" \
| grep -v "hugs/demos/Haskore" \
| grep -v "hugs/demos/Animation" \
> small_files

################################################################
# The Win 32 binary release excludes source code and includes
# hugs, runhugs and winhugs and the Win32 interface.
# Also omits Animation - which needs Xlib
# Also omits libraries which only work if you recompile.
# Also omits Fran
################################################################

cat all_files \
| grep -v "hugs/src" \
| grep -v "hugs/demos/Animation" \
| grep -v "hugs/lib/hugs/HugsInternals.hs" \
| grep -v "hugs/lib/hugs/GenericPrint.hs" \
| grep -v "hugs/lib/hugs/CVHAssert.hs" \
| grep -v "hugs/lib/Fran" \
| grep -v "hugs/SpriteLib.dll" \
> bin32_files

# Add these lines to the above to make distributions even smaller
# | grep -v "hugs/docs/haskell-library-1.4-html" 
# | grep -v "hugs/docs/haskell-report-1.4-html" 
# | grep -v "hugs/docs/tutorial-1.4-html" 

echo "\
hugs/hugs.exe
hugs/winhugs.exe
hugs/runhugs.exe
" >> bin32_files

# ################################################################
# # The Win 16 binary release excludes source code and includes binaries
# # for hugs, runhugs and winhugs.
# # It also excludes the Graphics library - since it needs Win32.
# # Also omits Int, Word and concurrency libs - names are too long.
# # Also omits libraries which only work if you recompile.
# # We also leave out the Haskell Tutorial which would push us over
# # the one-disk limit.
# ################################################################
# 
# cat all_files \
# | grep -v "hugs/src" \
# | grep -v "hugs/demos/Animation" \
# | grep -v "hugs/lib/Graphics" \
# | grep -v "hugs/lib/hugs/Graphics.hs" \
# | grep -v "hugs/lib/hugs/RandomIO.hs" \
# | grep -v "hugs/lib/win32" \
# | grep -v "hugs/lib/exts/Addr.hs" \
# | grep -v "hugs/lib/exts/Bits.hs" \
# | grep -v "hugs/lib/exts/Channel.hs" \
# | grep -v "hugs/lib/exts/ChannelVar.hs" \
# | grep -v "hugs/lib/exts/ConcBase.hs" \
# | grep -v "hugs/lib/exts/Concurrent.hs" \
# | grep -v "hugs/lib/exts/Int.hs" \
# | grep -v "hugs/lib/exts/SampleVar.hs" \
# | grep -v "hugs/lib/exts/Semaphore.hs" \
# | grep -v "hugs/lib/exts/Word.hs" \
# | grep -v "hugs/lib/hugs/HugsInternals.hs" \
# | grep -v "hugs/lib/hugs/GenericPrint.hs" \
# | grep -v "hugs/lib/hugs/CVHAssert.hs" \
# > bin16_files
# 
# # Add these lines to the above to make distributions even smaller
# # | grep -v "hugs/docs/haskell-library-1.4-html" 
# # | grep -v "hugs/docs/haskell-report-1.4-html" 
# # | grep -v "hugs/docs/tutorial-1.4-html" 
# 
# echo "\
# hugs/hugs.exe
# hugs/winhugs.exe
# hugs/runhugs.exe
# " >> bin16_files

################################################################
# The Installshield release excludes source code and includes a binary
# for the "most appropriate" system.
# Also omits Animation - which needs Xlib
# Also omits libraries which only work if you recompile.
# Does include Fran
################################################################

cat all_files \
| grep -v "hugs/src" \
| grep -v "hugs/demos/Animation" \
| grep -v "hugs/lib/hugs/HugsInternals.hs" \
| grep -v "hugs/lib/hugs/GenericPrint.hs" \
| grep -v "hugs/lib/hugs/CVHAssert.hs" \
> ishield_files

# Add these lines to the above to make distributions even smaller
# | grep -v "hugs/docs/haskell-library-1.4-html" 
# | grep -v "hugs/docs/haskell-report-1.4-html" 
# | grep -v "hugs/docs/tutorial-1.4-html" 
# | grep -v "hugs/lib/Fran"
# | grep -v "hugs/SpriteLib.dll"

echo "\
hugs/hugs.exe
hugs/winhugs.exe
hugs/runhugs.exe
" >> ishield_files

################################################################
# Build source only distribs
################################################################

# Make zipfile from tarfile
makeZip() {
  echo Making $2 from $1
  mkdir  tmp
  cd     tmp
  tar xf ../$1
  zip -rq9 ../$2 .
  cd ..
  rm -rf tmp
  # zip -T $2
}

# Compare a tarfile with the directory it was made from
cmptars() {
  tarfile=${1}
  dir=${2}

  echo "The following files are absent from ${tarfile}"
  mkdir tmp
  cd tmp
  tar xf ../${tarfile}
  cd ..
  diff -r tmp/${dir} ${dir}

  rm -rf tmp
}


# Make standard distribution files (tarfile and zipfile) from a file list
mkdistrib() {
  tarfile=$1.tar
  zipfile=$1.zip
  files=$2

  # Patch modification dates on machine generated files
  # (These dates tend to get scrogged by DOS/CVS)
  touch hugs/src/parser.c

  echo Making ${tarfile} using ${files}
  rm -f ${tarfile} # delete old one, don't add to it!
  tar cf ${tarfile} --files-from=${files}

  # tar tvf ${tarfile}
  # cmptars ${tarfile} hugs

  makeZip ${tarfile} ${zipfile}
}

mkdistrib hugs dev_files
# mkdistrib hugs_small small_files


################################################################
# Build binary and InstallShield distribs
################################################################

# cp win16/hugs.exe        hugs
# cp win16/runhugs.exe     hugs
# cp win16/winhugs.exe     hugs
# mkdistrib hugs16         bin16_files

cp win32/hugs.exe        hugs
cp win32/runhugs.exe     hugs
cp win32/winhugs.exe     hugs
mkdistrib hugs32         bin32_files
mkdistrib hugs_ishield   ishield_files

################################################################
# Finish
################################################################

# gzip *.tar
ls -l

exit 0
