#!/bin/bash
#
# Copyright (C) 2018 Purism SPC
#
# SPDX-License-Identifier: GPL-3.0+
#

if [ $# -ne 1 ]; then
    echo "usage: $0 <rootfs dir>"
    exit
fi

cwd=`pwd`

DIR=$1

echo "Customizing rootfs"
set -x

cat << EOF > "${DIR}/etc/rc.local"
#!/bin/bash
resize2fs /dev/ram0
exit 0
EOF
chmod +x ${DIR}/etc/rc.local

cat << EOF > "${DIR}/etc/systemd/system/rc-local.service"
[Unit]
Description=/etc/rc.local
ConditionPathExists=/etc/rc.local

[Service]
Type=oneshot
ExecStart=/etc/rc.local start
TimeoutSec=0
StandardOutput=tty
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF
mkdir -p ${DIR}/etc/systemd/system/multi-user.target.wants
ln -s /etc/systemd/system/rc-local.service  ${DIR}/etc/systemd/system/multi-user.target.wants/rc-local.service

mkdir -p ${DIR}/usr/src
cp files/rsi*tar.gz ${DIR}/usr/src

cat << EOF > "${DIR}/etc/modprobe.d/rsi_91x.conf"
# This is for wlan sta + BT
options rsi_91x dev_oper_mode=13 rsi_zone_enabled=1
blacklist rsi_sdio
EOF

mkdir -p ${DIR}/usr/local/bin
cp test_scripts/* ${DIR}/usr/local/bin

#mkdir -p ${DIR}/lib/firmware
#tar -xf files/rsi_firmware.tar.gz -C ${DIR}/lib/firmware
#cd ${DIR}/lib/modules/4.18.*/kernel/drivers/net/wireless/rsi &&
#    tar -xf ${cwd}/files/rsi_upgrade.tar.gz 

set +x
