Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 53 additions & 20 deletions apt-bundle
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Verbose=
DryRun=

Comment on lines -26 to -28
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe these may have been initialized here, empty, as documentation / for developer awareness of their existence.

LMK if you want them restored.

run() {
if [ -n "$DryRun" ]; then
echo "$*"
Expand All @@ -52,7 +49,12 @@ sq() {
}

main() {
local opt file destdir filename dest tmpfile ppa
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variables within this declaration became unused after extraction.

Replaced with local varname= in their respective functions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: AFAIK sh doesn't have local variables, or even the keyword itself.

Most of the time sh is actually an aliased bash with a compatibility flag enabled and bash gives it the local keyword even though POSIX sh doesn't support it.

# Script is being sourced, skip CLI logic
if [ "${0##*/}" != apt-bundle ]; then
initialize
main_prerequisites;
return
Comment on lines +54 to +56
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user is sourcing apt-bundle then the temporary directory & files need to be present in order for any of the subsequent source / package statements to work.

Prerequisites are also obviously necessary.

Prerequisites leverage the same system for installing packages so they need the same temp directory & files.

fi

set -e
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exit on first error can be enabled from the shebang #!/bin/sh -e, is this not useful?


Expand All @@ -61,6 +63,19 @@ main() {
exec sudo "$0" "$@"
fi

main_options "$@";

initialize
main_prerequisites;
main_env;

cat -- "$@" > "$TmpDir/Debfile"
. "$TmpDir/Debfile"
Comment on lines +72 to +73
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user is sourcing apt-bundle then the calling script is the Debfile (sort of).
Meaning we can't copy & source the entire (list of) Debfiles into the temp dir.

We have to let the user invoke source/package and eventually a "flush" function (not yet defined).


apply_changes;
}

main_options() {
while getopts "nv" opt; do
case $opt in
n)
Expand All @@ -80,10 +95,15 @@ main() {
echo "usage: $0 [-n] [-v] <Debfile>..." >&2
exit 64
fi
}

type apt >/dev/null 2>&1 || die "apt is not installed"
main_env() {
export ARCH="$(dpkg --print-architecture)"
eval "$(sed 's/^/export /' /etc/os-release)"
}

initialize
main_prerequisites() {
type apt >/dev/null 2>&1 || die "apt is not installed"

for file in apt-transport-https software-properties-common curl gpg; do
is_installed "$file" && continue
Expand All @@ -96,22 +116,29 @@ main() {
xrun apt install -y < "$TmpDir/packages"
: > "$TmpDir/packages"
fi
}

export ARCH="$(dpkg --print-architecture)"
eval "$(sed 's/^/export /' /etc/os-release)"
apply_changes() {
apply_keyrings;
apply_sources;
apply_ppas;

cat -- "$@" > "$TmpDir/Debfile"
. "$TmpDir/Debfile"
run apt update

destdir=/usr/share/keyrings
apply_packages;
apply_debs;
}

apply_keyrings() {
local destdir=/usr/share/keyrings

find "$TmpDir/keyring_scripts" -type f | while read -r file; do
filename=$(basename "$file" .sh).gpg
dest=$destdir/$filename
tmpfile=$TmpDir/keyrings/$filename
local filename=$(basename "$file" .sh).gpg
local dest=$destdir/$filename
local tmpfile=$TmpDir/keyrings/$filename
(
set -e
dir=$TmpDir/tmp
local dir=$TmpDir/tmp
rm -rf "$dir"
mkdir "$dir"
cd "$dir"
Expand All @@ -124,35 +151,41 @@ main() {
cmp -s "$tmpfile" "$dest" 2>/dev/null ||
run install -o 0 -g 0 -m 644 "$tmpfile" "$dest"
done
}

destdir=/etc/apt/sources.list.d
apply_sources() {
local destdir=/etc/apt/sources.list.d

find "$TmpDir/sources" -type f -name '*.list' | while read -r file; do
cmp -s "$file" "$destdir/$(basename "$file")" 2>/dev/null ||
run install -o 0 -g 0 -m 644 "$file" "$destdir/"
done
}

apply_ppas() {
while read ppa; do
if [ -z "$(find /etc/apt/sources.list.d -name '*.list' -type f -print0 | xargs -r0 awk -v repo="http://ppa.launchpad.net/$ppa/ubuntu" '$1 == "deb" && $2 == repo')" ]; then
run add-apt-repository --no-update -y "ppa:$ppa"
fi
done < "$TmpDir/ppas"
}

run apt update

apply_packages() {
if [ -s "$TmpDir/packages" ]; then
xrun apt install -y < "$TmpDir/packages"
fi

if [ -s "$TmpDir/satisfy" ]; then
xrun apt satisfy -y < "$TmpDir/satisfy"
fi
}

destdir=$HOME/.cache/$(basename "$0")
apply_debs() {
local destdir=$HOME/.cache/$(basename "$0")
mkdir -p "$destdir"

while read url; do
file=$(basename "$url")
local file=$(basename "$url")
if [ -f "$destdir/$file" ]; then
curl -fsSLR -z "$destdir/$file" -o "$destdir/$file" "$url"
else
Expand Down