#!/bin/bash
##----------------------------------------------------------------------------
##        File: make-tcz
## Description: Shell script for packaging a Tiny Corel Linux extension
##              for use with TCL >= 2.7
##----------------------------------------------------------------------------
## Package name
PACKAGE_NAME=partclone
## Base directory for the staging area
STAGING_BASE=/tmp
## Staging area 
STAGING_AREA=${STAGING_BASE}/${PACKAGE_NAME}

## Name of the script
SCRIPT_NAME=`basename $0`

## Directory of the script 
SCRIPT_DIR=`dirname $0`

## Directory to place the TCL extension
EXPORT_DIR=${PWD}/Exports/tinycorelinux

## Directory where source exists
SOURCE_DIR=${PWD}

## Extensions required to complete this script
REQUIRED_EXTENSIONS="squashfs-tools-4.0 bash tar compiletc ntfsprogs"

## Extensions that are missing
MISSING_EXTENSIONS=""

## Name used for Extension_by field in .info field
EXTENSION_BY="alabamapaul"

CONFIG_FLAGS="--enable-extfs --enable-fat --enable-ntfs"

## Name of current log file
LOG_FILE=

## Tiny Core Linux version
TCL_VERSION=`cat /usr/share/doc/tc/release.txt`

## Package version
PACKAGE_VER=""

##****************************************************************************
##     @fn LOG
##  @brief Echos the message to standard out as well as the LOG_FILE
##****************************************************************************
function LOG {
  if [ "${LOG_FILE}" != "" ]; then
    echo $* | tee -a ${LOG_FILE}
  else
    echo $* | tee -a ${LOG_FILE}
  fi
}

##****************************************************************************
##     @fn check_for_missing_extensions
##  @brief Check to see if the required extensions are installed
##****************************************************************************
function check_for_missing_extensions {
  ## /usr/local/tce.installed will contain a file for each extensions loaded
  for CHECK_FOR in ${REQUIRED_EXTENSIONS}
  do
    if [ ! -e "/usr/local/tce.installed/${CHECK_FOR}" ]; then
      MISSING_EXTENSIONS="${MISSING_EXTENSIONS} ${CHECK_FOR}"
    fi
  done
}

##****************************************************************************
##     @fn cleanup
##  @brief Deletes the existing directories if the KEEP variable is 0
##****************************************************************************
function cleanup {

  ## Return to the starting directory
  cd ${STARTING_DIR}  
  
  if [ ${KEEP} -ne 0 ]; then
    return
  fi
  
  ## Get rid of the staging area if it exists
  [ -d "${STAGING_AREA}" ] && rm -rf "${STAGING_AREA}" &>/dev/null
  
}

##****************************************************************************
##     @fn USAGE
##  @brief Print the USAGE message for this script
##****************************************************************************
function USAGE {
  cat <<__USAGE__
USAGE:
${SCRIPT_NAME} VERSION

DESCRIPTION
    This script is designed to be used to build the ${PACKAGE_NAME} package, 
    and create a Tiny Core Linux extension

__USAGE__

 exit 1
}

##****************************************************************************
##     @fn create_extension 
##  @brief Called afer the package is built and installed into the staging area 
##****************************************************************************
function create_extension {
local SAVED_DIR=`pwd`
  
  LOG "Creating ${PACKAGE_NAME} package"
  
  cd ${STAGING_BASE}
  LOG -n "  Creating ${PACKAGE_NAME}.tcz squashfs file..."
  mksquashfs  ${PACKAGE_NAME} ${PACKAGE_NAME}.tcz &>/dev/null
  LOG "DONE"
  
  LOG -n "  Creating listing file ${PACKAGE_NAME}.tcz.list ..."
  cd ${PACKAGE_NAME}
  mv ../${PACKAGE_NAME}.tcz . &>/dev/null
  find usr -not -type d > ${PACKAGE_NAME}.tcz.list
  LOG "DONE"
  
  LOG -n "  Creating md5 file ${PACKAGE_NAME}.tcz.md5.txt ..."
  md5sum ${PACKAGE_NAME}.tcz > ${PACKAGE_NAME}.tcz.md5.txt
  LOG "DONE"
  
  LOG -n "  Creating depends file ${PACKAGE_NAME}.tcz.dep ..."
  echo ntfsprogs.tcz > ${PACKAGE_NAME}.tcz.dep
  LOG "DONE"
  
  local PKG_DATE=`date +"%Y/%m/%d"`
  
  ## Capture the size of the extension
  local PKG_SIZE=`du -h ${PACKAGE_NAME}.tcz | cut -f 1`
  
  ## Clean up
  if [ ${KEEP} -eq 0 ]; then
    rm -rf usr  &>/dev/null
  fi
  
  ## Create the .info file
  LOG -n "  Creating skeleton info file ${PACKAGE_NAME}.tcz.info ..."
  cat > ${PACKAGE_NAME}.tcz.info <<__PKG_INFO__
Title:		${PACKAGE_NAME}.tcz
Description:    Provides utilities to back up and restore used-blocks of a 
		partition and it is designed for higher compatibility of the
		file system by using existing libraries.
Version:	${PACKAGE_VER}
Author:		Thomas Tsai,  Jazz Yao-Tsung Wang, Steven Shiau, Ceasar Sun
Original-site:	http://partclone.org
Copying-policy:	GPL
Size:		${PKG_SIZE}
Extension_by:	${EXTENSION_BY}
Comments: 	Binaries only
		Built with NTFS, FAT, EXT2/3/4 support
		---
		Created using ${TCL_VERSION}
		----
		PPI Compatible 
		----
Change-log:     2010/07/21 First version
Current:        ${PKG_DATE}

__PKG_INFO__
  LOG "DONE"
  
  ## Move the files into the Export directory
  LOG -n "  Moving files to export directory ${EXPORT_DIR} ..."
  if [ ! -d "${EXPORT_DIR}" ]; then
    mkdir -p "${EXPORT_DIR}" &>/dev/null
  fi
  
  ## Move all files to the cache
  mv -f ${PACKAGE_NAME}.* ${EXPORT_DIR}/ &>/dev/null
  LOG "DONE"
  
  ## Get rid of all files.
  if [ ${KEEP} -ne 0 ]; then
    return
  fi
  
  LOG -n " Cleaning up..."
  cd ${SAVED_DIR}
  rm -rf  /tmp/${PACKAGE_NAME} &>/dev/null
  LOG "DONE"
}

