#!/bin/sh
for f in etc/bash/local etc/profile.d/alias etc/profile.d/bash_colors etc/profile.d/init etc/profile.d/ps1 etc/profile.d/ps1_containers etc/profile.d/shell etc/shell_init ; do [ -f "/$f.apk-new" ] && mv "/$f.apk-new" "/$f"; done; true
[ -f /etc/profile.d/alias.local ] || touch /etc/profile.d/alias.local
[ -f /etc/profile.d/include ] || touch /etc/profile.d/include
chattr -i /etc/shell_history 2>/dev/null || true
{ if command -v zsh > /dev/null; then echo zsh; else echo '. /etc/profile'; fi; } > /etc/shell_history 2>/dev/null || true
chattr +i /etc/shell_history 2>/dev/null || true
grep -qF '. /etc/profile.d/init' /etc/profile || echo '. /etc/profile.d/init' >> /etc/profile
for bashrc in /etc/bash.bashrc /etc/bash/bashrc; do [ -f "$bashrc" ] && { grep -qF '. /etc/bash/local' "$bashrc" || echo '. /etc/bash/local' >> "$bashrc"; } || true; done
command -v zsh > /dev/null && {
	mkdir -p /etc/zsh/zshrc.d /etc/zsh/zcompletion
	cat > /etc/zsh/zshrc.d/pre << 'ZSHEOF'
fpath=(/etc/zsh/zcompletion $fpath)
ZSHEOF
	cat > /etc/zsh/zshrc.d/end << 'ZSHEOF'
prompt off
. /etc/profile.d/init
PS1=$PS1_ZSH
for file in $HOME $HOME/.config/zsh; do file="$file/.zsh_history"; rm -f "$file"; done
ZSHEOF
	[ -f /etc/zsh/zshrc.local ] && { grep -qF '. /etc/zsh/zshrc.d/end' /etc/zsh/zshrc.local || echo '. /etc/zsh/zshrc.d/end' >> /etc/zsh/zshrc.local; } || true
	[ -f /etc/zsh/zshenv ] && { grep -qF '. /etc/zsh/zshrc.d/pre' /etc/zsh/zshenv || echo '. /etc/zsh/zshrc.d/pre' >> /etc/zsh/zshenv; } || echo '. /etc/zsh/zshrc.d/pre' > /etc/zsh/zshenv
} || true
command -v fish > /dev/null && {
	mkdir -p /etc/fish/conf.d
	ln -sf /etc/profile.d/alias /etc/fish/conf.d/alias.fish
	cat > /etc/fish/conf.d/config.fish << 'FISHEOF'
set -g fish_history ""
rm -f ~/.local/share/fish/fish_history

if not set -q _SHELL_SOURCED
    set -gx _SHELL_SOURCED 1
    status is-interactive && command -q fastfetch && fastfetch
end
FISHEOF
} || true
