pkgupdate/pkgupdate

115 lines
2.5 KiB
Plaintext
Raw Permalink Normal View History

2021-08-15 18:58:08 +00:00
#!/bin/sh
INSTALLURL=$(awk '/^./ && ! /^#/ { print ; exit }' /etc/installurl)
export PKG_PATH="${INSTALLURL}/$(uname -r)/packages-stable/$(uname -p)/"
2021-08-15 18:58:08 +00:00
CACHE_DIR=/var/cache/pkgupdate/
SILENT=0
2021-08-15 18:58:08 +00:00
# hide output if SILENT is activated
silent() {
if [ "$SILENT" -eq 0 ]
then
echo "$1"
fi
}
# curl can only be used for http:// , it's quite complicated with https://
# due to the way pkg_add calls $FETCH_CMD
2021-08-15 18:58:08 +00:00
def_curl() {
if type curl 2> /dev/null >/dev/null
then
export FETCH_CMD="/usr/local/bin/curl -L -s -q -N"
fi
}
check_cache() {
# creating cache directory
if ! install -d -o root -g wheel -m 755 ${CACHE_DIR}
then
echo "Error when creating the cache directory ${CACHE_DIR}"
exit 1
fi
ftp -o - "$PKG_PATH" 2>/dev/null | grep tgz > ${CACHE_DIR}/index.now
if [ $? -eq 0 ]
then
# check if we already used pkgupdate
if [ ! -f ${CACHE_DIR}/index.previous ]
then
# first time we use it
mv ${CACHE_DIR}/index.now ${CACHE_DIR}/index.previous
else
# compare current output with the previous one
# if they are identical, no changes has been done
if cmp ${CACHE_DIR}/index.now ${CACHE_DIR}/index.previous
then
rm ${CACHE_DIR}/index.now
silent "No changes on the mirror."
2021-08-15 18:58:08 +00:00
exit 0
else
mv ${CACHE_DIR}/index.now ${CACHE_DIR}/index.previous
2021-08-15 18:58:08 +00:00
fi
fi
else
echo "error when downloading the index, return $?"
exit 1
fi
}
do_http() {
silent "Updating using $1 protocol"
2021-08-15 18:58:08 +00:00
check_cache
def_curl
pkg_add -u 2>&1 | grep -v "^Couldn't find updates for "
}
do_https() {
silent "Updating using $1 protocol"
check_cache
pkg_add -u 2>&1 | grep -v "^Couldn't find updates for "
2021-08-15 18:58:08 +00:00
}
unsupported() {
echo "protocol not supported: $1"
exit 1
}
unknown() {
echo "protocol not known for $1"
exit 1
}
2021-08-15 19:01:50 +00:00
if [ "$(id -u)" -ne 0 ]
then
echo "You need to run $0 as root"
exit 1
fi
if [ "$1" = "-q" ]
then
SILENT=1
fi
if sysctl kern.version | grep -E -- "(-current|-beta)" >/dev/null
then
silent "Enabling -current mode"
export PKG_PATH="${INSTALLURL}/snapshots/packages/$(uname -p)/"
fi
2021-08-15 18:58:08 +00:00
case "$INSTALLURL" in
http://*) do_http "http://"
;;
https://*) do_https "https://"
;;
ftp://*) unsupported ftp
;;
scp://*) unsupported scp
;;
*) unknown "$INSTALLURL"
;;
esac