
# MaxxAudio™ functions
# gh


prepare_environment() {
  ui_print " "
  ui_print "- Preparing environment..."
  if [ "$BOOTMODE" ]; then
    mirror=/sbin/.magisk/mirror
    sys=$(realpath $mirror/system)
    ven=$(realpath $mirror/vendor)
  else
    if ! is_mounted /data; then mount /data; fi
    if ! is_mounted /system; then mount /system; fi
    if ! is_mounted /vendor; then mount /vendor; fi
    if [ -e /system/system/build.prop ]; then
      sys=/system/system
    else
      sys=/system
    fi
    ven=$(realpath /vendor)
  fi
  etc=$sys/etc
  vetc=$ven/etc
  unzip -qqo "$ZIPFILE" 'MaxxAudio/*' -d $TMPDIR >&2
  cp -rf $TMPDIR/MaxxAudio/* $MODPATH
  acdbconfig=$MODPATH/acdb.conf
  modetc=$MODPATH/system/etc
  modven=$MODPATH/system/vendor
  modvetc=$MODPATH/system/vendor/etc
  modlib=$MODPATH/system/vendor/lib
  sleep 1
  ui_print "  ✓ Ready"
  sleep 1
}

configure_fx() {
  ui_print " "
  ui_print "- Checking for external module audio effect configuration support..."
  sleep 1
  acdb=$(find /data/adb -type d -name "acdb")
  aml=$(find /data/adb -type d -name "aml")
  acdbsupport=false
  amlsupport=false
  conf=*audio_effects*conf
  xml=*audio_effects*xml
  if [ -d "$acdb" ] && [ ! -d "$aml" ]; then
    ui_print "  √ ACDB module detected and will handle effect configuration each device boot."
    acdbsupport=true
  elif [ -d "$aml" ]; then
    ui_print "  √ AML module detected and will handle effect configuration each device boot."
    amlsupport=true
  else
    ui_print "  ! Install Audio Modification Library module for consistent audio effect configuration each device boot."
    sleep 1
    ui_print " "
    ui_print "- Patching all Magisk module audio effects configurations (unpredictable and vulnerable to pre-existing errors)..."
    for fxconfig in $etc/$conf; do
      if [ -e "$fxconfig" ]; then
        mkdir -p $modetc
        cp -f $fxconfig $modetc/
      fi
    done
    for fxconfig in $vetc/$conf $vetc/$xml; do
      if [ -e "$fxconfig" ]; then
        cp -f $fxconfig $modvetc/
        rm -f $modvetc/audio_effects_tune.xml 2>/dev/null
      fi
    done
    conf=$(find /data/adb/modules* -type f -name "*audio_effects*.conf")
    xml=$(find /data/adb/modules* -type f -name "*audio_effects*.xml") 
    libraryid=$(cat $acdbconfig | sed -n -e 's/^libraryid=//p')
    libraryname=$(cat $acdbconfig | sed -n -e 's/^libraryname=//p')
    effectid=$(cat $acdbconfig | sed -n -e 's/^effectid=//p')
    effectuuid=$(cat $acdbconfig | sed -n -e 's/^effectuuid=//p')
    for fxconfig in $xml; do
      if [ -e "$fxconfig" ]; then
        effect=$(grep $effectuuid $fxconfig)
        if [ ! "$effect" ]; then
          sed -i "1,/^    <\/libraries>/s/^    <\/libraries>/        <library name=\"$libraryid\" path=\"$libraryname\"\/>\n    <\/libraries>/" $fxconfig
          sed -i "1,/^    <\/effects>/s/^    <\/effects>/        <effect name=\"$effectid\" library=\"$libraryid\" uuid=\"$effectuuid\"\/>\n    <\/effects>/" $fxconfig
          sed -i "1,/^            <apply effect=\"music\_helper\"\/>/s/^            <apply effect=\"music\_helper\"\/>/            <!-- apply effect=\"music\_helper\"\/ -->/" $fxconfig
          sa3d=$(grep "sa3d" $fxconfig)
          if [ "$sa3d" ]; then
            sed -i "s/^            <apply effect=\"sa3d\"\/>/            <apply effect=\"sa3d\"\/>\n            <apply effect=\"$effectid\"\/>/g" $fxconfig
          fi
        fi
      fi
    done
    for fxconfig in $conf; do
      if [ -e "$fxconfig" ]; then
        effect=$(grep $effectuuid $fxconfig)
        if [ ! "$effect" ]; then
          sed -i "s/^libraries {/libraries {\n  $libraryid {\n    path \/vendor\/lib\/soundfx\/$libraryname\n  }/" $fxconfig
          sed -i "s/^effects {/effects {\n  $effectid {\n    library $libraryid\n    uuid $effectuuid\n  }/" $fxconfig
          sed -i "/^        music_helper {/ {;N s/        music_helper {\n        }/#        music_helper {\n#        }/}" $fxconfig
        fi
      fi
    done
    sleep 1
    ui_print "  ✓ Configured"
  fi
  sleep 1
}

finalize_module() {
  ui_print " "
  ui_print "- Finalizing module..."
  set_perm_recursive $MODPATH 0 0 0755 0644
  chown 0:2000 $modvetc $modlib $modlib/soundfx
  chcon -R u:object_r:vendor_file:s0 $modven
  chcon -R u:object_r:vendor_configs_file:s0 $modvetc
  sleep 1
  ui_print "  √ gh"
  ui_print " "
  sleep 1
}

go() {
  prepare_environment
  configure_fx
  finalize_module
}
