

# Audio Configuration Database functions
# gh


build() {
  ui_print " "
  ui_print "• Building database..."
  unzip -qqo "$ZIPFILE" 'acdb/*' -d $TMPDIR >&2
  cp -rf $TMPDIR/acdb/* $MODPATH
  if [ "$BOOTMODE" ]; then
    mirror=/sbin/.magisk/mirror
    sys=$(realpath $mirror/system)
    ven=$(realpath $mirror/vendor)
  else
    umount /system 2>/dev/null
    umount /vendor 2/dev/null
    mount /system 2>/dev/null
    mount /vendor 2>/dev/null
    if [ -e /system/system/build.prop ]; then
      sys=system/system
    else
      sys=/system
    fi
    ven=$(realpath /vendor)
  fi
  module=$(find /data/adb/modules* -mindepth 1 -maxdepth 1 -type d -not -path $MODPATH -print)
  modulefx=$(find $module -type f -name "*audio_effects*" ! -name "audio_effects_tune*")
  acdbconfig=$(find $module -type f -name "acdb.conf")
  acdbconfigs=$(find $module -type f -name "acdb[0-9].conf")
  acdbetc=$MODPATH/system/etc
  acdbsvetc=$MODPATH/system/vendor/etc
  etc=$sys/etc
  vetc=$ven/etc
  conf=*audio_effects*.conf
  xml=*audio_effects*.xml
  for fxconfig in $etc/$conf; do
    if [ -e "$fxconfig" ]; then
      cp -f $fxconfig $acdbetc/
      rm -f $acdbetc/placeholder 2>/dev/null
    fi
  done
  for fxconfig in $vetc/$conf $vetc/$xml; do
    if [ -e "$fxconfig" ]; then
      cp -f $fxconfig $acdbsvetc/
      rm -f $acdbsvetc/audio_effects_tune.xml 2>/dev/null
      rm -f $acdbsvetc/placeholder 2>/dev/null
    fi
  done
  sleep 1
}

nuke() {
  ui_print "• Deleting audio effects configurations from modules..."
  sleep 2
  ui_print "  ! An audio effect module no longer working will need a completed acdb.conf file in the module's root directory."
  sleep 2
  ui_print "  ! Refer module developer to this ACDB zip for effortless ACDB support, if necessary."
  ui_print " "
  if [ -e "$modulefx" ]; then
    rm -f $modulefx
  fi
  sleep 2
}

configure() {
  ui_print "• Configuring database..."
  for disable in /data/adb/modules/*/disable; do
    if [ -e "$disable" ]; then
      disabled=$(dirname $disable)
      for config in $disabled/acdb*.conf; do
        if [ -e "$config" ]; then
          mv -f $config $config.off
        fi
      done
    fi
  done
  for config in $module/acdb*.conf; do
    if [ -e "$config.off" ]; then
      disabled=$(dirname $config.off)
      for module in $disabled; do
        if [ ! -e "$module/disable" ]; then
          mv -f $config.off $config
        fi
      done
    fi
  done
  conf=$(find $MODPATH -type f -name "*audio_effects*.conf")
  xml=$(find $MODPATH -type f -name "*audio_effects*.xml")
  for config in $acdbconfig $acdbconfigs; do
    if [ -e "$config" ]; then 
      libraryid=$(cat $config | sed -n -e 's/^libraryid=//p')
      libraryname=$(cat $config | sed -n -e 's/^libraryname=//p')
      effectid=$(cat $config | sed -n -e 's/^effectid=//p')
      effectuuid=$(cat $config | sed -n -e 's/^effectuuid=//p')
      musicstream=$(grep "musicstream=true" $config)
      for libid in $libraryid; do
        library_id=${libid}
        for libname in $libraryname; do
          library_name=${libname}
          for fxid in $effectid; do
            effect_id=${fxid}
            for fxuuid in $effectuuid; do
              effect_uuid=${fxuuid}
              for fxconfig in $xml; do
                if [ -e "$fxconfig" ]; then
                  sed -i "1,/^    <\/libraries>/s/^    <\/libraries>/        <library name=\"$library_id\" path=\"$library_name\"\/>\n    <\/libraries>/" $fxconfig
                  sed -i "1,/^    <\/effects>/s/^    <\/effects>/        <effect name=\"$effect_id\" library=\"$library_id\" uuid=\"$effect_uuid\"\/>\n    <\/effects>/" $fxconfig
                  if [ "$musicstream" ]; then
                    sed -i "s/^        <stream type=\"music\">/        <stream type=\"music\">\n            <apply effect=\"$effect_id\"\/>/g" $fxconfig
                  fi
                  sed -i "1,/^            <apply effect=\"alarm\_helper\"\/>/s/^            <apply effect=\"alarm\_helper\"\/>/            <!-- apply effect=\"alarm\_helper\"\/ -->/" $fxconfig
                  sed -i "1,/^            <apply effect=\"music\_helper\"\/>/s/^            <apply effect=\"music\_helper\"\/>/            <!-- apply effect=\"music\_helper\"\/ -->/" $fxconfig
                  sed -i "1,/^            <apply effect=\"notification\_helper\"\/>/s/^            <apply effect=\"notification\_helper\"\/>/            <!-- apply effect=\"notification\_helper\"\/ -->/" $fxconfig
                  sed -i "1,/^            <apply effect=\"ring\_helper\"\/>/s/^            <apply effect=\"ring\_helper\"\/>/            <!-- apply effect=\"ring\_helper\"\/ -->/" $fxconfig
                fi
              done
              for fxconfig in $conf; do
                if [ -e "$fxconfig" ]; then
                  sed -i "s/^libraries {/libraries {\n  $library_id {\n    path \/vendor\/lib\/soundfx\/$library_name\n  }/" $fxconfig
                  sed -i "s/^effects {/effects {\n  $effect_id {\n    library $library_id\n    uuid $effect_uuid\n  }/" $fxconfig
                  if [ "$musicstream" ]; then
                    sed -i "s/^    music {/    music {\n        $effect_id {\n        }/g" $fxconfig
                  fi
                  sed -i "/^        alarm_helper {/ {;N s/        alarm_helper {\n        }/#        alarm_helper {\n#        }/}" $fxconfig
                  sed -i "/^        music_helper {/ {;N s/        music_helper {\n        }/#        music_helper {\n#        }/}" $fxconfig
                  sed -i "/^        notification_helper {/ {;N s/        notification_helper {\n        }/#        notification_helper {\n#        }/}" $fxconfig
                  sed -i "/^        ring_helper {/ {;N s/        ring_helper {\n        }/#        ring_helper {\n#        }/}" $fxconfig
                fi
              done
            done
          done
        done
      done
    fi
  done
  sleep 1
}

finalize() {
  ui_print "• Finalizing database..."
  set_perm_recursive $MODPATH 0 0 0755 0644
  chown 0:2000 $acdbsvetc
  chcon u:object_r:vendor_file:s0 $MODPATH/system/vendor
  chcon -R u:object_r:vendor_configs_file:s0 $acdbsvetc
  sleep 1
}

tidyup() {
  ui_print "• Tidying up..."
  rm -rf $MODPATH/META-INF
  rm -f $MODPATH/customize.sh
  ui_print " "
  sleep 1
}

go() {
  build
  nuke
  configure
  finalize
  tidyup
  ui_print "gh"
  sleep 1 && exit 0
}
