#!/bin/sh

:<<'########################################################################'

  Usage: make_luajit.sh
    
This script downloads and constructs the luajit extension. See

------------------------------------------------------------------------

Change-log: 
  2011-12-15 First version.

------------------------------------------------------------------------

This script was written in 2011 by vitex from forum.tinycorelinux.net.

To the extent possible under law, the author(s) have dedicated all
copyright and related and neighboring rights to this software to the
public domain worldwide. This software is distributed without any
warranty.

See http://creativecommons.org/publicdomain/zero/1.0/ for a copy 
of the CC0 Public Domain Dedication under which this work is 
distributed.

########################################################################

# Specify the name of the extension.
EXTENSION=luajit

# Specify the base portion of the extension.
EXTENSION0=LuaJIT

#-----------------------------------------------------------------------

# Install the extensions needed to build this extension.
tce-load -wi compiletc.tcz groff.tcz

#-----------------------------------------------------------------------

# Specify the temporary directory that will contain the build components.
TMP=/tmp/tcz

# Specify the directory in which the squashfs files will be assembled.
SQUASHFS=$TMP/squashfs 

# Specify the directory in which the submission package will be assembled.
PACKAGE=$TMP/package

#-----------------------------------------------------------------------

# Specify the location containing the software distribution.
VERSION=2.0.0-beta9
DOWNLOAD=http://luajit.org/download/LuaJIT-$VERSION.tar.gz

#-----------------------------------------------------------------------

# Download if necessary.
tar tzf $TMP/$(basename $DOWNLOAD) 1>/dev/null 2>&1 ||
  wget -O- $DOWNLOAD >$TMP/$(basename $DOWNLOAD)

#-----------------------------------------------------------------------

# Save the download for GPL compliance.
# cp $TMP/$(basename $DOWNLOAD) $PACKAGE

#-----------------------------------------------------------------------

# Compute the name of the directory into which the software will be unpacked.
DIRECTORY=$TMP/$EXTENSION0-$VERSION

#-----------------------------------------------------------------------

# Unpack the software.
rm -fR $DIRECTORY
tar xzf $TMP/$(basename $DOWNLOAD) -C $TMP

#-----------------------------------------------------------------------

export CFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export CXXFLAGS="-march=i486 -mtune=i686 -Os -pipe"
export LDFLAGS="-Wl,-O1"

#-----------------------------------------------------------------------

# Build the software.
{ cd $DIRECTORY ; make
  touch $TMP/TIMESTAMP ; sudo make install ; }

# Create the luajit executable.
ln -sf /usr/local/bin/luajit-$VERSION /usr/local/bin/luajit

#-----------------------------------------------------------------------

# Copy the installed files (those newer than $TMP/TIMESTAMP) from 
# /usr/local to $SQUASHFS/usr/local.
( cd / 
  find usr/local -not -type d -newer $TMP/TIMESTAMP |
    egrep '/bin/|/jit/' |
    cpio -pvd $SQUASHFS
)

#-----------------------------------------------------------------------

# Create $EXTENSION.tcz.info.
cat >$PACKAGE/$EXTENSION.tcz.info <<EOF
Title:          $EXTENSION.tcz
Description:    Lua Just-In-Time Compiler
Version:        $VERSION
Author:         Mike Pall
Original-site:  http://luajit.org/luajit.html
Copying-policy: GNU General Public License Version 2
Size:           SiZeK
Extension_by:   vitex
Comments:       From http://luajit.org/luajit.html:

                  LuaJIT implements the full set of language features
                  defined by Lua 5.1. The virtual machine (VM) is API-
                  and ABI-compatible to the standard Lua interpreter
                  and can be deployed as a drop-in replacement.

                  LuaJIT offers more performance, at the expense of
                  portability. It currently runs on all popular
                  operating systems based on x86 or x64 CPUs (Linux,
                  Windows, OSX etc.) or embedded systems based on ARM
                  (Android, iOS) or PPC CPUs. Other platforms will be
                  supported in the future, based on user demand and
                  sponsoring.

                  ...

                  The FFI library allows calling external C functions
                  and using C data structures from pure Lua code.

                  The FFI library largely obviates the need to write
                  tedious manual Lua/C bindings in C. No need to learn
                  a separate binding language — it parses plain C
                  declarations! These can be cut-n-pasted from C
                  header files or reference manuals. It's up to the
                  task of binding large libraries without the need for
                  dealing with fragile binding generators.
                  
                -----------------------------------------------------

                This extension was built from 

                  $DOWNLOAD

                This extension contains the x86 version of the LuaJIT
                compiler. See the luajit-dev extension for the
                developmental files from the LuaJIT distribution.

                See the LuaJIT web site for extensive documentation.

                -----------------------------------------------------

Change-log:     ----------
          	2010/01/01 First version
Current:	2011/12/14 Upgraded to 2.0.0-beta9
EOF

#-----------------------------------------------------------------------
