nix-tools/lib/util.sh

390 lines
8.1 KiB
Bash
Raw Normal View History

2014-10-08 00:11:53 +02:00
#!/bin/bash
# This program 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; version 2 of the License.
#
# 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.
##
# usage : in_array( $needle, $haystack )
# return : 0 - found
# 1 - not found
##
in_array() {
2014-11-11 23:02:40 +01:00
local needle=$1; shift
local item
for item in "$@"; do
[[ $item = $needle ]] && return 0 # Found
done
return 1 # Not Found
}
# $1: sofile
# $2: soarch
process_sofile() {
# extract the library name: libfoo.so
local soname="${1%.so?(+(.+([0-9])))}".so
# extract the major version: 1
soversion="${1##*\.so\.}"
if [[ "$soversion" = "$1" ]] && (($IGNORE_INTERNAL)); then
continue
fi
if ! in_array "${soname}=${soversion}-$2" ${soobjects[@]}; then
# libfoo.so=1-64
msg "${soname}=${soversion}-$2"
2014-12-27 22:49:23 +01:00
soobjects+=("${soname}=${soversion}-$2")
2014-11-11 23:02:40 +01:00
fi
2014-10-08 00:11:53 +02:00
}
##
# usage : get_full_version( [$pkgname] )
# return : full version spec, including epoch (if necessary), pkgver, pkgrel
##
2014-11-11 23:02:40 +01:00
get_full_version() {
# set defaults if they weren't specified in buildfile
pkgbase=${pkgbase:-${pkgname[0]}}
epoch=${epoch:-0}
if [[ -z $1 ]]; then
if [[ $epoch ]] && (( ! $epoch )); then
echo $pkgver-$pkgrel
else
echo $epoch:$pkgver-$pkgrel
2014-10-08 00:11:53 +02:00
fi
2014-11-11 23:02:40 +01:00
else
for i in pkgver pkgrel epoch; do
local indirect="${i}_override"
eval $(declare -f package_$1 | sed -n "s/\(^[[:space:]]*$i=\)/${i}_override=/p")
[[ -z ${!indirect} ]] && eval ${indirect}=\"${!i}\"
done
if (( ! $epoch_override )); then
echo $pkgver_override-$pkgrel_override
else
echo $epoch_override:$pkgver_override-$pkgrel_override
2014-10-08 00:11:53 +02:00
fi
2014-11-11 23:02:40 +01:00
fi
2014-10-08 00:11:53 +02:00
}
2014-11-11 23:02:40 +01:00
##
# usage: find_cached_package( $pkgname, $pkgver, $arch )
#
# $pkgver can be supplied with or without a pkgrel appended.
# If not supplied, any pkgrel will be matched.
##
find_cached_package() {
local searchdirs=("$PWD" "$PKGDEST") results=()
local targetname=$1 targetver=$2 targetarch=$3
local dir pkg pkgbasename pkgparts name ver rel arch size r results
for dir in "${searchdirs[@]}"; do
[[ -d $dir ]] || continue
2014-11-12 04:15:07 +01:00
for pkg in "$dir"/*.pkg.tar.xz; do
2014-11-11 23:02:40 +01:00
[[ -f $pkg ]] || continue
# avoid adding duplicates of the same inode
for r in "${results[@]}"; do
[[ $r -ef $pkg ]] && continue 2
done
# split apart package filename into parts
pkgbasename=${pkg##*/}
pkgbasename=${pkgbasename%.pkg.tar?(.?z)}
arch=${pkgbasename##*-}
pkgbasename=${pkgbasename%-"$arch"}
rel=${pkgbasename##*-}
pkgbasename=${pkgbasename%-"$rel"}
ver=${pkgbasename##*-}
name=${pkgbasename%-"$ver"}
if [[ $targetname = "$name" && $targetarch = "$arch" ]] &&
pkgver_equal "$targetver" "$ver-$rel"; then
results+=("$pkg")
fi
done
done
case ${#results[*]} in
0)
return 1
;;
1)
printf '%s\n' "$results"
return 0
;;
*)
error 'Multiple packages found:'
printf '\t%s\n' "${results[@]}" >&2
return 1
;;
esac
}
##
# usage: pkgver_equal( $pkgver1, $pkgver2 )
##
pkgver_equal() {
local left right
if [[ $1 = *-* && $2 = *-* ]]; then
# if both versions have a pkgrel, then they must be an exact match
[[ $1 = "$2" ]]
2014-10-08 00:11:53 +02:00
else
2014-11-11 23:02:40 +01:00
# otherwise, trim any pkgrel and compare the bare version.
[[ ${1%%-*} = "${2%%-*}" ]]
2014-10-08 00:11:53 +02:00
fi
}
2014-11-12 02:19:58 +01:00
check_root() {
(( EUID == 0 )) && return
if type -P sudo >/dev/null; then
exec sudo -- "$@"
else
exec su root -c "$(printf ' %q' "$@")"
fi
}
load_vars() {
2014-11-18 17:52:23 +01:00
local var
[[ -f $1 ]] || return 1
2014-11-13 13:33:38 +01:00
for var in {SRC,SRCPKG,PKG,LOG}DEST MAKEFLAGS PACKAGER CARCH GPGKEY; do
2014-11-18 17:52:23 +01:00
[[ -z ${!var} ]] && eval $(grep "^${var}=" "$1")
done
2014-11-18 17:52:23 +01:00
return 0
}
load_config(){
[[ -f $1 ]] || return 1
2014-12-08 23:18:46 +01:00
manjaro_tools_conf="$1"
2014-11-13 16:31:02 +01:00
[[ -r ${manjaro_tools_conf} ]] && source ${manjaro_tools_conf}
2014-12-03 22:59:30 +01:00
######################
2015-01-12 18:05:45 +01:00
# common
2014-12-03 22:59:30 +01:00
######################
if [[ -n ${branch} ]];then
branch=${branch}
else
branch='stable'
fi
2014-12-05 18:42:35 +01:00
if [[ -n ${arch} ]]; then
arch=${arch}
else
arch=$(uname -m)
fi
2015-01-12 01:08:48 +01:00
if [[ -n ${cache_dir} ]];then
cache_dir=${cache_dir}
else
cache_dir='/var/cache/manjaro-tools'
fi
2014-12-07 02:33:21 +01:00
###################
2015-01-12 18:05:45 +01:00
# buildpkg
2014-12-07 02:33:21 +01:00
###################
2014-12-03 22:59:30 +01:00
2015-01-13 13:41:28 +01:00
if [[ -n ${chroots_pkg} ]];then
chroots_pkg=${chroots_pkg}
else
2015-01-13 13:41:28 +01:00
chroots_pkg='/opt/buildpkg'
fi
2015-01-13 13:41:28 +01:00
if [[ -n ${sets_dir} ]];then
sets_dir=${sets_dir}
else
sets_dir="${SYSCONFDIR}/sets"
fi
if [[ -n ${buildset_pkg} ]];then
buildset_pkg=${buildset_pkg}
else
buildset_pkg='default'
fi
2015-01-13 13:41:28 +01:00
2014-11-26 15:53:19 +01:00
if [[ -n ${blacklist_trigger[@]} ]];then
blacklist_trigger=${blacklist_trigger[@]}
else
2015-01-13 01:03:18 +01:00
blacklist_trigger=('eudev' 'upower-pm-utils' 'eudev-systemdcompat')
2014-11-26 15:53:19 +01:00
fi
if [[ -n ${blacklist[@]} ]];then
blacklist=${blacklist[@]}
else
blacklist=('libsystemd')
fi
2014-12-03 22:59:30 +01:00
2014-12-07 02:33:21 +01:00
###################
2015-01-12 18:05:45 +01:00
# buildiso
2014-12-07 02:33:21 +01:00
###################
2014-12-03 22:59:30 +01:00
if [[ -n ${chroots_iso} ]];then
chroots_iso=${chroots_iso}
2014-12-03 22:59:30 +01:00
else
2015-01-13 13:41:28 +01:00
chroots_iso='/opt/buildiso'
2014-12-03 22:59:30 +01:00
fi
if [[ -n ${iso_dir} ]];then
iso_dir=${iso_dir}
else
2015-01-13 13:41:28 +01:00
iso_dir='/opt/manjaro-iso'
fi
2014-12-03 22:59:30 +01:00
if [[ -n ${iso_label} ]];then
iso_label=${iso_label}
else
2014-12-08 00:30:28 +01:00
source /etc/lsb-release
2015-01-12 01:08:48 +01:00
iso_label="MJRO${DISTRIB_RELEASE//.}"
2014-12-03 22:59:30 +01:00
fi
2014-12-04 14:21:42 +01:00
if [[ -n ${iso_version} ]];then
iso_version=${iso_version}
2014-12-08 00:30:28 +01:00
else
source /etc/lsb-release
iso_version=${DISTRIB_RELEASE}
2014-12-03 22:59:30 +01:00
fi
if [[ -n ${manjaro_kernel} ]];then
manjaro_kernel=${manjaro_kernel}
else
2014-12-29 18:25:23 +01:00
manjaro_kernel="linux317"
2014-12-03 22:59:30 +01:00
fi
2014-12-08 00:30:28 +01:00
manjaro_kernel_ver=${manjaro_kernel#*linux}
2014-12-03 22:59:30 +01:00
if [[ -n ${manjaro_version} ]];then
manjaro_version=${manjaro_version}
else
manjaro_version=$(date +%Y.%m)
2014-12-03 22:59:30 +01:00
fi
if [[ -n ${manjaroiso} ]];then
manjaroiso=${manjaroiso}
else
manjaroiso="manjaroiso"
fi
if [[ -n ${code_name} ]];then
code_name=${code_name}
else
2014-12-08 00:30:28 +01:00
source /etc/lsb-release
code_name="${DISTRIB_CODENAME}"
2014-12-03 22:59:30 +01:00
fi
if [[ -n ${img_name} ]];then
img_name=${img_name}
else
img_name=manjaro
fi
if [[ -n ${hostname} ]];then
hostname=${hostname}
else
hostname="manjaro"
fi
if [[ -n ${username} ]];then
username=${username}
else
username="manjaro"
fi
if [[ -n ${install_dir} ]];then
install_dir=${install_dir}
else
install_dir=manjaro
fi
if [[ -n ${plymouth_theme} ]];then
plymouth_theme=${plymouth_theme}
else
plymouth_theme=manjaro-elegant
fi
if [[ -n ${compression} ]];then
compression=${compression}
else
compression=xz
fi
2014-12-07 06:36:46 +01:00
if [[ -n ${password} ]];then
password=${password}
else
password="manjaro"
fi
if [[ -n ${addgroups} ]];then
addgroups=${addgroups}
else
addgroups="video,audio,power,disk,storage,optical,network,lp,scanner"
fi
2014-12-17 02:14:15 +01:00
if [[ -n ${start_systemd} ]];then
start_systemd=${start_systemd}
else
start_systemd=('cronie' 'cupsd' 'tlp' 'tlp-sleep')
fi
2014-12-17 02:14:15 +01:00
if [[ -n ${start_openrc} ]];then
start_openrc=${start_openrc}
else
start_openrc=('cronie' 'cupsd' 'metalog' 'dbus' 'consolekit' 'acpid')
fi
2014-12-17 02:14:15 +01:00
if [[ -n ${start_systemd_live} ]];then
start_systemd_live=${start_systemd_live}
else
2014-12-17 02:14:15 +01:00
start_systemd_live=('bluez' 'NetworkManager' 'ModemManager')
2014-12-12 02:32:58 +01:00
fi
2014-12-17 02:14:15 +01:00
if [[ -n ${start_openrc_live} ]];then
start_openrc_live=${start_openrc_live}
else
2015-01-13 01:03:18 +01:00
start_openrc_live=('bluetooth' 'networkmanager')
2014-12-17 02:14:15 +01:00
fi
return 0
}
2015-01-11 21:09:01 +01:00
# $1: sets_dir
2015-01-11 21:09:01 +01:00
load_sets(){
local prof temp
for item in $(ls $1/*.set); do
2015-01-11 21:09:01 +01:00
temp=${item##*/}
prof=${prof:-}${prof:+|}${temp%.set}
done
echo $prof
}
2015-01-13 13:41:28 +01:00
# $1: sets_dir
# $2: buildset
eval_buildset(){
eval "case $1 in
$(load_sets $2)) is_buildset=true ;;
*) is_buildset=false ;;
esac"
}
2015-01-13 13:41:28 +01:00
load_user_info(){
OWNER=${SUDO_USER:-$USER}
if [[ -n $SUDO_USER ]]; then
eval "USER_HOME=~$SUDO_USER"
else
USER_HOME=$HOME
fi
USER_CONFIG="$USER_HOME/.config"
}