#!/bin/sh
# Set initial variables:
CWD=`pwd`
if [ "$TMP" = "" ]; then
  TMP=/tmp
fi
PKG1=$TMP/emacsbin
PKG2=$TMP/emacmisc
PKG3a=$TMP/elispc1
PKG3b=$TMP/elispc2
PKG3c=$TMP/elispc3
PKG4a=$TMP/elisp1
PKG4b=$TMP/elisp2
PKG4c=$TMP/elisp3
PKG4d=$TMP/elisp4
PKG6=$TMP/emac_nox
PKG7=$TMP/emacinfo

SRC=/devel/manpagesrc
INFO=/devel/info-pages/usr/info
TEX=/devel/texinfo-docs

if [ ! -d $TMP ]; then
  mkdir -p $TMP # location to build the source
fi
for pkg in $PKG1 $PKG2 $PKG3a $PKG3b $PKG3c \
$PKG4a $PKG4b $PKG4c $PKG4d $PKG6 $PKG7 ; do
  if [ ! -d $pkg ]; then
    mkdir -p $pkg # place for the package to be built
  fi
done

# Explode the package framework:
( cd $PKG1 ; explodepkg $CWD/_emacsbin.tar.gz )
( cd $PKG2 ; explodepkg $CWD/_emacmisc.tar.gz )
( cd $PKG3a ; explodepkg $CWD/_elispc1.tar.gz )
( cd $PKG3b ; explodepkg $CWD/_elispc2.tar.gz )
( cd $PKG3c ; explodepkg $CWD/_elispc3.tar.gz )
( cd $PKG4a ; explodepkg $CWD/_elisp1.tar.gz )
( cd $PKG4b ; explodepkg $CWD/_elisp2.tar.gz )
( cd $PKG4c ; explodepkg $CWD/_elisp3.tar.gz )
( cd $PKG4d ; explodepkg $CWD/_elisp4.tar.gz )
( cd $PKG6 ; explodepkg $CWD/_emac_nox.tar.gz )
( cd $PKG7 ; explodepkg $CWD/_emacinfo.tar.gz )

# Function to handle manpage source:
man2gz () { # $1 is source page name, $2 is target name for preformatted
            # output (full path && name) and $3 is the same, but for the
            # source.
  mkdir -p `dirname $2`
  groff -Tascii -mandoc $1 | gzip -9c > $2
  if [ ! "$3" = "" ]; then
    mkdir -p `dirname $3`
    cat $1 > $3 
  fi 
}

echo "+=============+"
echo "| emacs-19.34 |"
echo "+=============+"
cd $TMP
tar xzvf $CWD/emacs-19.34b.tar.gz
cd emacs-19.34
#cat $CWD/emacs-19.34linux.diff | patch -p1
# First let's build a version with X support:
./configure i486-slackware-linux --prefix=/usr --with-x11 --with-x-toolkit
make CFLAGS=-O2 LDFLAGS=-s
cp -a GETTING.GNU.SOFTWARE PROBLEMS INSTALL README $PKG1/usr/doc/emacs-19.34
( cd $PKG1/usr/doc/emacs-19.34 ; chmod 644 * )
cat lib-src/rcs-checkin > $PKG1/usr/bin/rcs-checkin
cat lib-src/rcs2log > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/rcs2log
cat lib-src/vcdiff > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/vcdiff
cd info
rm COPYING dir
for file in * ; do
  cat $file | gzip -9c > $PKG7/usr/info/$file.gz
done
cd ../src
cat emacs > $PKG1/usr/bin/emacs-19.34-with-x11
cd ../lib-src
cat b2m > $PKG1/usr/bin/b2m
cat ctags > $PKG1/usr/bin/ctags
cat etags > $PKG1/usr/bin/etags
cat emacsclient > $PKG1/usr/bin/emacsclient
cat cvtmail > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/cvtmail
cat digest-doc > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/digest-doc
cat emacsserver > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/emacsserver
cat fakemail > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/fakemail
cat hexl > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/hexl
cat movemail > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/movemail
cat profile > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/profile
cat rcs2log > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/rcs2log
cat sorted-doc > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/sorted-doc
cat vcdiff > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/vcdiff
cat yow > $PKG1/usr/libexec/emacs/19.34/i486-slackware-linux/yow
cd ../etc
for page in emacs.1 ctags.1 etags.1 ; do
  cat $page | gzip -9c > $PKG1/usr/man/man1/$page.gz
