## 文件来自 jixun.uk
## 这是 Jixun 自用的 Arch 安装过程记录脚本

loadkeys uk

mkfs.ext4 /dev/sda4
mkswap /dev/sda2

mount /dev/sda4 /mnt
mount --mkdir /dev/sda1 /mnt/boot
mount --mkdir /dev/sda3 /mnt/edata
swapon /dev/sda2

pacstrap -K /mnt base linux-lts linux-firmware
genfstab -U /mnt >>/mnt/etc/fstab
arch-chroot /mnt

pkgs=()

pkgs+=(exfatprogs e2fsprogs xfsprogs)                   # 文件系统
pkgs+=(mesa vulkan-radeon libva-mesa-driver mesa-vdpau) # 显卡驱动

# basic dev related things
pkgs+=(base-devel binutils cmake ninja git pacman-contrib)
pkgs+=(boost) # cpp libs
pkgs+=(python-lsp-server bash-language-server)
pkgs+=({yaml,typescript,yaml,eslint}-language-server)
pkgs+=(nodejs npm pnpm yarn)
pkgs+=(go hugo)
pkgs+=(docker docker-compose)
pkgs+=(python python-pip)
pkgs+=(jdk17-openjdk) # JDK + 运行时
pkgs+=(openssh)       # ssh

# Network
pkgs+=(networkmanager rp-pppoe modemmanager iwd)
pkgs+=(dnsutils inetutils iproute2)

# utilities
pkgs+=(bash bash-completion)
pkgs+=(vi vim)
pkgs+=(nano nano-syntax-highlighting)
pkgs+=(man-db man-pages texinfo) # docs
pkgs+=(bc gawk pv rsync jq)      # shell scripting
pkgs+=(openssl curl wget tree htop tree tmux sudo)
pkgs+=(neofetch)
pkgs+=(reflector cronie)
pkgs+=(pyside2)                                      # qt
pkgs+=(tar zip unzip gzip xz bzip2 unrar p7zip zstd) # archive

# CPU patches / Boot Loader
pkgs+=(amd-ucode) # amd-ucode _OR_ intel-ucode
pkgs+=(efibootmgr efivar)
# systemd-boot, no extra packages required
# https://wiki.archlinux.org/title/Systemd-boot

# gui stuff
pkgs+=(xorg-xwayland sddm weston)               # wayland and whatnot
pkgs+=(kwin plasma-meta plasma-wayland-session) # kde 桌面环境

# kde 全家桶
# pkgs+=(kde-applications-meta)

# 或者只安装自己需要的组件
pkgs+=(kde-accessibility-meta)
pkgs+=(kde-education-meta)
pkgs+=(kde-games-meta)
pkgs+=(audiocd-kio ffmpegthumbs kmix) # kde-multimedia-meta
pkgs+=(gwenview qt{5,6}-imageformats svgpart)
pkgs+=(kamera kcolorchooser kolourpaint okular spectacle)
# pkgs+=(kde-pim-meta) # 个人信息管理软件
pkgs+=(kde-system-meta kde-utilities-meta) # 系统工具

# 媒体播放支持
#pkgs+=(gstreamer gst-libav gst-plugins-{base,good,ugly})
pkgs+=(vlc mpv phonon-qt5-vlc ffmpeg)

pkgs+=(dolphin-plugins kdegraphics-thumbnailers kdenetwork-filesharing)  # dolphi extras
pkgs+=(pipewire wireplumber pipewire-{audio,alsa,pulse,jack})            # audio
pkgs+=(firefox)                                                          # 浏览器
pkgs+=(print-manager cups system-config-printer)                         # 打印支持
pkgs+=(hplip)                                                            # HP 打印机支持
pkgs+=(tesseract-data-{chi_sim{,_vert},chi_tra{,_vert},eng})             # OCR
pkgs+=(noto-fonts-cjk ttf-sarasa-gothic ttf-cascadia-code-nerd)          # 字体
pkgs+=(fcitx5-{im,rime,chinese-addons,table-extra,breeze,pinyin-zhwiki}) # 输入法

pacman -Sy # 刷新缓存
pacman -S --needed "${pkgs[@]}"

