#!/bin/bash

MACHINE_TYPE=""
if [ -e /etc/astra/machine_type ]; then
    MACHINE_TYPE=`cat /etc/astra/machine_type | head -1 | tr -s ' '`
    echo "astra-mobile: detected machine type $MACHINE_TYPE"
fi

if [[ $MACHINE_TYPE == "KVADRAT.arm64"* ]]; then
    cp -f /root/hardware/yadro/kvadrat.pref /etc/apt/preferences.d/

    # Remove old files which were used before 4.7.8 and 4.7.9 accordingly:
    rm -f /etc/apt/preferences.d/kvadrat
    rm -f /etc/apt/preferences
elif [[ $MACHINE_TYPE == "LT11.arm64"* ]]; then
    cp -f /root/hardware/mig/lt11/lt11.pref /etc/apt/preferences.d/

    # Remove old file which was used before 4.7.9:
    rm -f /etc/apt/preferences
elif [[ $MACHINE_TYPE == "OKOTAB5PRO.arm64"* ]]; then
    cp -f /root/hardware/byterg/okotab5pro.pref /etc/apt/preferences.d/
elif [[ $MACHINE_TYPE == "PINEPRO.arm64"* ]]; then
    cp -f /root/hardware/pine64/pinephonepro.pref /etc/apt/preferences.d/
elif [[ $MACHINE_TYPE == "ZHISD.arm64"* ]]; then
    cp -f /root/hardware/zhisd/arm64/zhisd.pref /etc/apt/preferences.d/
fi

ADDONS_REPO_LIST="/etc/apt/sources.list.d/astra-mobile-addons.list"
if [ -e $ADDONS_REPO_LIST ] && [ -e /etc/astra/mobile_version ]; then
    ASTRA_VERSION=$(cat /etc/astra/mobile_version | sed -e 's/^\(.*\)\.mobile.*/\1/')

    if [[ $MACHINE_TYPE == "KVADRAT.arm64"* ]] && [[ $ASTRA_VERSION == "4.7.7" ]]; then
        sed -i "s/ 4.7_arm .*$/ 4.7_arm 4.7.7 4.7.7_kvadrat/g" $ADDONS_REPO_LIST
    else
        sed -i "s/ 1.7_x86-64 .*$/ 1.7_x86-64 $ASTRA_VERSION/g" $ADDONS_REPO_LIST
        sed -i "s/ 1.8_x86-64 .*$/ 1.8_x86-64 $ASTRA_VERSION/g" $ADDONS_REPO_LIST
        sed -i "s/ 4.7_arm .*$/ 4.7_arm $ASTRA_VERSION/g" $ADDONS_REPO_LIST
        sed -i "s/ 4.8_arm .*$/ 4.8_arm $ASTRA_VERSION/g" $ADDONS_REPO_LIST
    fi
fi

if [ "$(du -s /var/cache/apt/archives | awk '{ print $1 }')" -gt "10240" ]; then
    apt clean
fi
