From 535c05e713162c8d5b7efaf9e4901484ef9029e6 Mon Sep 17 00:00:00 2001
From: 0x1eef <0x1eef@protonmail.com>
Date: Sat, 14 Jan 2023 14:30:29 -0300
Subject: [PATCH] First commit
---
.projectile | 0
README.md | 25 ++++++++++++++
bin/portzap | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 122 insertions(+)
create mode 100644 .projectile
create mode 100644 README.md
create mode 100755 bin/portzap
diff --git a/.projectile b/.projectile
new file mode 100644
index 0000000..e69de29
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..7ac507d
--- /dev/null
+++ b/README.md
@@ -0,0 +1,25 @@
+## About
+
+portzap is a simple bourne shell script that takes care of keeping up to
+date with the [HardenedBSD](https://hardenedbsd.org) ports collection.
+
+The
+[HardenedBSD ports collection](https://git.hardenedbsd.org/hardenedbsd/ports.git)
+is maintained as a git repository, and portzap allows the repository to be cloned
+(and updated) using a regular user account. The unpacking of the repository to
+`/usr/ports` is also supported but must be performed by root.
+
+## Usage
+
+* **portzap clone**
+ This command should be run with a regular user account.
+ The command clones HardenedBSD's ports collection to `/tmp/ports`.
+
+
+* **portzap pull**
+ This command should be run with a regular user account.
+ The command updates an existing repository previously cloned with `portzap clone`.
+
+* **portzap unpack**
+ This command should be run as root. It copies `/tmp/ports` to `/usr/ports`.
+
diff --git a/bin/portzap b/bin/portzap
new file mode 100755
index 0000000..673323d
--- /dev/null
+++ b/bin/portzap
@@ -0,0 +1,97 @@
+#!/bin/sh
+
+##
+# A shell script that takes care of keeping up to date with
+# the HardenedBSD ports collection.
+
+source="https://git.hardenedbsd.org/hardenedbsd/ports.git"
+stage_dir="/tmp/ports/"
+
+
+##
+# Utils
+
+exit_on_missing_deps() {
+ deps="git"
+ for dep in $deps; do
+ which -s $dep
+ if [ $? -ne 0 ]; then
+ echo $dep is missing
+ exit 1
+ fi
+ done
+}
+
+user_is_root() {
+ user_id=$(id -u $(whoami))
+ return $user_id = "0"
+}
+
+user_is_not_root() {
+ user_id=$(id -u $(whoami))
+ result=$(test $user_id -ne "0")
+ return $result
+}
+
+##
+# Commands
+
+help() {
+ echo portzap "[clone|pull|unpack]"
+}
+
+clone() {
+ if user_is_root
+ then
+ echo "The clone command should not be run as root."
+ exit 1
+ fi
+ rm -rf $stage_dir
+ git clone --depth 1 $source $stage_dir
+}
+
+pull() {
+ if user_is_root
+ then
+ echo "The pull command should not be run as root."
+ exit 1
+ fi
+ if [ -e "$stage_dir/.git" ];
+ then
+ cd $stage_dir
+ git pull --rebase origin hardenedbsd/main
+ else
+ echo "Run 'portzap clone' first"
+ exit 1
+ fi
+}
+
+unpack() {
+ if user_is_not_root
+ then
+ echo "The unpack command should be run as root."
+ exit 1
+ fi
+ cp -Rfv /tmp/ports /usr/
+}
+
+case $1 in
+ "clone")
+ exit_on_missing_deps
+ clone
+ break
+ ;;
+ "pull")
+ exit_on_missing_deps
+ pull
+ break
+ ;;
+ "unpack")
+ unpack
+ break
+ ;;
+ *)
+ help
+ break
+ ;;
+esac