I don't know if there's a foolproof way to determine the location of the
pkgsrc tree, but I will look into this when I get a chance. pkg_chk
relies on $PKGSRCDIR for example, but I don't know if anything like that
is guaranteed to be defined for every installation.
Attachment:
freshen.1
Description: Binary data
#! /bin/sh
# $NetBSD$
# Copyright (c) 2023 Alistair Crooks <agc%NetBSD.org@localhost>
# All rights reserved.
#
# 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.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 AUTHOR 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.
#
# utility function to warn and then exit
die()
{
echo >&2 "$*"
exit 1
}
# utility function to find an executable
check_prog()
{
_var="$1"; _name="$2"
eval _tmp=\"\$$_var\"
if [ "x$_tmp" != "x" ]; then
# Variable is already set (by the user, for example)
return 0
fi
for _d in `echo $PATH | tr ':' ' '`; do
if [ -f "$_d/$_name" ] && [ -x "$_d/$_name" ]; then
# Program found
eval $_var=\""$_d/$_name"\"
return 1
fi
done
die "$_name not found in path."
}
freshen_version="20230419"
user=""
scmupdate=true
pkgsrc=""
localbase=""
while getopts "Vnu:v" arg; do
case "${arg}" in
V) echo "freshen version ${freshen_version}"
exit 0
;;
o) case "${OPTARG}" in
localbase=*) localbase="${OPTARG#localbase=}"
;;
owner=*) owner="${OPTARG#owner=}"
;;
pkgsrc=*) pkgsrc="${OPTARG#pkgsrc=}"
;;
*) echo "Unknown option \"${OPTARG}\"" >&2
;;
esac
;;
n) scmupdate=false ;;
v) set -x ;;
\?) printf >&2 '%s\n' \
"Usage: $0 [-V] [-n] [-o pkgsrc=dir] [-o owner=owner] \\" \
" [-o localbase=localbase] [-o tool=toolname] [-v] [pkgsrcdir]"
exit 2
esac
done
shift $((OPTIND - 1))
check_prog awk awk
check_prog grep grep
check_prog id id
check_prog ls ls
check_prog make make
check_prog pkg_info pkg_info
check_prog sudo sudo
# check this script is run as root. yeah :(
case "$(${id} -u)" in
0) ;;
*) die "Must be root to run this"
;;
esac
# see if pkgsrc has been passed as an argument
case "${pkgsrc}" in
"") if [ $# -eq 1 ]; then
pkgsrc=$1
fi
;;
esac
# where else would it be?
case "${pkgsrc}" in
"") pkgsrc=/usr/pkgsrc
;;
esac
# if we need to update the sources, do it
if ${scmupdate}; then
case "${pkgsrc}" in
"") die "No pkgsrc directory found"
;;
esac
# now work out which scm tool we're going to use
if [ ! -d "${pkgsrc}" -o ! -d ${pkgsrc}/pkgtools/digest ]; then
die "\"${pkgsrc}\" is not a checked-out pkgsrc tree"
fi
if [ -d ${pkgsrc}/.git ]; then
tool=git
echo "Using git to update ${pkgsrc}"
check_prog git git
elif [ -d ${pkgsrc}/.hg ]; then
tool=hg
echo "Using hg to update ${pkgsrc}"
check_prog hg hg
else
# there are some people who rename cvs directories, so just assume this for now
tool=cvs
echo "Using cvs to update ${pkgsrc}"
check_prog cvs cvs
fi
case "${user}" in
"") user="$(${ls} -al ${pkgsrc}/Makefile | ${awk} '{ print $3}')"
;;
esac
${grep} "^${user}:" /etc/passwd > /dev/null || die "${user} is not an active user on this system"
case "${tool}" in
cvs) updatecmd="${cvs} -q update -dP ." ;;
git) updatecmd="${git} pull" ;;
hg) updatecmd="${hg} pull -u" ;;
"") die "unknown tool \"${tool}\" - please send patches to agc%netbsd.org@localhost"
;;
esac
echo "Updating pkgsrc sources in ${pkgsrc} using ${tool} as ${user}"
(cd ${pkgsrc} && ${sudo} -u "${user}" ${updatecmd})
else
echo "Skipping pkgsrc update"
fi
# discover localbase
case "${localbase}" in
"") localbase=$(cd ${pkgsrc}/pkgtools/digest && ${make} show-var VARNAME=LOCALBASE)
;;
esac
case "${localbase}" in
"") localbase="$(${pkg_info} -Q LOCALBASE digest)"
;;
esac
case "${localbase}" in
"") die "Cannot find localbase directory"
;;
esac
# discover binary package owner
case "${owner}" in
"") owner="$(${ls} -al ${localbase}/bin/digest | ${awk} '{print $3}')" ;;
esac
case "${owner}" in
"") die "Cannot work out binary packagee owner"
;;
esac
echo "Using pkg_rolling-replace to freshen packages in ${localbase} from ${pkgsrc} as ${owner}"
(cd ${pkgsrc} && ${sudo} -u "${owner}" pkg_rolling-replace -rsuv)