#!/bin/bash
# Copyright (C) 2007 Wolven
# Copyright (c) 2007 Steven Oliver
# Copyright (c) 2007-2008 Tong SUN
#
# {{{ LICENSE:
# This script is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
# }}}
# This is a shell script which will generate a .lzm module from a list
# of Slackware packages. See the included README file for details.
#
# @Script: metabuild
# @Author: Wolven
# @Author: Tong SUN
# @Version: 1.1.1
# ############################################################## &cs ###
# ::::::::::::::::::::::::::::::::::::::::::::::::::::::::: Var Defs :::
# == Script Begin
ROOTDIR=/tmp/build
PKGSLST=packages
downloader=
CWD=`pwd`
# prog_real: prog canonicalized absolute path+name
prog_real=`readlink -f -- $0`
# prog_name: prog base name
prog_name=`basename $prog_real`
# ############################################################## &cs ###
# :::::::::::::::::::::::::::::::::::::::::::::::: Subroutines begin :::
. $prog_real.inc
# {{{ meta module building functions:
function preliminary_check() {
# Check if the script is run as root, if not then exit.
if [ `id -u` != "0" ]; then
prog_abort "You need to be root to run this script." 1
fi
if [ `which curl 2>/dev/null` ]; then
downloader=curl
elif [ `which lynx 2>/dev/null` ]; then
downloader="lynx -source"
else
prog_abort "No proper downloader can be found. Please install curl or lynx first." 1
fi
[ `which wget 2>/dev/null` ] ||
prog_abort "The wget downloader can be found. Please install it first." 1
[ -d $_opt_cache ] || mkdir -p $_opt_cache
}
function update_package_list() {
verbose "Updating package list from '$_opt_mirror$_opt_release/PACKAGES.TXT'. This might take a while... "
$downloader $_opt_mirror$_opt_release/PACKAGES.TXT | awk -F: '/^PACKAGE NAME:/ { pname = $2; sub(/^ +/, "", pname); }; /^PACKAGE LOCATION:/ { ploc = $2; sub(/^ +/, "", ploc); print ploc "/" pname }' > $_opt_cache/$PKGSLST
verbose "Package list '$_opt_cache/$PKGSLST' updated."
exit 0
}
function sanity_check() {
# Check if the $PKGSLST has been downloaded, if not then exit.
[ -s $_opt_cache/$PKGSLST ] ||
prog_abort "You need to download the packages list '$_opt_cache/$PKGSLST' first." 1
}
function download() {
cd $CWD
cd $_opt_cache >/dev/null
verbose -h "Downloading package(s). This might take a while... "
# Download the packages in the 'packagelist' only if you
# don't already have them
echo "$PKGLIST" |
while read line; do
package=$(grep "$line[^/]*\.tgz" $PKGSLST)
verbose "pkg: $line, file: $package"
if [ "$package" ]; then
packagef=$(basename $package)
[ "$packagef" ] ||
prog_abort "Error in the .conf file at line '$line'.\nMake sure only one package can be selected from it." 1
if [ "$(ls $packagef* 2>/dev/null)" ]; then
verbose "Already have package '${package}'"
true
else
verbose -s "Downloading '${package}'"
wget $_opt_mirror$_opt_release/$package
fi
else
prog_warning "Package '$line' not found."
true
fi
done
[ $? -ne 0 ] && exit 1
if [ "$_opt_download" ]; then
verbose "Option --download specified. Exit now."
exit 0
fi
}
function build_meta() {
verbose -h "Installing package(s) to the build directory... "
cd $CWD
[ -d $ROOTDIR ] || mkdir -p $ROOTDIR
[ -d $_opt_dest ] || mkdir -p $_opt_dest
rm -rf $ROOTDIR/*
# Install the packages to the build directory.
echo "$PKGLIST" |
while read line; do
package=$(grep "$line[^/]*\.tgz" $_opt_cache/$PKGSLST)
[ "$package" ] || {
verbose "Package '$line' not found in '$PKGSLST' file. Looking into local cache '$_opt_cache/' instead."
package=$(ls $_opt_cache/"$line"*)
}
if [ "$package" ]; then
packagef=$(basename $package)
execute_cmd$NO_EXEC "ROOT=$ROOTDIR installpkg $_opt_cache/$packagef"
else
prog_warning "Package '$line' not found either in '$PKGSLST' file or local cache."
fi
done
verbose -h "Compressing the module. Please wait... "
# Make the module from the build directory, change permission
# X execute_cmd$NO_EXEC "dir2lzm $ROOTDIR $_opt_dest/$MODULE"
# the above command would fail to include the build directory contents
# if the $ROOTDIR is merely a symlink.
execute_cmd$NO_EXEC "( cd $ROOTDIR; dir2lzm . `readlink -f $_opt_dest`/$MODULE; )"
chmod 644 $_opt_dest/$MODULE
}
function wrap_up() {
# Clean up
if [ $_opt_cleanup ]; then
rm -rf $ROOTDIR
fi
verbose -h "All done. - Your module is ready."
echo
echo "[1;39mSummary:"
echo "[1;39m--------"
echo
echo "[1;39m* [1;33mModule: [1;39m$MODULE"
echo "[1;39m* [1;33mOutput: [1;39m$_opt_dest/$MODULE[0;39m"
echo
echo
}
# }}}
# ############################################################## &cs ###
# :::::::::::::::::::::::::::::::::::::::::::::::: Main script begin :::
[ ${1+T} ] || fhelp
# sourcing init & CLP scripts
. $prog_real.ini
[ -f $HOME/.$prog_name.ini ] &&
. $HOME/.$prog_name.ini
PKGSLST=$PKGSLST.$_opt_release.lst
. $prog_real.clp
if [ "$_opt_check_failed" ]; then
prog_abort "Not all mandatory options are set." 1
exit 1
fi
preliminary_check
if [ $_opt_update ]; then
update_package_list
fi
[ ${1+T} ] || fhelp
conf=$1; shift
[ -s $conf ] ||
prog_abort "meta_module_conf_file '$conf' not found." 1
. $conf
# Name of the module
MODNAME=${MODNAME:-$(echo $conf | get_fname)}
# Version of the module
MODVERS=${MODVERS:-$_opt_release}
# Name of the module file
MODULE=${MODNAME}-${MODVERS}${_opt_tag:+-$_opt_tag}.lzm
verbose "Building the module as '${MODULE}'"
sanity_check
# Building the meta module
download
pre_build
build_meta
post_build
wrap_up
# ============================================================== &ss ===
# :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: end :::