#!/bin/sh
# This is the post install script for PineTab 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  
if [ ! -f /usr/bin/pico-wizard-session ]; then
    useradd -m -g users -G wheel,sys,audio,input,video,storage,lp,network,users,power,rfkill -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
groupadd i2c
chown :i2c /dev/i2c-*
chmod g+rw /dev/i2c-*
usermod -aG i2c $USER

# add user to plugdev group
groupadd plugdev
usermod -aG plugdev $USER

# add user to autologin group
groupadd autologin
usermod -aG autologin $USER

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

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

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

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

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