# Added the awk calls to drop the Utopia trace garbage...
model=`pcedit -q rokumodel | awk 'END{print}' | sed 's:/:_:g'`
odmmodel=`pcedit -q odmmodel | awk 'END{print}' | sed 's:/:_:g'`
panel=`pcedit -q panel | awk 'END{print}'`
speaker=`pcedit -q speaker | awk 'END{print}'`
echo odmmodel=${odmmodel} model=${model} panel=${panel} speaker=${speaker}

# LV-2616 fix a TCL factory bug that fail to put correct rokumodel

if [ $model == "(unset)" ] && [ $odmmodel != "(unset)" ]
then
    odmmodel_type=${odmmodel:0:3}
    # 43S405,43S403,49S405,49S403,55S401,55S403,55S405,65S401,65S403,65S405
    echo type=$odmmodel_type
    if [ $odmmodel_type == "43S" ] || [ $odmmodel_type == "49S" ] || [ $odmmodel_type == "55S" ] || [ $odmmodel_type == "65S" ]
    then
        echo "==== update roku model to $odmmodel ===="
        model=`grep $odmmodel /custom/projects | awk 'END{print $10}' | sed 's/["|,]//g'`
        echo model=$model
        pcedit rokumodel="$model" -w
    fi
fi

# Figure out the PQ database file.  PQ is loaded as one single file as-follows:
#
#  1. If dev_mode, try loading V_{rokumodel}_{panel}.txt from USB stick.  If doesn't exist,
#   2. If dev_mode, try loading V_{rokumodel}.txt from USB stick.  If doesn't exist,
#    3. If dev_mode, try loading V_Default.txt from USB stick.  If doesn't exist,
#     4. Try loading V_{rokumode}_{panel}.txt from /custom/pq acramfs.  If doesn't exixt,
#      5. Try loading V_{rokumode}.txt from /custom/pq acramfs.  If doesn't exixt,
#       6. Load V_Default.txt from /custom/pq acramfs.
#
# Also, if there is a "pq/" directory on the USB stick, first copy all files
# from USB stick pq/ directory into NVRAM (i.e. nvram/pq directory).
#

pq_dir="/custom/pq"

# check if configuration file for PQ exits in USB drive
if [ $dev_mode_enabled = 1 ]
then    
    usb_path="/media/ext1:/pq" #NOTE: we do not need '\' here
    if [ -d "${usb_path}" ] ; then
        rm -rf /nvram/pq                 # clean up '/nvram/pq' only when sideloading
        mkdir -p /nvram/pq
        mkdir -p /tmp/pq
        cp -r ${usb_path}/. /nvram/pq    # /. is important since FAT is case insensitive
        cp -r ${usb_path}/. /tmp/pq    # /. is important since FAT is case insensitive
        echo "Copied PQ data from USB to /nvram/pq and /tmp/pq"
    fi
    if [ -e "${usb_path}/V_${model}_${panel}.txt" ] ; then
        echo "using '${usb_path}/V_${model}_${panel}.txt'"
        pq_dir=${usb_path}
    elif [ -e "${usb_path}/V_${model}.txt" ] ; then
        echo "using '${usb_path}/V_${model}.txt'"
        pq_dir=${usb_path}
    elif [ -e ${usb_path}/V_Default.txt ] ; then
        echo "using '${usb_path}/V_Default.txt'"
        pq_dir=${usb_path}
    fi
fi

# Look for the model/panel specific file first, the model specific file second and the default last
if [ -e "${pq_dir}/V_${model}_${panel}.txt" ] ; then
  pqconfig /tmp/V_Default.pqdb fileinit "${pq_dir}/V_${model}_${panel}.txt" > /dev/null 2>&1
elif [ -e "${pq_dir}/V_${model}.txt" ] ; then
  pqconfig /tmp/V_Default.pqdb fileinit "${pq_dir}/V_${model}.txt" > /dev/null 2>&1
