init-debian/init.sh
2024-11-04 05:08:21 +08:00

197 lines
6.9 KiB
Bash
Executable File

#!/usr/bin/env bash
set -e
OS_ARCH=$(dpkg --print-architecture) || (echo "Not Debian Based Distro" && exit 1)
OS_CODE=$(source /etc/os-release && echo "$VERSION_CODENAME")
OS_VERSION=""
case $OS_CODE in
bullseye) OS_VERSION="11" ;;
bookworm) OS_VERSION="12" ;;
esac
SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
choose_sites(){
case $MIRROR_SOURCE in
1)
MIRROR_URL=https://mirrors.nju.edu.cn/debian/
SEC_URL=https://mirrors.nju.edu.cn/debian-security
DOCKER_URL=https://mirrors.nju.edu.cn/docker-ce/linux/debian
PVE_URL=https://mirrors.nju.edu.cn/proxmox/debian/pve
;;
2)
MIRROR_URL=https://mirrors.xjtu.edu.cn/debian/
SEC_URL=https://mirrors.xjtu.edu.cn/debian-security
DOCKER_URL=https://mirrors.xjtu.edu.cn/docker-ce/linux/debian
PVE_URL=https://mirrors.ustc.edu.cn/proxmox/debian/pve
;;
3)
MIRROR_URL=https://mirrors.ustc.edu.cn/debian/
SEC_URL=https://mirrors.ustc.edu.cn/debian-security
DOCKER_URL=https://mirrors.ustc.edu.cn/docker-ce/linux/debian
PVE_URL=https://mirrors.ustc.edu.cn/proxmox/debian/pve
;;
4)
MIRROR_URL=https://mirrors.tuna.tsinghua.edu.cn/debian/
SEC_URL=https://mirrors.tuna.tsinghua.edu.cn/debian-security
DOCKER_URL=https://mirrors.tuna.tsinghua.edu.cn/docker-ce/linux/debian
PVE_URL=https://mirrors.tuna.tsinghua.edu.cn/proxmox/debian/pve
;;
*)
MIRROR_URL=https://deb.debian.org/debian/
SEC_URL=https://security.debian.org/debian-security
DOCKER_URL=https://download.docker.com/linux/debian
PVE_URL=http://download.proxmox.com/debian/pve
;;
esac
}
configure(){
choose_sites
sed -i -E "s/.*set tabsize.*/set tabsize 4/g" /etc/nanorc
sed -i -E "s/.*set tabstospaces.*/set tabstospaces/g" /etc/nanorc
sed -i -E "s/.*set softwrap.*/set softwrap/g" /etc/nanorc
sed -i -E "/.*HashKnownHosts.*/d" /etc/ssh/ssh_config
sed -i -E "/.*HostKeyAlgorithms.*/d" /etc/ssh/ssh_config
sed -i -E "/.*PubkeyAcceptedKeyTypes.*/d" /etc/ssh/ssh_config
echo " HashKnownHosts no" >> /etc/ssh/ssh_config
echo " HostKeyAlgorithms ssh-ed25519,ssh-rsa,rsa-sha2-256" >> /etc/ssh/ssh_config
echo " PubkeyAcceptedKeyTypes ssh-ed25519,ssh-rsa,rsa-sha2-256" >> /etc/ssh/ssh_config
rm /etc/apt/sources.list || true
rm /etc/apt/sources.list.d/* || true
echo -e "Enabled: yes
Types: deb
URIs: $MIRROR_URL
Suites: $OS_CODE $OS_CODE-updates $OS_CODE-backports
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
Architectures: $OS_ARCH
Enabled: yes
Types: deb-src
URIs: $SEC_URL
Suites: $OS_CODE-security
Components: main contrib non-free non-free-firmware
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
" | tee /etc/apt/sources.list.d/debian.sources
# echo -e "deb $MIRROR_URL $OS_CODE main contrib non-free non-free-firmware
#deb $MIRROR_URL $OS_CODE-updates main contrib non-free non-free-firmware
#deb $MIRROR_URL $OS_CODE-backports main contrib non-free non-free-firmware
#deb $SEC_URL $OS_CODE-security main contrib non-free non-free-firmware
#" | tee /etc/apt/sources.list
if [ $FOR_PVE -eq 1 ]; then
echo "deb $PVE_URL bookworm pve-no-subscription" | \
tee /etc/apt/sources.list.d/pve-no-subscription.list
fi
command="nala"
apt update
apt install -y $command || command="apt"
$command install -y ca-certificates curl gnupg lsb-release zsh
if [[ $OS_VERSION=="12" ]]; then
OS_VERSION="Testing"
fi
echo "deb http://download.opensuse.org/repositories/shells:/zsh-users:/zsh-completions/Debian_$OS_VERSION/ /" | \
tee /etc/apt/sources.list.d/shells:zsh-users:zsh-completions.list
curl -fsSL "https://download.opensuse.org/repositories/shells:zsh-users:zsh-completions/Debian_$OS_VERSION/Release.key" | \
gpg --dearmor | tee /etc/apt/trusted.gpg.d/shells_zsh-users_zsh-completions.gpg > /dev/null
if [ $FOR_PVE -eq 1 ]; then
rm /etc/apt/sources.list.d/docker.list || true
rm /etc/apt/sources.list.d/azlux.list || true
else
echo "deb [arch=$OS_ARCH] $DOCKER_URL $OS_CODE stable" | \
tee /etc/apt/sources.list.d/docker.list
curl -fsSL "https://download.docker.com/linux/debian/gpg" | \
gpg --dearmor | tee /etc/apt/trusted.gpg.d/docker.gpg > /dev/null
echo "deb [arch=$OS_ARCH] http://packages.azlux.fr/debian $OS_CODE main" | \
tee /etc/apt/sources.list.d/azlux.list
curl -fsSL "https://azlux.fr/repo.gpg.key" | \
gpg --dearmor | tee /etc/apt/trusted.gpg.d/azlux-archive-keyring.gpg > /dev/null
fi
usermod -s /usr/bin/zsh $(whoami)
cp "$SCRIPTPATH/.zshrc" $HOME
cp "$SCRIPTPATH/.screenrc" $HOME
if [ $FOR_PVE -eq 1 ]; then
sed -i -E "s/.*prompt .*/prompt bart/g" $HOME/.zshrc
fi
cp "$SCRIPTPATH/TINK-CA.crt" /usr/local/share/ca-certificates
update-ca-certificates
if [ $FOR_PVE -eq 1 ]; then
sed -i -E "s/.*zsh-autocomplete.*//g" $HOME/.zshrc
sed -i -E "s/.*menuselect.*//g" $HOME/.zshrc
sed -i -E "s/#autoload -Uz compinit/autoload -Uz compinit/g" $HOME/.zshrc
sed -i -E "s/#compinit/compinit/g" $HOME/.zshrc
sed -i -E "s/.*zoxide.*//g" $HOME/.zshrc
else
git -C /usr/share clone --depth 1 https://git.tink.ltd:8443/tink/zsh-autocomplete.git 2>/dev/null || \
git -C /usr/share/zsh-autocomplete pull
$SCRIPTPATH/install-github-release.sh -r ajeetdsouza/zoxide -m x86_64-unknown -p /usr/local/bin -n zoxide -f -v -V
fi
}
install_pkg(){
command="nala"
command -v $command &>/dev/null || command="apt"
$command update
test -r "$PKG_BASE"
xargs -a <(awk '! /^ *(#|$)/' "$PKG_BASE") -r $command install -y
}
usage(){
echo "Usage: $0 [OPTIONS]";
echo "OPTIONS:";
echo " -f <PACKAGES> package list file (default: $PKG_BASE)";
echo " -i install only";
echo " -p pve";
echo " -m <CHOICE> apt mirror source";
echo " * official"
echo " 1 nju (default)"
echo " 2 xjtu"
echo " 3 ustc"
echo " 4 tsinghua"
}
PKG_BASE="$SCRIPTPATH/packages-base"
INSTALL_ONLY=0;
FOR_PVE=0;
MIRROR_SOURCE=1;
while getopts ":f:ipm:" OPT; do
case $OPT in
f)
PKG_BASE=$OPTARG;
;;
i)
INSTALL_ONLY=1;
;;
p)
FOR_PVE=1;
;;
m)
MIRROR_SOURCE=$OPTARG;
;;
?)
echo "Invalid Option: -$OPTARG";
usage;
exit 1;;
esac;
done;
if [ $INSTALL_ONLY -eq 0 ]; then
configure;
fi;
install_pkg;