done
cd ../lisp
cp -a abbrevlist.el blessmail.el cal-menu.el cdl.el cl-specs.el foldout.el \
iso-transl.el ispell4.el loaddefs.el loadup.el paths.el sc.el \
version.el vt-control.el $PKG1/usr/share/emacs/19.34/lisp
( cd $PKG1/usr/share/emacs/19.34/lisp ; chown root.root * ; chmod 644 * )
( cd $PKG1/usr/share/emacs/19.34/lisp ; chmod 755 term )
cd term
cp -a * $PKG1/usr/share/emacs/19.34/lisp/term
( cd $PKG1/usr/share/emacs/19.34/lisp/term ; chown root.root * ; chmod 644 * )
cd ../../etc
cp -a * $PKG2/usr/share/emacs/19.34/etc
cd ../lisp
cp -a {a,b,c,d,e,f,g,h,i}*.elc $PKG3a/usr/share/emacs/19.34/lisp
cp -a {j,k,l,m,n,o,p,q,r}*.elc $PKG3b/usr/share/emacs/19.34/lisp
cp -a {s,t,u,v,w,x,y,z}*.elc $PKG3c/usr/share/emacs/19.34/lisp
chmod 644 $PKG3a/usr/share/emacs/19.34/lisp/*.elc
chmod 644 $PKG3b/usr/share/emacs/19.34/lisp/*.elc
chmod 644 $PKG3c/usr/share/emacs/19.34/lisp/*.elc
cp -a {a,b,c,d,e,f}*.el $PKG4a/usr/share/emacs/19.34/lisp
cp -a {g,h,i,j,k,l}*.el $PKG4b/usr/share/emacs/19.34/lisp
cp -a {m,n,o,p,q,r}*.el $PKG4c/usr/share/emacs/19.34/lisp
cp -a {s,t,u,v,w,x,y,z}*.el $PKG4d/usr/share/emacs/19.34/lisp
chmod 644 $PKG4a/usr/share/emacs/19.34/lisp/*.el
chmod 644 $PKG4b/usr/share/emacs/19.34/lisp/*.el
chmod 644 $PKG4c/usr/share/emacs/19.34/lisp/*.el
chmod 644 $PKG4d/usr/share/emacs/19.34/lisp/*.el
# OK, now we rebuild for people that don't have X:
echo "+====================================+"
echo "| Rebuilding emacs without X support |"
echo "+====================================+"
cd ..
make clean
./configure i486-slackware-linux --prefix=/usr --with-x=no
make CFLAGS=-O2 LDFLAGS=-s
cd src
cat emacs > $PKG6/usr/bin/emacs-19.34-no-x11
cd ../etc
cat DOC-19.34.1 > $PKG6/usr/share/emacs/19.34/etc/DOC-19.34.1
cd ../man
#cp *.texi $TEX

# Build the packages:
( cd $PKG1 ; tar czvf $TMP/emacsbin.tgz . )
( cd $PKG2 ; tar czvf $TMP/emacmisc.tgz . )
( cd $PKG3a ; tar czvf $TMP/elispc1.tgz . )
( cd $PKG3b ; tar czvf $TMP/elispc2.tgz . )
( cd $PKG3c ; tar czvf $TMP/elispc3.tgz . )
( cd $PKG4a ; tar czvf $TMP/elisp1.tgz . )
( cd $PKG4b ; tar czvf $TMP/elisp2.tgz . )
( cd $PKG4c ; tar czvf $TMP/elisp3.tgz . )
( cd $PKG4d ; tar czvf $TMP/elisp4.tgz . )
( cd $PKG6 ; tar czvf $TMP/emac_nox.tgz . )
( cd $PKG7 ; tar czvf $TMP/emacinfo.tgz . )

# Clean up the extra stuff:
if [ "$1" = "--cleanup" ]; then
  rm -rf $TMP/emacs-19.34
  rm -rf $PKG1 $PKG2 $PKG3a $PKG3b $PKG3c $PKG4a $PKG4b $PKG4c $PKG4d \
$PKG6 $PKG7
fi