elif [ -e ${pq_dir}/V_Default.txt ] ; then
  pqconfig /tmp/V_Default.pqdb fileinit ${pq_dir}/V_Default.txt > /dev/null 2>&1
fi
rm -f /tmp/V_Default.txt
rm -f /tmp/V_Default.pqdb.bak

mkdir -p /tmp/pq
# Unzip Main.bin,Main_Color.bin (Qmap) and DLC.ini
if [ -e "/tmp/pq/Main.bin" ] ; then # avoid overwriting sideloaded Main.bin
  echo "side-loading /tmp/pq/Main.bin"
elif [ -e "${pq_dir}/Main_${model}_${panel}.bin.zip" ] ; then # in case we need big Qmap per model (rarely happen)  
  unzip ${pq_dir}/Main_${model}_${panel}.bin.zip -d /tmp/pq  
elif [ -e "${pq_dir}/Main_${model}.bin.zip" ] ; then # in case we need big Qmap per model (rarely happen)  
  unzip ${pq_dir}/Main_${model}.bin.zip -d /tmp/pq
elif [ -e "${pq_dir}/Main.bin.zip" ]; then
  unzip ${pq_dir}/Main.bin.zip -d /tmp/pq
elif [ -e "/custom/pq/Main_${model}.bin.zip" ] ; then # fall back to custom package  
  unzip /custom/pq/Main_${model}.bin.zip -d /tmp/pq  
elif [ -e "/custom/pq/Main.bin.zip" ]; then  
  unzip /custom/pq/Main.bin.zip -d /tmp/pq    
fi

if [ -e "/tmp/pq/Main_Color.bin" ] ; then # avoid overwriting sideloaded Main_Color.bin
  echo "side-loading /tmp/pq/Main_Color.bin"
elif [ -e "${pq_dir}/Main_${model}_${panel}_Color.bin.zip" ] ; then # unzip small Qmap
  unzip ${pq_dir}/Main_${model}_${panel}_Color.bin.zip -d /tmp/pq  
elif [ -e "${pq_dir}/Main_${model}_Color.bin.zip" ] ; then # unzip small Qmap
  unzip ${pq_dir}/Main_${model}_Color.bin.zip -d /tmp/pq
elif [ -e "${pq_dir}/Main_Color.bin.zip" ]; then
  unzip ${pq_dir}/Main_Color.bin.zip -d /tmp/pq
elif [ -e "/custom/pq/Main_${model}_Color.bin.zip" ]; then # fall back to custom package
  unzip /custom/pq/Main_${model}_Color.bin.zip -d /tmp/pq
elif [ -e "/custom/pq/Main_Color.bin.zip" ]; then
  unzip /custom/pq/Main_Color.bin.zip -d /tmp/pq    
fi

if [ -e "/tmp/pq/Main_TMO.bin" ] ; then # avoid overwriting sideloaded Main_TMO.bin
  echo "side-loading /tmp/pq/Main_TMO.bin"
elif [ -e "${pq_dir}/Main_${model}_${panel}_TMO.bin.zip" ] ; then # unzip ToneMap Qmap
  unzip ${pq_dir}/Main_${model}_${panel}_TMO.bin.zip -d /tmp/pq
elif [ -e "${pq_dir}/Main_${model}_TMO.bin.zip" ] ; then # unzip ToneMap Qmap
  unzip ${pq_dir}/Main_${model}_TMO.bin.zip -d /tmp/pq
elif [ -e "${pq_dir}/Main_TMO.bin.zip" ]; then
  unzip ${pq_dir}/Main_TMO.bin.zip -d /tmp/pq
elif [ -e "/custom/pq/Main_${model}_${panel}_TMO.bin.zip" ] ; then # fall back to custom package
  unzip /custom/pq/Main_${model}_${panel}_TMO.bin.zip -d /tmp/pq  
elif [ -e "/custom/pq/Main_${model}_TMO.bin.zip" ] ; then # fall back to custom package
  unzip /custom/pq/Main_${model}_TMO.bin.zip -d /tmp/pq  
