#!/system/bin/bash

conf="/data/local/tmp/XtremeBS/XtremeBS.conf"

getconf() {
  # change made here to set default
  # if not set
  opt=$(grep "^$1=" "$conf" | cut -d= -f2)
  [ "$opt" != "" ] && echo "$opt" || echo "$2"
}

ctl_file=$(getconf ctl_file "/data/local/tmp/xbs")
version=$(getconf version 1)


print_help_v2() {
cat << EOM
  XtremeBS Controller Options:

  start		starts the manual event or a custom event

  stop		stops the manual event or a custom event

  pause		pauses the XtremeBS daemon

  resume	resumes the XtremeBS daemon or exits safe mode

  safe		stops all events and enters safe mode
EOM
}

print_help_v1() {
cat << EOM
  XtremeBS Controller Options:

  start		enables power save mode manually.

  stop		disables power save mode manually.

  reload	reloads the config file.

  pause		pauses the daemon.

  resume	resumes the daemon or exits Safe Mode.

  safe		Enters Safe Mode.
EOM
}

case $1 in
  ''|'-h'|'--help')
    if [ "$version" = 2 ]; then
      print_help_v2
    else
      print_help_v1
    fi
    ;;
  *)
    if [[ "$1" = "start" || "$1" = "stop" ]] && [ "$version" = "2" ] && [ ! "$2" ]; then
      echo "start manual" > "$ctl_file"
    else
      echo "$@" > "$ctl_file"
    fi
    ;;
esac
