Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save colemancda/fa6d2b60e14602fd88a230888bb699cf to your computer and use it in GitHub Desktop.
Save colemancda/fa6d2b60e14602fd88a230888bb699cf to your computer and use it in GitHub Desktop.
Install script for the Swift Community Apt Repository - https://archive.swiftlang.xyz
#! /bin/bash
#
# Install script for the Swift Community Apt Repository - swiftlang.xyz
#
# check distribution version and infer distro with switch/case
## as all distribution versions have different names we can use this information to infer the distribution name.
check_ver () {
for var in "${SUPPORTED_VER[@]}"
do
if grep -q $var tmp_sources.txt;
then
vers=$var
case $vers in
bionic|focal|hirsute|impish) dist="ubuntu";;
buster|bullseye) dist="debian";;
esac
break
fi
done
}
## raspbian is an edge case
## check for raspbian
check_raspbian () {
if grep -q "raspbian" tmp_sources.txt;
then
dist="raspbian"
fi
}
## check if dist/version is supported
check_supported () {
if [ $dist == "unsupported" ] || [ $vers == "unsupported" ]
then
echo sorry your system is unsupported.
echo
exit 0
fi
}
## user_input allows the selection of the available repository sections
## this will vary depending on which Ubuntu/Debian distribution is installed
user_input_all () {
# User Input
# # For distributions with release versions from v5.4.* and developer snapshots available.
echo
echo "--------------- Choose Swift Release Version ---------------"
echo
echo " If you always want to have the latest release/stable version of Swift available choose [1], if not, choose the version most suitable for your project. "
echo " The latest release/stable version of Swift is currently 5.5 "
echo
echo "---------------------------------------------------------"
echo
echo " 1) latest/main - This will update to the latest release/stable version of Swift available"
echo " 2) developer - Swift developer builds - this will update to the latest developer build available"
echo " 3) Swift version 5.3.* - this will update to the latest point/patch release of Swift 5.3"
echo " 4) Swift version 5.4.* - this will update to the latest point/patch release of Swift 5.4"
echo " 5) Swift version 5.5.* - this will update to the latest point/patch release of Swift 5.5"
echo
echo "---------------------------------------------------------"
echo
read -r -p "Enter number [1/2/3/...] : " n < /dev/tty
echo
case $n in
1) main=" main"; echo "Installing the Swift main/latest repository"; echo;;
2) dev=" dev"; main=""; echo "Installing the Swift developer repository"; echo;;
3) v5_3=" v5_3"; main=""; echo "Installing the Swift version 5.3.* repository"; echo;;
4) v5_4=" v5_4"; main=""; echo "Installing the Swift version 5.4.* repository"; echo;;
5) v5_5=" v5_5"; main=""; echo "Installing the Swift version 5.5.* repository"; echo;;
*) echo "Invalid option"; echo; exit 1;;
esac
}
user_input_from_5_4 () {
# User Input
# # For distributions with release versions from v5.4.* and developer snapshots available.
echo
echo "--------------- Choose Swift Release Version ---------------"
echo
echo " If you always want to have the latest release/stable version of Swift available choose [1], if not, choose the version most suitable for your project. "
echo " The latest release/stable version of Swift is currently 5.5 "
echo
echo "---------------------------------------------------------"
echo
echo " 1) latest/main - This will update to the latest/stable release of Swift available"
echo " 2) developer - Swift developer builds - this will update to the latest developer build available"
echo " 3) Swift version 5.4.* - this will update to the latest point/patch release of Swift 5.4"
echo " 4) Swift version 5.5.* - this will update to the latest point/patch release of Swift 5.5"
echo
echo "---------------------------------------------------------"
echo
read -r -p "Enter number [1/2/3/...] : " n < /dev/tty
echo
case $n in
1) main=" main"; echo "Installing the Swift main/latest repository"; echo;;
2) dev=" dev"; main=""; echo "Installing the Swift developer repository"; echo;;
3) v5_4=" v5_4"; main=""; echo "Installing the Swift version 5.4.* repository"; echo;;
4) v5_5=" v5_5"; main=""; echo "Installing the Swift version 5.5.* repository"; echo;;
*) echo "Invalid option"; echo; exit 1;;
esac
}
## check curl is installed
curl_check () {
echo "Checking for curl..."
if command -v curl > /dev/null; then
echo "Detected curl..."
else
echo -n "Installing curl... "
apt-get install -q -y curl &> /dev/null
echo "done."
if [ "$?" -ne "0" ]; then
echo "Unable to install curl! Your base system has a problem; please check your default OS's package repositories because curl should work."
echo "Repository installation aborted."
exit 1
fi
fi
}
## check gpg is installed
gpg_check () {
echo "Checking for gpg..."
if command -v gpg > /dev/null; then
echo "Detected gpg..."
else
echo -n "Installing gnupg for GPG verification... "
apt-get install -y gnupg &> /dev/null
echo "done."
if [ "$?" -ne "0" ]; then
echo "Unable to install GPG! Your base system has a problem; please check your default OS's package repositories because GPG should work."
echo "Repository installation aborted."
exit 1
fi
fi
}
install_debian_keyring () {
if [ "${dist}" = "debian" ]; then
echo "Installing debian-archive-keyring which is needed for installing "
echo "apt-transport-https on many Debian systems."
apt-get install -y debian-archive-keyring &> /dev/null
fi
}
file_check () {
if [ -f "$apt_source_path" ]
then
rm $apt_source_path
fi
}
main() {
# default repo values
v5_3=""
v5_4=""
v5_5=""
main=" main"
dev=""
# default values
gpg_key_url="https://archive.swiftlang.xyz/swiftlang_repo.gpg.key"
apt_source_path="/etc/apt/sources.list.d/swiftlang-release.list"
# SUPPORTED_DIST=('ubuntu' 'debian' 'raspbian')
SUPPORTED_VER=('bionic' 'focal' 'hirsute' 'impish' 'buster' 'bullseye')
# default to unsupported
dist="unsupported"
vers="unsupported"
# ----
# run apt update
echo -n "running update... "
apt-get update > tmp_sources.txt
echo "done."
# scan tmp_sources.txt
echo -n "scanning sources... "
#check_dist
check_ver
check_raspbian
echo "done."
# clean tmp file
rm tmp_sources.txt
# check if system is supported - if not - exit
check_supported
# if ok continue with install
# ----
echo system detected is compatible with $dist/$vers
echo
# check prerequisists
curl_check
gpg_check
install_debian_keyring
echo -n "Installing apt-transport-https... "
apt-get install -y apt-transport-https &> /dev/null
echo "done."
# import the gpg key
echo -n "Importing swiftlang gpg key... "
curl -L "${gpg_key_url}" 2> /dev/null | apt-key add - &>/dev/null
echo "done."
# user input
if [ "${dist}" != "raspbian" ];
then
case $vers in
bionic|focal|buster) user_input_all;;
*) user_input_from_5_4;;
esac
fi
# add source list file
echo -n "Installing $apt_source_path... "
file_check
echo "deb https://archive.swiftlang.xyz/$dist/ $vers$v5_3$v5_4$v5_5$main$dev" > $apt_source_path
# echo "deb https://swiftlang.xyz/$dist/ $vers$v5_3$v5_4$v5_5$main$dev" >> $apt_source_path
echo "done."
#
echo -n "Running apt-get update... "
# update apt on this system
apt-get update &> /dev/null
echo "done."
echo
echo "The repository is setup!"
echo "You can now install swift using 'sudo apt install swiftlang' "
echo
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment