#!/bin/sh
# This is the post install script for PinePhone devices!

# Expand the filesystem
resize-fs

# Set chassis
hostnamectl set-chassis handset

# Add users
[[ $(pacman -Q plasma-mobile-settings 2>/dev/null) ]] && USER='kde' || USER='manjaro'
echo "$USER" > /tmp/user
echo "123456" > /tmp/password
echo "root" > /tmp/rootpassword
groupadd i2c
groupadd plugdev
if [ ! -f /usr/bin/pico-wizard ]; then
    [[ -f /usr/bin/fbcli ]] && _feedbackd=",feedbackd"
    useradd -m -g users -G wheel,sys,audio,input,video,storage,lp,network,users,power,rfkill,i2c,plugdev$_feedbackd -p $(openssl passwd -6 $(cat /tmp/password)) \
    -s /bin/bash $(cat /tmp/user) 1> /dev/null 2>&1
fi
awk -i inplace -F: "BEGIN {OFS=FS;} \$1 == \"root\" {\$2=\"$(openssl passwd -6 $(cat /tmp/rootpassword))\"} 1" /etc/shadow 1> /dev/null 2>&1
rm /tmp/user /tmp/password /tmp/rootpassword

# Update appstream DB
[[ -e /usr/bin/appstreamcli ]] && /usr/bin/appstreamcli refresh-cache --force

# change owners of the sound cards
chown :audio /proc/asound/cards

# fix i2c errors
chown :i2c /dev/i2c-*
chmod g+rw /dev/i2c-*

# Set locales
localectl set-locale "en_US.UTF-8"

# Disable this service, so it only gets run on first boot
systemctl disable pinephone-post-install.service

# Start phosh
[[ -e /usr/bin/phosh ]] && systemctl start phosh


# Start sddm
[[ -e /usr/bin/sddm ]] && systemctl enable --now sddm

# Pacman Init
pacman-key --init
pacman-key --populate archlinuxarm manjaro manjaro-arm

# Setup Waydroid
[[ -e /usr/bin/setup-waydroid ]] && setup-waydroid

# Disable the root login
passwd --lock root