# 安装 yay
(
    pacman -S --needed git base-devel
    git clone https://aur.archlinux.org/yay-bin.git /tmp/yay-bin
    cd /tmp/yay-bin
    makepkg -si
    rm -rf /tmp/yay-bin
)

aur=()
aur+=(visual-studio-code-bin)
aur+=(ttf-roboto ttf-paratype ttf-cascadia-{code,mono}-nerd)

# 初始化默认配置文件
install -d -m0700 /etc/skel/.config
# VSCode: 原生 Wayland 支持
install -m0755 <(
    cat <<EOF
--ozone-platform-hint=auto
--enable-wayland-ime
EOF
) /etc/skel/.config/code-flags.conf

## Configure user
useradd -m -G adm,wheel -s /bin/bash jixun
passwd jixun

install -m0440 <(
    cat <<EOF
# 允许 wheel 组的用户执行 sudo
%wheel ALL=(ALL:ALL) ALL
EOF
) /etc/sudoers.d/jixun_admin

## network related
systemctl enable systemd-resolved
install -d -m0755 /etc/systemd/resolved.conf.d/
install -m0644 <(
    cat <<EOF
[Resolve]
DNS=1.0.0.1#cloudflare-dns.com 8.8.4.4#dns.google
FallbackDNS=1.1.1.1#cloudflare-dns.com 9.9.9.9#dns.quad9.net

# 使用加密 DNS
DNSSEC=yes
DNSOverTLS=yes

# mDNS 内网发现
MulticastDNS=yes
EOF
) /etc/systemd/resolved.conf.d/60-jixun.conf

command -v nmcli && systemctl enable NetworkManager
if pacman -Qs iwd >/dev/null; then
    install -d -m 755 /etc/NetworkManager/conf.d/
    install -m0644 <(
        cat <<EOF
[device]
# 使用 iwd 来提供无线网络加密后端
wifi.backend=iwd
EOF
    ) /etc/NetworkManager/conf.d/wifi_backend.conf
fi
echo 'annie' >/etc/hostname

# cups 打印支持
if pacman -Qs cups >/dev/null; then
    systemctl enable cups
fi

## time config
ln -sf /usr/share/zoneinfo/Europe/London /etc/localtime
systemctl enable systemd-timesyncd
hwclock --systohc

## locale
nano /etc/locale.gen
locale-gen
localectl set-x11-keymap gb

# keymap
echo 'KEYMAP=uk' >/etc/vconsole.conf
echo 'LANG=en_US.UTF-8' >/etc/locale.conf

## Bootloader
bootctl install
echo "
# Jixun's changes
default @saved
reboot-for-bitlocker yes
" >>/boot/loader/loader.conf
cat >/boot/loader/entries/arch.conf <<EOF
title   Arch Linux
linux   /vmlinuz-linux-lts
initrd  /amd-ucode.img
initrd  /initramfs-linux-lts.img
options root=UUID=$(blkid -o value -s UUID /dev/sda4) rw
EOF
cat >/boot/loader/entries/arch-fallback.conf <<EOF
title   Arch Linux (fallback)
linux   /vmlinuz-linux-lts
initrd  /amd-ucode.img
initrd  /initramfs-linux-lts-fallback.img
options root=UUID=$(blkid -o value -s UUID /dev/sda4) rw quiet splash
EOF

# auto update bootloader
install -d -m755 /etc/pacman.d/hooks/
cat >/etc/pacman.d/hooks/95-systemd-boot.hook <<'EOF'
[Trigger]
Type = Package
Operation = Upgrade
Target = systemd

[Action]
Description = Gracefully upgrading systemd-boot...
When = PostTransaction
Exec = /usr/bin/systemctl restart systemd-boot-update.service
EOF

## SDDM Login Manager
install -d -m755 /etc/sddm.conf.d/
cat >/etc/sddm.conf.d/10-wayland.conf <<'EOF'
[General]
DisplayServer=wayland
[Theme]
Current=breeze
EOF

# Docker
if command -v docker >/dev/null; then
    usermod -aG docker jixun
    systemctl enable docker
fi

# VLC
if [[ -f /usr/lib/vlc/vlc-cache-gen ]]; then
    /usr/lib/vlc/vlc-cache-gen /usr/lib/vlc/plugins
fi