##****************************************************************************
## MAIN BODY OF SCRIPT
##****************************************************************************
##--------------------------------------
## Make sure we are running TCL
##--------------------------------------
if [ ! -e "/usr/share/doc/tc/release.txt" ]; then
  echo "ERROR: It does not appear that this system is running Tiny Core Linux!"
  echo ""
  USAGE
fi
##--------------------------------------
## Check for missing TCL extensions
##--------------------------------------
check_for_missing_extensions
if [ "${MISSING_EXTENSIONS}" != "" ]; then
  echo "The following required extension(s) are missing: "
  echo "   ${MISSING_EXTENSIONS}"
  echo " "
  echo "Please install or mount these extensions."
  echo " "
  exit 1
fi

##--------------------------------------
## Process the command line 
##--------------------------------------
KEEP=0
while [ "$1" != "" ]; do
  case $1 in
    --help | -h | -?)
      USAGE
      ;;
      
    --keep* | -k)
      KEEP=1
      ;;
      
    *)
      if [ "${PACKAGE_VER}" == "" ]; then
        PACKAGE_VER=$1
      else
        echo "ERROR: Unknown parameter $1"
        echo ""
        USAGE
      fi
      ;;
  esac
  
  ## Shift off the argument
  shift
done

if [ "${PACKAGE_VER}" == "" ]; then
  echo "ERROR: You must provide the package version information!"
  echo ""
  USAGE
fi

##--------------------------------------
## Setup, and print a banner
##--------------------------------------
STARTING_DIR=$PWD
LOG_FILE="${STARTING_DIR}/${PACKAGE_NAME}-${PACKAGE_VER}.log"
LOG "------------------------------------------------------------"
LOG "Package Name:    ${PACKAGE_NAME}"
LOG "Package Version: ${PACKAGE_VER}"
LOG "LOG File:        ${LOG_FILE}"
LOG "------------------------------------------------------------"
##--------------------------------------
## Make sure the source directory exists
##--------------------------------------
if [ ! -d "${SOURCE_DIR}" ]; then
  LOG "ERROR: Could not locate source directory"
  exit 1
fi

##--------------------------------------
## Remove existing staging area
##--------------------------------------
if [ -d "${STAGING_AREA}" ]; then
  LOG "Removing existing staging area"
  rm -rf "${STAGING_AREA}" &>/dev/null
fi

cd ${SOURCE_DIR}

##--------------------------------------
## Remove existing config
##--------------------------------------
if [ -e Makefile ]; then
  LOG "Looks like configure script has already been run"
  LOG -n "Removing old configuration ..."
  make distclean &>/dev/null
  ## See if there was an error
  if [ $? -ne 0 ]; then
    LOG "ERROR"
    cleanup
    exit 1
  fi
  LOG "DONE!"
fi

##--------------------------------------
## Run configure script
##--------------------------------------
LOG -n "Running configure script..."
./configure --prefix=/usr/local ${CONFIG_FLAGS} &>> ${LOG_FILE}
## See if there was an error
if [ $? -ne 0 ]; then
  LOG "ERROR"
  cleanup
  exit 1
fi
LOG "DONE!"
  
##--------------------------------------
## Make the package
##--------------------------------------
LOG -n "Building the package..."
make &>> ${LOG_FILE}
## See if there was an error
if [ $? -ne 0 ]; then
  LOG "ERROR"
  cleanup
  exit 1
fi
LOG "DONE!"

##--------------------------------------
## Create the staging area
##--------------------------------------
LOG -n "Creating the staging area ${STAGING_AREA} ..."
mkdir -p "${STAGING_AREA}/usr/local"
## See if there was an error
if [ $? -ne 0 ]; then
  LOG "ERROR"
  cleanup
  exit 1
fi
LOG "DONE!"

##--------------------------------------
## Install the package 
##  install-strip will also reduce the 
##  size of binaries by removing all 
##  symbols and debug information
##--------------------------------------
LOG -n "Installing the pacakage to ${STAGING_AREA} ..."
make DESTDIR=${STAGING_AREA} install-strip &>> ${LOG_FILE}
## See if there was an error
if [ $? -ne 0 ]; then
  LOG "ERROR"
  cleanup
  exit 1
fi
LOG "DONE!"

##--------------------------------------
## Create the TCL extension
##-------------------------------------
create_extension

## Return to our starting location
cd ${STARTING_DIR}

exit 0

