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
99 changes: 57 additions & 42 deletions contrib/ez-apt-install.sh
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
#!/bin/bash -e
# script adds the correct apt source and installs package
# Author: Liraz Siri <liraz@turnkeylinux.org>
# Original Author: Liraz Siri <liraz@turnkeylinux.org>
# Updated by: Jeremy Davis <jeremy@turnkeylinux.org>

fatal() { echo "FATAL: $*" >&2; exit 1; }
info() { echo "INFO: $*"; }
GRN='\033[1;32m'
RED='\033[1;31m'
NC='\033[0m'
fatal() { echo -e "${RED}FATAL:${NC} $*" >&2; exit 1; }
info() { echo -e "${GRN}INFO:${NC} $*"; }

[[ -z "$DEBUG" ]] || set -x


get_debian_dist() {
case "$1" in
6.*) echo squeeze ;;
7.*) echo wheezy ;;
8.*) echo jessie ;;
9.*) echo stretch ;;
10.*) echo buster ;;
11.*) echo bullseye ;;
12.*) echo bookworm ;;
*/*) echo "$1" | sed 's|/.*||' ;;
*/*) echo "${1//\/}";;
esac
}

[[ -f /etc/debian_version ]] || fatal "not a Debian derived system - no /etc/debian_version file"
deb_dist=$(get_debian_dist "$(cat /etc/debian_version)")

APT_URL=${APT_URL:="http://archive.turnkeylinux.org/debian"}

base_url="https://raw.githubusercontent.com/turnkeylinux/common/master"
base_path="overlays/bootstrap_apt"

KEY_FILE="usr/share/keyrings/tkl-${deb_dist}-main.gpg"
key_url="$base_url/$base_path/${KEY_FILE%gpg}asc"

APT_URL=${APT_URL:="http://archive.turnkeylinux.org/debian"}
APT_KEY_URL=${APT_KEY_URL:="$key_url"}
if [[ -f "/etc/debian_version" ]]; then
deb_dist=$(get_debian_dist "$(cat /etc/debian_version)")
elif [[ -f "/etc/issue" ]]; then
deb_dist=$(get_debian_dist \
"$(sed -En "/^Debian GNU\/Linux/ s|^[a-zA-Z /]* ([0-9]+) .*|\1.|p"\
/etc/issue)")
else
fatal "not a supported Debian based system - checked" \
" /etc/debian_version & /etc/issue"
fi

usage() {
cat<<EOF
Expand All @@ -47,8 +45,19 @@ EOF
exit 1
}

echo

[[ -n "$PACKAGE" ]] || PACKAGE="tklbam"

base_url="https://raw.githubusercontent.com/turnkeylinux/common/master"
base_path="overlays/bootstrap_apt"

KEY_FILE="usr/share/keyrings/tkl-${deb_dist}-main.gpg"
key_url="$base_url/$base_path/${KEY_FILE%gpg}asc"

APT_URL=${APT_URL:="http://archive.turnkeylinux.org/debian"}
APT_KEY_URL=${APT_KEY_URL:="$key_url"}

if [[ "$APT_KEY_URL" == *.asc ]]; then
if ! which gpg >/dev/null 2>&1; then
info "Installing gpg to process apt sigining key."
Expand All @@ -57,35 +66,41 @@ if [[ "$APT_KEY_URL" == *.asc ]]; then
fi
tmp_file=/tmp/$(basename "$APT_KEY_URL")
elif [[ "$APT_KEY_URL" == *.gpg ]]; then
tmp_file=''
tmp_file=""
else
error "APT_KEY_URL does not appear to be a GPG file (should end with .gpg or .asc)"
fatal "APT_KEY_URL does not appear to be a GPG file (should end with .gpg or .asc)"
fi

if ! rgrep . /etc/apt/sources.list* | sed 's/#.*//' | grep -q "$APT_URL"; then

apt_name=$(echo "$APT_URL" | sed 's|.*//||; s|/.*||')
apt_file="/etc/apt/sources.list.d/${apt_name}.list"
# just in case there are already tkl repos enabled - disable them...
# (a bit dirty because it will recomment existing commented lines, but does no harm)
readarray -d '' apt_files < <(find /etc/apt -type f -name "*.list" -print0)
for file in "${apt_files[@]}"; do
if grep -q "tkl-$deb_dist-main" "$file"; then
info "backing up $file"
sed -i.bak "/archive.turnkeylinux.org/ s|^|#|g" "$file"
fi
done

echo "deb [signed-by=/$KEY_FILE] $APT_URL $deb_dist main" > "$apt_file"
apt_name=$(sed -En "s|^http.*/([a-z\.]*)/.*|\1|p" <<<"$APT_URL")
apt_file="/etc/apt/sources.list.d/${apt_name}.list"
echo "deb [signed-by=/$KEY_FILE] $APT_URL $deb_dist main" > "$apt_file"
info "downloading $APT_KEY_URL"

info "downloading $APT_KEY_URL"
local_file=/$KEY_FILE
if [[ -n "$tmp_file" ]]; then
local_file=$tmp_file
fi
wget -O "$local_file" "$APT_KEY_URL"
if [[ -n "$tmp_file" ]]; then
gpg -o "/$KEY_FILE" --dearmor "$tmp_file"
rm -f "$tmp_file"
fi
info "Added $APT_URL package source to $apt_file"
local_file=/$KEY_FILE
if [[ -n "$tmp_file" ]]; then
local_file=$tmp_file
fi
wget -O "$local_file" "$APT_KEY_URL"
if [[ -n "$tmp_file" ]]; then
gpg -o "/$KEY_FILE" --dearmor "$tmp_file"
rm -f "$tmp_file"
fi
info "Added $APT_URL package source to $apt_file"

info "Running 'apt-get update'"
apt-get update \
|| fatal "Command fialed. Please report to TurnKey Linux."
|| fatal "Command failed. Please report to TurnKey Linux."

info "Installing $PACKAGE"
apt-get install "$PACKAGE" \
|| fatal "Package install failed, please report to TurnKe Linux."
apt-get install --yes "$PACKAGE" \
|| fatal "Package install failed, please report to TurnKey Linux."