fi

if [ -e "/tmp/pq/DLC.ini" ] ; then # avoid overwriting sideloaded DLC.ini
  echo "side-loading /tmp/pq/DLC.ini"
elif [ -e "${pq_dir}/DLC_${model}_${panel}.ini.zip" ] ; then # unzip DLC.ini per model given panel
  unzip ${pq_dir}/DLC_${model}_${panel}.ini.zip -d /tmp/pq
elif [ -e "${pq_dir}/DLC_${model}.ini.zip" ] ; then # unzip DLC.ini per model
  unzip ${pq_dir}/DLC_${model}.ini.zip -d /tmp/pq      
elif [ -e "${pq_dir}/DLC.ini.zip" ] ; then # unzip DLC.ini
  unzip ${pq_dir}/DLC.ini.zip -d /tmp/pq
elif [ -e "/custom/pq/DLC_${model}.ini.zip" ] ; then # unzip DLC.ini per model
  unzip /custom/pq/DLC_${model}.ini.zip -d /tmp/pq      
elif [ -e "/custom/pq/DLC.ini.zip" ] ; then # fall back to custom package
  unzip /custom/pq/DLC.ini.zip -d /tmp/pq  
fi

if [ -e "${pq_dir}/libipq.so" ] ; then
    cp ${pq_dir}/libipq.so /tmp/pq
    echo "Copied ipq library from custom package to /tmp/pq " 
fi


# Figure out the AQ database file.  AQ is hierarchical i.e. load any model/panel or model specific
# file, then Default file, and finally the Master file.  The AQ loading process is as-follows:
#
#  1. If dev_mode, try loading A_{rokumodel}_{speaker}.txt from USB stick.  If doesn't exist,
#   2. If dev_mode, try loading A_{rokumodel}.txt from USB stick.  If doesn't exist,
#    3. Try loading A_{rokumode}_{speaker}.txt from /custom/aq acramfs.  If doesn't exixt,
#     4. Try loading A_{rokumode}.txt from /custom/aq acramfs.
#
#  5. If dev_mode, try loading A_Default.txt from USB stick.  If doesn't exist,
#   6. Load A_Default.txt from /custom/aq acramfs.
#
#  7. Load A_Master.txt from /custom/aq acramfs.
#
# Also, if there is an "aq/" directory on the USB stick, first copy all files
# from USB stick aq/ directory into NVRAM (i.e. nvram/aq directory).
#
aq_dir="/custom/aq"

# check if configuration file for AQ exits in USB drive
if [ $dev_mode_enabled = 1 ]
then    
    usb_path="/media/ext1:/aq" #NOTE: we do not need '\' here
    if [ -d "${usb_path}" ] ; then
        rm -rf /nvram/aq                # clean up '/nvram/aq' only when sideloading 
        mkdir -p /nvram/aq
        cp -r ${usb_path}/. /nvram/aq   # /. is important since FAT is case insensitive
        echo "Copied AQ data from USB to /nvram"
    fi
    if [ -e "${usb_path}/A_${model}_${speaker}.txt" ] ; then
        echo "using '${usb_path}/A_${model}_${speaker}.txt'"
        aq_dir=${usb_path}
    elif [ -e "${usb_path}/A_${model}.txt" ] ; then
        echo "using '${usb_path}/A_${model}.txt'"
        aq_dir=${usb_path}
    # Make it easier to sideload model layer from USB
    elif [ -e "${usb_path}/A_Model.txt" ] ; then
        echo "using '${usb_path}/A_Model.txt'"
        aq_dir=${usb_path}
    fi
fi

# Model is the model specific settings, if any
if [ -e "${aq_dir}/A_${model}_${speaker}.txt" ] ; then
  pqconfig /tmp/A_Model.aqdb fileinit "${aq_dir}/A_${model}_${speaker}.txt" > /dev/null 2>&1
