#!/usr/bin/bash

# This file must contain all required operations to fix/update any issue
# of the data partition.

# This script is called by systemd as an oneshot service just after the
# filesystem be mounted. However, it should execute the operations only
# once after a system upgrade.

# All code found here must be kept for long-life term or even forever and
# must always contain, at least, the following information: title, version
# it was added and reason why it was added (description).

# mount /data in tmpfs if unavailable
if [ ! -w /data/ ] && (! /usr/bin/mount | /usr/bin/grep /data | /usr/bin/grep -q -e ext4 -e ubifs); then
    /usr/bin/mount -t tmpfs -o defaults,noatime,mode=1777 tmpfs /data
fi

# mount /root in tmpfs if unavailable
if [ ! -w /root/ ] && (! /usr/bin/mount | /usr/bin/grep -q /root); then
    /usr/bin/mount -t tmpfs -o defaults,noatime,mode=1777 tmpfs /root
    # let others know of what we did...
    /usr/bin/touch /tmp/root-mounted-tmpfs
fi

# make sure /data is writable by HMI user.
# forced-install updates can retain the same version, but install new contents of user data,
# without this, it is impossible to save alsamixer state, the error being:
# /usr/sbin/alsactl: save_state:1635: Cannot open /data/asound.state for writing: Permission denied
/usr/bin/chmod 777 /data/

# check if it's a new version
if [[ -f /data/.last-data-operation ]]; then
    DATA_OP=$(/usr/bin/cat /data/.last-data-operation | /usr/bin/cut -d'.' -f1-3)
    RELEASE=$(/usr/bin/cat /etc/mod-release/release | /usr/bin/cut -d'.' -f1-3)
    LATEST=$(/usr/bin/echo -e "${DATA_OP}\n${RELEASE}" | /usr/bin/sort -t. -k 1,1n -k 2,2n -k 3,3n | /usr/bin/tail -n1)

    if [[ "${LATEST}" == "${DATA_OP}" ]]; then
        exit 0
    fi
fi

# title: remove old authorized_keys
# added: v1.1.0
# description: Since this version ssh access via password is allowed again.
# The authorized_keys should not contain any old access key.
if [[ -f /root/.ssh/authorized_keys ]]; then
    /usr/bin/sed -i '/user@mod/d' /root/.ssh/authorized_keys
fi

# title: adjust asound.state for newly added CV controls
# added: v1.9.0
# description: amixer fails to load state from v1.8 in v1.9 onwards, because we added new controls to the soundcard.
# To fix that, we manually patch the indexes and introduce good known values.
# Note that this is only relevant if running Duo X with imx8mq model (since v1.8 does not work on rk3399 model).
if [[ -f /data/asound.state ]] && [[ -e /proc/device-tree/compatible ]]; then
    MODEL_IMX8MQ=$(/usr/bin/grep -q imx8mq /proc/device-tree/compatible && /usr/bin/echo 1)
    if [[ -n "${MODEL_IMX8MQ}" ]] && (! /usr/bin/grep -q "Headphone/CV Mode" /data/asound.state); then
          cp /data/asound.state /tmp/asound.state
          sed -i "s/control.27 {/control.30 {/" /tmp/asound.state
          sed -i "s/control.26 {/control.29 {/" /tmp/asound.state
          sed -i "s/control.25 {/control.28 {/" /tmp/asound.state
          sed -i "s/control.24 {/control.27 {/" /tmp/asound.state
          sed -i "s|control.23 {|control.23 {\n\
                iface MIXER\n\
                name 'Headphone/CV Mode'\n\
                value false\n\
        }\n\
        control.24 {\n\
                iface MIXER\n\
                name 'CV/Exp.Pedal Mode'\n\
                value false\n\
        }\n\
        control.25 {\n\
                iface MIXER\n\
                name 'Exp.Pedal Mode'\n\
                value true\n\
        }\n\
        control.26 {|" /tmp/asound.state
          mv /tmp/asound.state /data/asound.state
    fi
fi

# title: adjust asound.state for ensuring a few options are enabled
# added: v1.9.1 (with a few more values in v1.9.2)
# description: on duox, we want a few mixer options to be enabled by default
# In order to ensure that happens for existing users, we patch the asound.state here
if [[ -f /data/asound.state ]] && [[ -e /proc/device-tree/compatible ]]; then
    if /usr/bin/grep -q -e imx8mq -e rk3399 /proc/device-tree/compatible; then
        cp /data/asound.state /tmp/asound.state
        sed -i "/'ADC Soft Ramp Switch'/{n;s/.*/\t\tvalue true/}" /tmp/asound.state
        sed -i "/'ADC Zero Cross Switch'/{n;s/.*/\t\tvalue true/}" /tmp/asound.state
        sed -i "/'DAC INV Switch'/{n;s/.*/\t\tvalue true/}" /tmp/asound.state
        sed -i "/'DAC Zero Cross Switch'/{n;s/.*/\t\tvalue true/}" /tmp/asound.state
        sed -i "/'De-emp 44.1kHz Switch'/{n;s/.*/\t\tvalue false/}" /tmp/asound.state
        mv /tmp/asound.state /data/asound.state
    fi
fi

# title: delete known factory pedalboards from user data
# added: v1.13.0
# description: starting on 1.13, dwarf includes factory pedalboards in the system partition
# We run a tool that deletes factory pedalboards in user data in a safe way
# (achieved through hashing of critical pedalboard files)
if [[ -e /proc/device-tree/compatible ]]; then
    if /usr/bin/grep -q px30 /proc/device-tree/compatible; then
        /usr/bin/mod-user-pedalboards-cleanup
        if [[ -f /root/data/banks.json ]] && (/bin/echo a6eccb780096c3ef6bab3760b27d491d /root/data/banks.json | /usr/bin/md5sum -c --status -); then
            /usr/bin/rm /root/data/banks.json
        fi
    fi
fi

# update last data operation
/usr/bin/cp /etc/mod-release/release /data/.last-data-operation
exit 0
