#!/bin/sh

PREFIX=${PREFIX:-/usr/local}
WRKDIR=${WRKDIR:-`pwd`}

tmprfx=`basename $0`

Usage="\
byte_compile [options] emacs_name port_name [files]
	-l load_el	: load *.el file when byte-compile
	-h		: show this message
	emacs_name	: should be one of below
			  \"emacs\", \"emacs20\", \"mule\", \"xemacs19\", \"xemacs20\",
			  \"xemacs21\", \"xemacs-mule\"
	port_name	: port name(replaced port_name.el when files not specified)
	files		: *.el files should be compiled"

while [ -z "`getopts "l: h" opt > /tmp/${tmprfx}:getopt_err.log`" \
	-a X"${opt}" != "X?" ]; do
    case ${opt} in
	l)
	    load_el=${OPTARG}
	    break
	    ;;
	h)
	    echo "${Usage}"
	    exit 1
	    ;;
	*)
	    ;;
    esac
#    echo "opt=${opt},OPTARG=${OPTARG}"
done

if [ -s /tmp/${tmprfx}:getopt_err.log ]; then
    cat /tmp/${tmprfx}:getopt_err.log
    rm -f /tmp/${tmprfx}:getopt_err.log
    exit 1
fi
rm -f /tmp/${tmprfx}:getopt_err.log

shift `expr ${OPTIND} - 1`

if [ -z "$1" -o -z "$2" ]; then
    echo "${Usage}"
    exit 1
fi

emacs_name=$1
shift
port_name=$1

if [ $# -le 1 ]; then
    files=${port_name}.el
else
    files=`echo $* | sed -e "s/${port_name} *//"`
fi

#echo "emacs_name=${emacs_name}"
#echo "port_name=${port_name}"
#echo "files=${files}"
#echo "load_el=${load_el}"
#exit 1

case ${emacs_name} in
    emacs)
	elispdir=${PREFIX}/share/emacs/site-lisp
	load_path=${PREFIX}/share/emacs/site-lisp/${load_el}
	emacscmd=emacs
	;;
    emacs20)
	elispdir=${PREFIX}/share/emacs/site-lisp
	load_path=${PREFIX}/share/emacs/site-lisp/${load_el}
	emacscmd=emacs20
	;;
    mule)
	elispdir=${PREFIX}/share/mule/site-lisp
	load_path=${PREFIX}/share/mule/site-lisp/${load_el}
	emacscmd=mule
	;;
    xemacs19)
	elispdir=${PREFIX}/lib/xemacs/site-lisp
	load_path=${PREFIX}/lib/xemacs/site-lisp/${load_el}
	emacscmd=xemacs
	;;
    xemacs20)
	elispdir=${PREFIX}/lib/xemacs/site-lisp
	load_path=${PREFIX}/lib/xemacs/site-lisp/${load_el}
	emacscmd=xemacs
	;;
    xemacs21)
	elispdir=${PREFIX}/lib/xemacs/site-packages/lisp/${port_name}
	load_path=${PREFIX}/lib/xemacs/site-packages/lisp/${load_el}
	pkg_path=${PREFIX}/lib/xemacs/site-packages
	emacscmd=xemacs
	package_install=yes
	;;
    xemacs-mule)
	elispdir=${PREFIX}/lib/xemacs/site-packages/lisp/${port_name}
	load_path=${PREFIX}/lib/xemacs/site-packages/lisp/${load_el}
	pkg_path=${PREFIX}/lib/xemacs/site-packages
	emacscmd=xemacs
	package_install=yes
	;;
    *)
	echo "${Usage}"
	exit 1
	;;
esac
if [ -n "${load_el}" ]; then
    echo "(setq load-path (cons \"${load_path}\" load-path))" > /tmp/${tmprfx}:load.el
fi
for f in ${files}; do
    if [ -f ${WRKDIR}/${f} ]; then
	f_elc=`basename ${f} .el`.elc
	if [ -n "${load_el}" ]; then
	    ${emacscmd} -batch -l /tmp/${tmprfx}:load.el -q -no-site-file \
		-f batch-byte-compile ${WRKDIR}/${f}
	else
	    ${emacscmd} -batch -q -no-site-file -f batch-byte-compile ${WRKDIR}/${f}
	fi
	if [ X"${WRKDIR}" != "X${elispdir}" -a -d ${elispdir} ]; then
	    rm -f ${elispdir}/${f_elc}
	    ln -sf ${WRKDIR}/${f} ${elispdir}/${f}
	    install -c -m 444 -g bin -o bin ${WRKDIR}/${f_elc} ${elispdir}/${f_elc}
	    rm -f ${WRKDIR}/${f_elc}
	fi
	if [ $? -eq 0 -a -n "${package_install}" -a -d ${elispdir} ]; then
	    cp ${pkg_path}/pkginfo/MANIFEST.${port_name} /tmp/${tmprfx}:tempfile
	    grep -v "${f_elc}" /tmp/${tmprfx}:tempfile \
		> ${pkg_path}/pkginfo/MANIFEST.${port_name}
	    echo "lisp/${port_name}/${f_elc}" >> ${pkg_path}/pkginfo/MANIFEST.${port_name}
	fi
    else
	echo "\"${WRKDIR}/${f}\": file not found!"
    fi
done

rm -f /tmp/${tmprfx}:load.el /tmp/${tmprfx}:tempfile
