#!/bin/bash ##---------------------------------------------------------------------------- ## File: make_perl_tcz.sh ## Description: Shell script for packaging a Perl module for Tiny Corel Linux ##---------------------------------------------------------------------------- ## $Date: 2011-10-03 15:47:38 -0500 (Mon, 03 Oct 2011) $ ## $Revision: 4351 $ ## $Author: pdurden $ ##---------------------------------------------------------------------------- ## Base directory for the staging area STAGING_BASE=/tmp ## Staging area STAGING_AREA=${STAGING_BASE}/perl ## Instllation directory INSTALL_DIR=${STAGING_AREA}/usr/local ## Local storage for extensions TCZ_CACHE=/home/tc/local-tcz ## Name of the script SCRIPT_NAME=`basename $0` ## Extensions required to complete this script REQUIRED_EXTENSIONS="compiletc perl5 squashfs-tools-4.x bash tar" ## Extensions that are missing MISSING_EXTENSIONS="" ## Name used for Extension_by field in .info field EXTENSION_BY="AlabamaPaul" ## Name of current log file LOG_FILE= TCL_REV=`cat /usr/share/doc/tc/release.txt` PERL_REV=`perl -e "print $^V"` ##**************************************************************************** ## @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 { cd ${STARTING_DIR} if [ ${KEEP} -eq 0 ]; then while [ "$1" != "" ]; do rm -rf $1 &>/dev/null shift done fi } ##**************************************************************************** ## @fn USAGE ## @brief Print the USAGE message for this script ##**************************************************************************** function USAGE { cat <<__USAGE__ USAGE: ${SCRIPT_NAME} {--cache CACHE-DIR} {--keep} CPAN_MODULE_TARBALL CPAN_MODULE_TARBALL - Filename of the CPAN module tarball --cache CACHE-DIR - Cache where the newly built extension will be stored DEFAULT: /home/tc/local-tcz --keep - Do not remove source --force - Force install, even if tests fail DESCRIPTION This script is designed to be used to unpack, build, install, and package CPAN Perl modules into a Tiny Core Linux extension. This script will complete the following steps: 1) Unpack the CPAN module source 2) Create a staging area for installing the module in a PPI compatible method 3) Build the CPAN module 4) Test the CPAN module 5) Install the CPAN module into the PPI staging area compatible method 6) Package the staging area into a Tiny Core Linux Module, creating skeleton versions of the .info, .list, .dep files, and generating the .md5.txt file. 7) Move the generated extension files to the cache directory 8) Clean up the staging area, and CPAN source directory __USAGE__ exit 1 } ##**************************************************************************** ## @fn initialize_staging_area ## @brief Initialize the build directory ##**************************************************************************** function initialize_staging_area { ## Remove the staging area if it already exists [ -d "${STAGING_AREA}" ] && rm -rf ${STAGING_AREA} ## Create the installation directory mkdir -p ${INSTALL_DIR} } ##**************************************************************************** ## @fn create_extension PACKAGE_NAME MODULE_NAME MODULE_VERSION ## @brief Called afer the module is built and installed into the staging area ##**************************************************************************** function create_extension { local PKG_NAME=$1 local MODULE_NAME=$2 local MODULE_VERSION=$3 local SAVED_DIR=`pwd` if [ "${PKG_NAME}" == "" ]; then return 1 fi ## Default MODULE_NAME if one is not provided [ "${MODULE_NAME}" == "" ] && MODULE_NAME=??? ## Default MODULE_VERSION if one is not provided [ "${MODULE_VERSION}" == "" ] && MODULE_VERSION=??? ## Rename the staging area to the package name before ## creating the squashfs file LOG -n " Changing ${STAGING_AREA} to /tmp/${PKG_NAME} ..." mv ${STAGING_AREA} ${STAGING_BASE}/${PKG_NAME} &>/dev/null LOG "DONE" STAGING_AREA=${STAGING_BASE}/${PKG_NAME} LOG -n " Checking for perllocal.pod file..." cd ${STAGING_AREA} local PERLLOCAL_ORIGINAL=`find . -name perllocal.pod` if [ "${PERLLOCAL_ORIGINAL}" != "" ]; then LOG "FOUND!" LOG -n " Removing file..." rm -f "${PERLLOCAL_ORIGINAL}" &>/dev/null LOG "DONE" else LOG "NOT FOUND" fi cd ${STAGING_BASE} LOG -n " Creating ${PKG_NAME}.tcz squashfs file..." mksquashfs ${PKG_NAME} ${PKG_NAME}.tcz &>/dev/null LOG "DONE" LOG -n " Creating listing file ${PKG_NAME}.tcz.list ..." cd ${PKG_NAME} mv ../${PKG_NAME}.tcz . &>/dev/null find usr -not -type d > ${PKG_NAME}.tcz.list LOG "DONE" LOG -n " Creating md5 file ${PKG_NAME}.tcz.md5.txt ..." md5sum ${PKG_NAME}.tcz > ${PKG_NAME}.tcz.md5.txt LOG "DONE" LOG -n " Creating skeleton depends file ${PKG_NAME}.tcz.dep ..." echo -e "perl5.tcz\n\n" >> ${PKG_NAME}.tcz.dep LOG "DONE" LOG -n " Creating skeleton info file ${PKG_NAME}.tcz.info ..." local PKG_DATE=`date +"%Y/%m/%d"` local PKG_SIZE=`du -h ${PKG_NAME}.tcz | cut -f 1` rm -rf usr &>/dev/null cat > ${PKG_NAME}.tcz.info <<__PKG_INFO__ Title: ${PKG_NAME}.tcz Description: Perl module ${MODULE_NAME} Version: ${MODULE_VERSION} Author: see CPAN Original-site: http://cpan.perl.org Copying-policy: GPL Size: ${PKG_SIZE} Extension_by: ${EXTENSION_BY} Comments: Binaries only ---- Created using ${TCL_REV} using perl ${PERL_REV} ---- PPI Compatible ---- Change-log: ${PKG_DATE} First version Current: ${PKG_DATE} __PKG_INFO__ LOG "DONE" LOG -n " Moving files to local cache ${TCZ_CACHE} ..." ## Create the local cache if it does not exist [ ! -d "${TCZ_CACHE}" ] && mkdir -p ${TCZ_CACHE} ## Move all files to the cache mv ${PKG_NAME}.* ${TCZ_CACHE} &>/dev/null LOG "DONE" ## Get rid of all files. LOG -n " Cleaning up..." cd ${SAVED_DIR} rm -rf /tmp/${PKG_NAME} &>/dev/null LOG "DONE" return 0 } ##**************************************************************************** ## MAIN BODY OF SCRIPT ##**************************************************************************** ##-------------------------------------- ## 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 ##-------------------------------------- ## If KEEP != 0, then no files will be deleted during cleanup KEEP=0 MODULES="" FORCE=0 while [ "$1" != "" ]; do case $1 in --keep | -k) KEEP=1 ;; --force | -f) FORCE=1 ;; --help | -h | -?) USAGE;; --cache ) ## Shift off the argument to get at the parameter shift TCZ_CACHE=$1 ;; *) MODULES="${MODULES} $1" ;; esac ## Shift off the argument shift done ##-------------------------------------- ## Make sure we have at least 1 module ##-------------------------------------- if [ "${MODULES}" == "" ]; then echo "ERROR: At least one CPAN module filename must be provided!" USAGE fi ##-------------------------------------- ## Now loop through all modules ##-------------------------------------- STARTING_DIR=$PWD for MODULE in ${MODULES} do ## Start from a known location cd ${STARTING_DIR} ## Make sure the source tarball exists if [ -e "${MODULE}" ]; then ## Now, determine the various parts based on the CPAN filename ## using the perl portion of the script SPLIT_MODULE=`perl -x $0 ${MODULE}` ## Use the bash set command to set the positional parameters set -- ${SPLIT_MODULE} SOURCE=$1 PACKAGE_NAME=$2 MODULE_NAME=$3 MODULE_VERSION=$4 ##--------------------------- ## Set the log filename ##--------------------------- LOG_FILE="${STARTING_DIR}/${SOURCE}.log" LOG "------------------------------------------------------------" LOG "Extension Name: ${PACKAGE_NAME}" LOG "CPAN Module: ${MODULE_NAME} v${MODULE_VERSION}" LOG "LOG File: ${LOG_FILE}" LOG "------------------------------------------------------------" ##--------------------------- ## Unpack the source ##--------------------------- LOG -n "Unpacking module source ${MODULE} ..." tar -xzf ${MODULE} &>/dev/null ## See if there was an error if [ $? -ne 0 ]; then LOG "ERROR" LOG " Could not unpack ${MODULE}" cleanup $SOURCE ${INSTALL_DIR} continue fi echo "DONE!" ##--------------------------- ## Initialize staging area ##--------------------------- LOG -n "Initializing staging area..." initialize_staging_area LOG "DONE" ##--------------------------- ## Create Makefile ##--------------------------- cd $SOURCE LOG "" LOG "Building CPAN module: creating Makefile" perl Makefile.PL PREFIX=${INSTALL_DIR} | tee -a ${LOG_FILE} ## See if there was an error if [ ${PIPESTATUS[0]} -ne 0 ]; then LOG "ERROR: Could not create Makefile" cleanup $SOURCE ${INSTALL_DIR} continue fi ##--------------------------- ## Make the module ##--------------------------- LOG "" LOG "Building CPAN module: building the module" make | tee -a ${LOG_FILE} ## See if there was an error if [ ${PIPESTATUS[0]} -ne 0 ]; then LOG "ERROR: make failed" cleanup $SOURCE ${INSTALL_DIR} continue fi ##--------------------------- ## Test the module ##--------------------------- LOG "Building CPAN module: Testing the module" make test | tee -a ${LOG_FILE} ## See if there was an error if [ ${PIPESTATUS[0]} -ne 0 ]; then if [ ${FORCE} -eq 0 ]; then LOG "ERROR: Tests failed" cleanup $SOURCE ${INSTALL_DIR} continue else LOG "NOTICE: Tests failed, ignoring error because of --force" fi fi ##--------------------------- ## Install the module ##--------------------------- LOG "" LOG "Building CPAN module: Installing the module" make install | tee -a ${LOG_FILE} ## See if there was an error if [ ${PIPESTATUS[0]} -ne 0 ]; then LOG "ERROR: Installation failed" cleanup $SOURCE ${INSTALL_DIR} continue fi ##--------------------------- ## Create the extension ##--------------------------- LOG "" LOG "Packaging the extension" create_extension $PACKAGE_NAME $MODULE_NAME $MODULE_VERSION ## See if there was an error if [ $? -ne 0 ]; then LOG "ERROR: Could not create extension" cleanup $SOURCE ${INSTALL_DIR} continue fi ##--------------------------- ## Cleanup ##--------------------------- cleanup $SOURCE ${INSTALL_DIR} else echo "ERROR: Could not find ${MODULE}" fi done ## Return to our starting location cd ${STARTING_DIR} exit 0 ##---------------------------------------------------------------------------- ## This is the Perl portion of the script which can be invoked with ## perl -x SCRIPTNAME ##---------------------------------------------------------------------------- #!/usr/bin/perl -w ##---------------------------------------------------------------------------- ## This portion of the script is used to take a standard filename for a ## downloaded CPAN module, and create the various parameters the shell script ## needs ##---------------------------------------------------------------------------- use File::Basename; ## Loop through all command line arguments while (@ARGV) { my $dirname = shift; ## Ignore names that aren't tarballs next if ($dirname !~ /\.tar\.gz/); ## Remove any leading path information $dirname = basename($dirname); ## Strip off the .tar.gz $dirname =~ s/\.tar\.gz//; ## Split the name at all dashes - my @parts = split(/-/, $dirname); ## The version number is the last section, ## so use splice to pull off the last element my $module_version = splice(@parts, -1, 1); ## Now create the TCL extension name my $tcz_name = "perl_" . join('_', @parts); ## Now create the Perl module name my $module_name = join('::', @parts); ## Now print out the results print join(' ', $dirname, $tcz_name, $module_name, $module_version), "\n"; }