2021-01-22 19:44:05 +03:00
#!/usr/bin/env bash
2019-09-24 09:56:39 +03:00
set -eu
2019-12-21 00:49:27 +03:00
2021-03-12 00:00:38 +03:00
SCRIPT = " $( dirname " ${ 0 } " ) "
export SERENITY_ARCH = " ${ SERENITY_ARCH :- i686 } "
2021-04-12 00:21:01 +03:00
HOST_CC = " ${ CC : =cc } "
HOST_CXX = " ${ CXX : =c++ } "
HOST_AR = " ${ AR : =ar } "
HOST_RANLIB = " ${ RANLIB : =ranlib } "
HOST_PATH = " ${ PATH : = } "
HOST_PKG_CONFIG_DIR = " ${ PKG_CONFIG_DIR : = } "
HOST_PKG_CONFIG_SYSROOT_DIR = " ${ PKG_CONFIG_SYSROOT_DIR : = } "
HOST_PKG_CONFIG_LIBDIR = " ${ PKG_CONFIG_LIBDIR : = } "
DESTDIR = "/"
2021-03-11 21:50:44 +03:00
2021-04-03 06:53:41 +03:00
maybe_source( ) {
if [ -f " $1 " ] ; then
. " $1 "
fi
}
2021-04-12 00:21:01 +03:00
2021-04-18 10:13:16 +03:00
enable_ccache( ) {
if command -v ccache & >/dev/null; then
export CC = " ccache ${ CC } "
export CXX = " ccache ${ CXX } "
fi
}
2021-04-12 00:21:01 +03:00
target_env( ) {
maybe_source " ${ SCRIPT } /.hosted_defs.sh "
}
target_env
host_env( ) {
export CC = " ${ HOST_CC } "
export CXX = " ${ HOST_CXX } "
export AR = " ${ HOST_AR } "
export RANLIB = " ${ HOST_RANLIB } "
export PATH = " ${ HOST_PATH } "
export PKG_CONFIG_DIR = " ${ HOST_PKG_CONFIG_DIR } "
export PKG_CONFIG_SYSROOT_DIR = " ${ HOST_PKG_CONFIG_SYSROOT_DIR } "
export PKG_CONFIG_LIBDIR = " ${ HOST_PKG_CONFIG_LIBDIR } "
2021-04-18 10:13:16 +03:00
enable_ccache
2021-04-12 00:21:01 +03:00
}
2021-04-03 06:53:41 +03:00
packagesdb = " ${ DESTDIR } /usr/Ports/packages.db "
2019-05-28 01:02:29 +03:00
2020-02-04 03:07:27 +03:00
MD5SUM = md5sum
if [ ` uname -s` = "OpenBSD" ] ; then
MD5SUM = "md5 -q"
fi
2019-09-24 09:56:39 +03:00
. " $@ "
shift
: " ${ makeopts : =-j $( nproc) } "
: " ${ installopts : = } "
: " ${ workdir : = $port - $version } "
: " ${ configscript : =configure } "
: " ${ configopts : = } "
: " ${ useconfigure : =false } "
: " ${ depends : = } "
: " ${ patchlevel : =1 } "
2021-04-23 14:53:53 +03:00
: " ${ auth_type : = } "
2020-02-04 03:07:27 +03:00
: " ${ auth_import_key : = } "
: " ${ auth_opts : = } "
2021-04-20 19:36:03 +03:00
: " ${ launcher_name : = } "
: " ${ launcher_category : = } "
: " ${ launcher_command : = } "
2019-05-28 02:25:29 +03:00
2019-09-24 09:56:39 +03:00
run_nocd( ) {
2019-05-28 03:58:36 +03:00
echo " + $@ (nocd) "
( " $@ " )
}
2019-09-24 09:56:39 +03:00
run( ) {
echo " + $@ "
( cd " $workdir " && " $@ " )
2019-05-28 01:02:29 +03:00
}
2021-04-20 19:36:03 +03:00
run_replace_in_file( ) {
2019-11-03 12:05:02 +03:00
run perl -p -i -e " $1 " $2
2019-09-24 09:56:39 +03:00
}
2021-04-20 19:36:03 +03:00
install_launcher( ) {
if [ -z " $launcher_name " ] || [ -z " ${ launcher_category } " ] || [ -z " ${ launcher_command } " ] ; then
return
fi
script_name = " ${ launcher_name ,, } "
script_name = " ${ script_name // / } "
mkdir -p $DESTDIR /usr/local/libexec
cat >$DESTDIR /usr/local/libexec/$script_name <<SCRIPT
#!/bin/sh
set -e
cd -- "\$(dirname -- " \$ ( which -- $( printf %q " ${ launcher_command %% * } " ) ) ")"
exec $( printf '%q ' $launcher_command )
SCRIPT
chmod +x $DESTDIR /usr/local/libexec/$script_name
chmod +x $DESTDIR /usr/local/libexec
mkdir -p $DESTDIR /res/apps
cat >$DESTDIR /res/apps/$script_name .af <<CONFIG
[ App]
Name = $launcher_name
Executable = /usr/local/libexec/$script_name
Category = $launcher_category
CONFIG
unset script_name
}
2019-09-24 09:56:39 +03:00
# Checks if a function is defined. In this case, if the function is not defined in the port's script, then we will use our defaults. This way, ports don't need to include these functions every time, but they can override our defaults if needed.
func_defined( ) {
2019-11-03 12:05:02 +03:00
PATH = command -V " $1 " > /dev/null 2>& 1
2019-09-24 09:56:39 +03:00
}
2020-02-04 03:07:27 +03:00
func_defined post_fetch || post_fetch( ) {
:
}
fetch( ) {
if [ " $auth_type " = = "sig" ] && [ ! -z " ${ auth_import_key } " ] ; then
# import gpg key if not existing locally
2020-06-09 22:10:00 +03:00
# The default keyserver keys.openpgp.org prints "new key but contains no user ID - skipped"
# and fails. Use a different key server.
gpg --list-keys $auth_import_key || gpg --keyserver hkps://keyserver.ubuntu.com --recv-key $auth_import_key
2020-02-04 03:07:27 +03:00
fi
2019-09-24 09:56:39 +03:00
OLDIFS = $IFS
IFS = $'\n'
for f in $files ; do
IFS = $OLDIFS
2020-02-04 03:07:27 +03:00
read url filename auth_sum<<< $( echo " $f " )
echo " URL: ${ url } "
2021-04-11 02:01:20 +03:00
# FIXME: Serenity's curl port does not support https, even with openssl installed.
2021-04-11 21:07:32 +03:00
if which curl && ! curl https://example.com -so /dev/null; then
2021-04-11 02:01:20 +03:00
url = $( echo " $url " | sed "s/^https:\/\//http:\/\//" )
fi
2020-02-04 03:07:27 +03:00
# download files
2019-12-23 15:24:56 +03:00
if [ -f " $filename " ] ; then
echo " $filename already exists "
else
2021-04-11 21:07:32 +03:00
if which curl; then
run_nocd curl ${ curlopts :- } " $url " -L -o " $filename "
else
run_nocd pro " $url " > " $filename "
fi
2020-02-04 03:07:27 +03:00
fi
# check md5sum if given
if [ ! -z " $auth_sum " ] ; then
if [ " $auth_type " = = "md5" ] || [ " $auth_type " = = "sha256" ] || [ " $auth_type " = = "sha1" ] ; then
echo " Expecting ${ auth_type } sum: $auth_sum "
if [ " $auth_type " = = "md5" ] ; then
calc_sum = " $( $MD5SUM $filename | cut -f1 -d' ' ) "
elif [ " $auth_type " = = "sha256" ] ; then
calc_sum = " $( sha256sum $filename | cut -f1 -d' ' ) "
elif [ " $auth_type " = = "sha1" ] ; then
calc_sum = " $( sha1sum $filename | cut -f1 -d' ' ) "
fi
echo " ${ auth_type } sum( $filename ) = ' $calc_sum ' "
if [ " $calc_sum " != " $auth_sum " ] ; then
# remove downloaded file to re-download on next run
rm -f $filename
echo " ${ auth_type } sum's mismatching, removed erronous download. Please run script again. "
exit 1
fi
fi
fi
# extract
if [ ! -f " $workdir " /.${ filename } _extracted ] ; then
case " $filename " in
2021-04-11 02:01:20 +03:00
*.tar.gz| *.tgz)
run_nocd tar -xzf " $filename "
run touch .${ filename } _extracted
; ;
2020-02-04 03:07:27 +03:00
*.tar.gz| *.tar.bz| *.tar.bz2| *.tar.xz| *.tar.lz| .tbz*| *.txz| *.tgz)
2021-04-11 02:01:20 +03:00
run_nocd tar -xf " $filename "
2020-02-04 03:07:27 +03:00
run touch .${ filename } _extracted
; ;
*.gz)
run_nocd gunzip " $filename "
run touch .${ filename } _extracted
; ;
*.zip)
run_nocd bsdtar xf " $filename " || run_nocd unzip -qo " $filename "
run touch .${ filename } _extracted
; ;
*.asc)
run_nocd gpg --import " $filename " || true
; ;
*)
echo " Note: no case for file $filename . "
; ;
esac
2019-12-23 15:24:56 +03:00
fi
done
2020-02-04 03:07:27 +03:00
# check signature
if [ " $auth_type " = = "sig" ] ; then
2021-04-11 02:01:20 +03:00
if $NO_GPG ; then
echo "WARNING: gpg signature check was disabled by --no-gpg-verification"
2020-02-04 03:07:27 +03:00
else
2021-04-11 02:01:20 +03:00
if $( gpg --verify $auth_opts ) ; then
echo "- Signature check OK."
else
echo "- Signature check NOT OK"
for f in $files ; do
rm -f $f
done
rm -rf " $workdir "
echo " Signature mismatching, removed erronous download. Please run script again."
exit 1
fi
2020-02-04 03:07:27 +03:00
fi
fi
post_fetch
}
func_defined patch_internal || patch_internal( ) {
# patch if it was not yet patched (applying patches multiple times doesn't work!)
2019-09-24 09:56:39 +03:00
if [ -d patches ] ; then
2021-01-17 10:14:27 +03:00
for filepath in patches/*.patch; do
2020-02-04 03:07:27 +03:00
filename = $( basename $filepath )
if [ ! -f " $workdir " /.${ filename } _applied ] ; then
run patch -p" $patchlevel " < " $filepath "
run touch .${ filename } _applied
fi
2019-09-24 09:56:39 +03:00
done
2019-05-28 19:55:49 +03:00
fi
}
2020-04-15 15:54:23 +03:00
func_defined pre_configure || pre_configure( ) {
:
}
2019-09-24 09:56:39 +03:00
func_defined configure || configure( ) {
2021-01-22 19:09:40 +03:00
chmod +x " ${ workdir } " /" $configscript "
2021-03-11 23:58:18 +03:00
run ./" $configscript " --host= " ${ SERENITY_ARCH } -pc-serenity " $configopts
2019-05-28 19:55:49 +03:00
}
2021-02-01 21:40:19 +03:00
func_defined post_configure || post_configure( ) {
:
}
2019-09-24 09:56:39 +03:00
func_defined build || build( ) {
2019-11-03 12:05:02 +03:00
run make $makeopts
2019-05-28 11:25:39 +03:00
}
2019-09-24 09:56:39 +03:00
func_defined install || install( ) {
2021-04-03 06:53:41 +03:00
run make DESTDIR = $DESTDIR $installopts install
2021-04-20 19:36:03 +03:00
install_launcher
2019-05-28 02:25:29 +03:00
}
2020-03-25 17:54:30 +03:00
func_defined post_install || post_install( ) {
echo
}
2019-09-24 09:56:39 +03:00
func_defined clean || clean( ) {
2019-11-03 12:05:02 +03:00
rm -rf " $workdir " *.out
2019-05-28 01:02:29 +03:00
}
2019-09-24 09:56:39 +03:00
func_defined clean_dist || clean_dist( ) {
OLDIFS = $IFS
IFS = $'\n'
for f in $files ; do
IFS = $OLDIFS
read url filename hash <<< $( echo " $f " )
rm -f " $filename "
done
2019-05-28 01:02:29 +03:00
}
2019-09-24 09:56:39 +03:00
func_defined clean_all || clean_all( ) {
2019-11-03 12:05:02 +03:00
rm -rf " $workdir " *.out
2019-09-24 09:56:39 +03:00
OLDIFS = $IFS
IFS = $'\n'
for f in $files ; do
IFS = $OLDIFS
read url filename hash <<< $( echo " $f " )
rm -f " $filename "
done
2019-05-28 01:02:29 +03:00
}
2019-09-24 09:56:39 +03:00
addtodb( ) {
2021-02-01 21:36:18 +03:00
if [ ! -f " $packagesdb " ] ; then
echo " Note: $packagesdb does not exist. Creating. "
2021-04-03 06:53:41 +03:00
mkdir -p " ${ DESTDIR } /usr/Ports/ "
2021-02-01 21:36:18 +03:00
touch " $packagesdb "
2019-09-24 09:56:39 +03:00
fi
2021-02-01 21:36:18 +03:00
if ! grep -E " ^(auto|manual) $port $version " " $packagesdb " > /dev/null; then
2019-09-24 09:56:39 +03:00
echo " Adding $port $version to database of installed ports! "
if [ " ${ 1 :- } " = "--auto" ] ; then
2021-02-01 21:36:18 +03:00
echo " auto $port $version " >> " $packagesdb "
2019-09-24 09:56:39 +03:00
else
2021-02-01 21:36:18 +03:00
echo " manual $port $version " >> " $packagesdb "
2019-11-03 12:05:02 +03:00
if [ ! -z " ${ dependlist :- } " ] ; then
2021-02-01 21:36:18 +03:00
echo " dependency $port $dependlist " >> " $packagesdb "
2019-09-24 09:56:39 +03:00
fi
fi
else
>& 2 echo " Warning: $port $version already installed. Not adding to database of installed ports! "
fi
2019-05-28 01:02:29 +03:00
}
2019-09-24 09:56:39 +03:00
installdepends( ) {
for depend in $depends ; do
dependlist = " ${ dependlist :- } $depend "
done
for depend in $depends ; do
2021-02-01 21:36:18 +03:00
if ! grep " $depend " " $packagesdb " > /dev/null; then
2019-09-24 09:56:39 +03:00
( cd " ../ $depend " && ./package.sh --auto)
fi
done
2019-05-28 05:02:42 +03:00
}
2019-09-24 09:56:39 +03:00
uninstall( ) {
2021-02-01 21:36:18 +03:00
if grep " ^manual $port " " $packagesdb " > /dev/null; then
2019-09-24 09:56:39 +03:00
if [ -f plist ] ; then
for f in ` cat plist` ; do
case $f in
*/)
2021-04-03 06:53:41 +03:00
run rmdir " ${ DESTDIR } / $f " || true
2019-09-24 09:56:39 +03:00
; ;
*)
2021-04-03 06:53:41 +03:00
run rm -rf " ${ DESTDIR } / $f "
2019-09-24 09:56:39 +03:00
; ;
esac
done
# Without || true, mv will not be executed if you are uninstalling your only remaining port.
2021-02-01 21:36:18 +03:00
grep -v " ^manual $port " " $packagesdb " > packages.db.tmp || true
mv packages.db.tmp " $packagesdb "
2019-09-24 09:56:39 +03:00
else
>& 2 echo "Error: This port does not have a plist yet. Cannot uninstall."
fi
else
>& 2 echo " Error: $port is not installed. Cannot uninstall. "
fi
}
2021-01-21 11:31:31 +03:00
do_installdepends( ) {
echo " Installing dependencies of $port ! "
2019-09-24 09:56:39 +03:00
installdepends
2021-01-21 11:31:31 +03:00
}
do_fetch( ) {
2019-09-24 09:56:39 +03:00
echo " Fetching $port ! "
2019-05-28 01:02:29 +03:00
fetch
2019-09-24 09:56:39 +03:00
}
2020-02-04 03:07:27 +03:00
do_patch( ) {
echo " Patching $port ! "
patch_internal
}
2019-09-24 09:56:39 +03:00
do_configure( ) {
if [ " $useconfigure " = "true" ] ; then
echo " Configuring $port ! "
2020-04-15 15:54:23 +03:00
pre_configure
2019-09-24 09:56:39 +03:00
configure
2021-02-01 21:40:19 +03:00
post_configure
2019-09-24 09:56:39 +03:00
else
echo "This port does not use a configure script. Skipping configure step."
fi
}
do_build( ) {
echo " Building $port ! "
2019-05-28 01:02:29 +03:00
build
2019-09-24 09:56:39 +03:00
}
do_install( ) {
echo " Installing $port ! "
2019-05-28 01:02:29 +03:00
install
2020-03-25 17:54:30 +03:00
post_install
2019-09-24 09:56:39 +03:00
addtodb " ${ 1 :- } "
}
do_clean( ) {
echo " Cleaning workdir and .out files in $port ! "
clean
}
do_clean_dist( ) {
echo " Cleaning dist in $port ! "
clean_dist
}
do_clean_all( ) {
echo " Cleaning all in $port ! "
clean_all
}
do_uninstall( ) {
echo " Uninstalling $port ! "
uninstall
}
2021-04-23 14:53:53 +03:00
do_showproperty( ) {
if [ -z ${ !1+x } ] ; then
echo " Property ' $1 ' is not set. " >& 2
exit 1
fi
echo ${ !1 }
2021-04-21 23:53:02 +03:00
}
2019-09-24 09:56:39 +03:00
do_all( ) {
2021-01-21 11:31:31 +03:00
do_installdepends
2019-09-24 09:56:39 +03:00
do_fetch
2020-02-04 03:07:27 +03:00
do_patch
2019-09-24 09:56:39 +03:00
do_configure
do_build
do_install " ${ 1 :- } "
}
2019-05-28 01:02:29 +03:00
2021-04-11 02:01:20 +03:00
NO_GPG = false
parse_arguments( ) {
if [ -z " ${ 1 :- } " ] ; then
do_all
else
case " $1 " in
2021-04-23 14:53:53 +03:00
fetch| patch| configure| build| install| installdepends| clean| clean_dist| clean_all| uninstall| showproperty)
2021-04-25 01:27:58 +03:00
method = $1
shift
do_${ method } " $@ "
2021-04-11 02:01:20 +03:00
; ;
--auto)
do_all $1
; ;
--no-gpg-verification)
NO_GPG = true
shift
parse_arguments $@
; ;
*)
2021-04-23 14:53:53 +03:00
>& 2 echo " I don't understand $1 ! Supported arguments: fetch, patch, configure, build, install, installdepends, clean, clean_dist, clean_all, uninstall, showproperty. "
2021-04-11 02:01:20 +03:00
exit 1
; ;
esac
fi
}
parse_arguments $@