Skip to content

Instantly share code, notes, and snippets.

@tuxillo
Created November 22, 2014 13:16
Show Gist options
  • Save tuxillo/1b54805d49cf84899913 to your computer and use it in GitHub Desktop.
Save tuxillo/1b54805d49cf84899913 to your computer and use it in GitHub Desktop.
#! /bin/sh
#
# Copyright (c) 2014 The DragonFly Project. All rights reserved.
#
# This code is derived from software contributed to The DragonFly Project
# by Antonio Huete <[email protected]>
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name of The DragonFly Project nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific, prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# set -x
#! /bin/sh
dports_dir=/usr/dports
wrkdir=
logdir=
logfile=
tgtdir=
distdir=
mkenvvars=
npkg=0
maxwidth=16
rmprev=0
categories="irc"
verbose=0
#
# info message
# Display an informational message, typically under
# verbose mode.
info()
{
local msg=$1
shift
[ ${verbose} -gt 0 ] && echo "INFO: " ${msg} $*
}
#
# err exitval message
# Display an error and exit.
#
err()
{
exitval=${1:-1}
shift
echo 1>&2 "ERROR: $*"
exit $exitval
}
#
# runcmd failonerr cmd
# Execute a command
runcmd()
{
local failonerr=$1
local logf=${logfile}
shift
local cmd=$*
# If we don't have a logfile yet, dump the output
[ ! -f ${logfile} ] && logf="/dev/null"
echo "Run: " ${cmd} >> ${logf}
${cmd} >> ${logf} 2>&1
if [ $? -ne 0 -a ${failonerr} -eq 1 ]; then
err 1 "Command failed: " ${cmd}
fi
}
#
# countpkg
# Counts all packages to be processed
countpkg()
{
# Iterate allowed categories and count all directories
# and while here specify maximum width
for category in ${categories}
do
for dir in ${dports_dir}/${category}/*
do
[ -d ${dir} ] && npkg=$(( npkg + 1 ))
tmp=$(echo ${dir} | wc -c)
if [ ${tmp} -gt ${maxwidth} ]; then
maxwidth=${tmp}
fi
done
done
}
#
# setup
# Do some initial tasks required for dports extraction
setup()
{
local mkdir_flags="-p"
# local timestamp=$(date +'%Y%m%d%H%M%S')
# Directories and flag setup
wrkdir=${wrkdir:-$HOME/temp/dports}
tgtdir=${tgtdir:-${wrkdir}/extracted}
logdir=${logdir:-${wrkdir}/log}
logfile=${logfile:-${logdir}/extraction.log}
distdir=${distdir:-${wrkdir}/distdir}
[ ${verbose} -gt 0 ] && mkdir_flags=${mkdir_flags}" -v"
countpkg # Sum up all elements to be processed
#
# Cleanup tgtdir and logdir only, if requested
# Normally it makes no sense to remove distdir
if [ ${rmprev} -gt 0 ]; then
info "Cleaning up prior executions."
rm -fr ${tgtdir}
rm -fr ${logdir}
fi
info "Creating directory for the extraction: "${wrkdir}
for dir in ${wrkdir} ${tgtdir} ${logdir} ${distdir}
do
[ ! -d ${dir} ] && runcmd 1 mkdir ${mkdir_flags} ${dir}
done
# Make sure logfile exists
touch ${logfile}
# Make specific environment variables
mkenvvars="WRKDIRPREFIX=${tgtdir} DISTDIR=${distdir} BATCH=yes INSTALL_AS_USER=yes"
}
#
# cleanup
# A little cleanup
cleanup()
{
# Post-extraction cleanup
}
#
# process_category category
# Do the actual tasks required for dports extraction/patching
process_category()
{
local category=$1
local count=1
local src=""
local tgt=""
for dir in ${dports_dir}/${category}/*
do
if [ -d ${dir} ]; then
# Advertise which package we're handling
[ ${verbose} -gt 0 ] && \
printf " ==> %-${maxwidth}s [%d/%d]\n" ${dir} ${count} ${npkg}
# Actual processing
# 1. Change to port dir
# 2. Cleanup if requested
# 3. Perform 'make patch' on each port
# 4. Move our WRKSRC to the tgtdir
# 5. Remove all *.log *.bak *.orig *.rej
# 6. Dispose "work" directory
runcmd 1 cd ${dir}
if [ ${rmprev} -gt 0 ]; then
runcmd 1 make ${mkenvvars} rmconfig
runcmd 1 make ${mkenvvars} clean
fi
runcmd 1 make ${mkenvvars} patch
# Deal with work and WRKSRC
src=$(make ${mkenvvars} -VWRKSRC)
tgt=$(make ${mkenvvars} -VWRKDIR)
# Remove all *.log *.bak *.orig *.rej
runcmd 0 find ${src} \( \
-iname "*.log" -o \
-iname "*.bak" -o \
-iname "*.orig" -o \
-iname "*.orig" -o \
-iname "*.rej" \) -delete
runcmd 1 mv ${src} ${tgt%/*}
rm -fr $tgt
# Next port
count=$(( count + 1))
fi
done
}
#
# USAGE -
usage()
{
exitval=${1:-1}
echo "Usage: ${0##*/} [-hrv] [-w wordir]"
exit $exitval
}
# ---------------------------------
# Handle options
while getopts hrw:v op
do
case $op in
v)
verbose=1
;;
r)
rmprev=1
;;
w)
wrkdir=$OPTARG
;;
h)
usage 0
;;
*)
usage
;;
esac
done
shift $(($OPTIND - 1))
# Make sure we have what we need to begin!
setup
# Iterate allowed categories
for category in ${categories}
do
info "Working on category" ${category}
process_category ${category}
done
# Cleanup
cleanup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment