portzap/bin/portzap

133 lines
2.3 KiB
Bash
Executable file

#!/bin/sh
# A shell script that takes care of keeping up to date with
# the HardenedBSD ports collection.
##
# Configuration
source="https://git.hardenedbsd.org/hardenedbsd/ports.git"
transient_dir="/home/_portzap/ports"
final_dir="/usr/ports/"
##
# Default modes
init_mode=707
clone_mode=007
pull_mode=007
unpack_mode=022
##
# Utils
exit_on_missing_deps() {
deps=$1
for dep in $deps; do
which -s $dep
if [ $? -ne 0 ]; then
echo $dep is missing
exit 1
fi
done
}
has_portzap_access() {
groups=$(id -Gn)
in_group=1
for g in $groups; do
if [ $g="_portzap" ];
then
in_group=0
fi
done
return $in_group
}
user_is_not_root() {
user_id=$(id -u $(whoami))
result=$(test $user_id -ne "0")
return $result
}
##
# Commands
help() {
echo "Usage: portzap init|clone|pull|unpack"
}
init() {
if user_is_not_root;
then
echo "The init command should be run as root."
exit 1
fi;
umask $init_mode
pw userdel _portzap -r
pw useradd _portzap -m -s /sbin/nologin
}
clone() {
if has_portzap_access;
then
if [ -e "$transient_dir/.git" ];
then
echo "$transient_dir has already been cloned."
echo "Run 'portzap pull' instead."
exit 1
fi
umask $clone_mode
git clone --depth 1 $source $transient_dir
else
echo "Permission denied."
fi
}
pull() {
if has_portzap_access;
then
if [ -e "$transient_dir/.git" ];
then
umask $pull_mode
cd $transient_dir
git pull --rebase origin hardenedbsd/main
else
echo "Run 'portzap clone' first."
exit 1
fi
else
echo "Permission denied."
exit 1
fi
}
unpack() {
if user_is_not_root;
then
echo "The unpack command should be run as root."
exit 1
fi
umask $unpack_mode
cp -Rfv "$transient_dir/." $final_dir
}
case $1 in
"init")
init
break
;;
"clone")
exit_on_missing_deps "git"
clone
break
;;
"pull")
exit_on_missing_deps "git"
pull
break
;;
"unpack")
unpack
break
;;
*)
help
break
;;
esac