elif [ -e "${aq_dir}/A_${model}.txt" ] ; then
  pqconfig /tmp/A_Model.aqdb fileinit "${aq_dir}/A_${model}.txt" > /dev/null 2>&1
elif [ -e "${aq_dir}/A_Model.txt" ] ; then
  pqconfig /tmp/A_Model.aqdb fileinit "${aq_dir}/A_Model.txt" > /dev/null 2>&1
fi

# For AQ, we always process A_Default.txt unlike PQ...
aq_dir="/custom/aq"

# check if configuration file for AQ exits in USB drive
if [ $dev_mode_enabled = 1 ]
then
    usb_path="/media/ext1:/aq" #NOTE: we do not need '\' here
    if [ -e "${usb_path}/A_Default.txt" ] ; then
        echo "using '${usb_path}/A_Default.txt'"
        aq_dir=${usb_path}
    fi
fi

if [ -e ${aq_dir}/A_Default.txt ] ; then
  pqconfig /tmp/A_Default.aqdb fileinit ${aq_dir}/A_Default.txt > /dev/null 2>&1
fi

# A_Master.aqdb is a new addition.  It is the base layer for all products
# containing all settings and their default values.  Simplifies the code in
# audio device for initializing the aqdb.
if [ -e /custom/aq/A_Master.txt ] ; then
  pqconfig /tmp/A_Master.aqdb fileinit /custom/aq/A_Master.txt > /dev/null 2>&1
fi

# Adding sound effect core supplements for Dolby DAP and DTS Studio/TheaterSound
if [ -e /custom/aq/A_Master_DAP_Supp.txt ] ; then
  pqconfig /tmp/A_Master_DAP_Supp.aqdb fileinit /custom/aq/A_Master_DAP_Supp.txt > /dev/null 2>&1
fi
if [ -e /custom/aq/A_Master_DTS_Supp.txt ] ; then
  pqconfig /tmp/A_Master_DTS_Supp.aqdb fileinit /custom/aq/A_Master_DTS_Supp.txt > /dev/null 2>&1
fi
if [ -e /custom/aq/A_Master_DTSVX_Supp.txt ] ; then
  pqconfig /tmp/A_Master_DTSVX_Supp.aqdb fileinit /custom/aq/A_Master_DTSVX_Supp.txt > /dev/null 2>&1
fi
# Support split sound modes for VSurr toggle
if [ -e /custom/aq/A_Master_VSurr_Supp.txt ] ; then
  pqconfig /tmp/A_Master_VSurr_Supp.aqdb fileinit /custom/aq/A_Master_VSurr_Supp.txt > /dev/null 2>&1
fi
# Adding sound effect core supplement for Sonic Emotion
if [ -e /custom/aq/A_Master_SonicEmotion_Supp.txt ] ; then
  pqconfig /tmp/A_Master_SonicEmotion_Supp.aqdb fileinit /custom/aq/A_Master_SonicEmotion_Supp.txt > /dev/null 2>&1
fi
if [ -e /custom/aq/A_Master_AudioWeaver_Supp.txt ] ; then
  pqconfig /tmp/A_Master_AudioWeaver_Supp.aqdb fileinit /custom/aq/A_Master_AudioWeaver_Supp.txt > /dev/null 2>&1
fi

# Clean up unnecessary files in tmp
rm -f /tmp/A_Model.aqdb.bak
rm -f /tmp/A_Default.aqdb.bak
rm -f /tmp/A_Master.aqdb.bak
rm -f /tmp/A_Master_DAP_Supp.aqdb.bak
rm -f /tmp/A_Master_DTS_Supp.aqdb.bak
rm -f /tmp/A_Master_DTSVX_Supp.aqdb.bak
rm -f /tmp/A_Master_VSurr_Supp.aqdb.bak
rm -f /tmp/A_Master_SonicEmotion_Supp.aqdb.bak
rm -f /tmp/A_Master_AudioWeaver_Supp.aqdb.bak