#!/bin/sh /etc/rc.common

START=99
STOP=10

DIR=/usr/share/tm_cpps
OPT=/opt/storage/files/tm_cpps
###TMP=/var/run/tm_cpps
TMP=/tmp/tm_cpps

mkDIR()
{
  [ -f "$1" ] || mkdir -p "$1"
}
lnDIR()
{
  mkDIR "$1"
  ln -sf "$1" "$DIR"
}
lnFILE()
{
  [ -f "$1" ] || ln -s "$1" "$DIR"
}
lnOPT()
{
  [ -f "$DIR"/"$1" ] || ln -s "$OPT"/"$1" "$DIR"
}
HTML="$TMP"/html
delHTML()
{
  [ -d "$HTML" ] && rm -rf "$HTML"
}

start() {
  mkDIR "$TMP"

  HTMLgz="$DIR"/html.tar.gz
  [ -f "$HTMLgz" ] || {
    echo "not found" "$HTMLgz"
    logger -t "tm-server" "not found" "$HTMLgz"
  #  exit 1
  }

  delHTML
  tar -C "$TMP" -zxf "$HTMLgz"
  [ -d "$HTML" ] || {
    echo "extract failed" "$HTMLgz"
    logger -t "tm-server" "extract failed" "$HTMLgz"
  #  exit 1
  }
###  lnFILE "$HTML" "$DIR"

  ## prepare sim-link to local DB to tmpfs
###  lnFILE "$TMP"/vals.sqlite

  ## prepare log dir
###  lnDIR "$TMP"/logs

  ## prepare confs dir
###  lnDIR "$OPT"/confs

  ## prepare scheme dir
###  lnDIR "$OPT"/scheme
  
  ## prepare permanent configs from $OPT
  mkDIR "$OPT"

###  lnOPT users.cfg
###  lnOPT users.xml
###  lnOPT all.cfg
###  lnOPT sys.cfg
###  lnOPT conf.xml
###  lnOPT conf_back.xml
###  lnOPT pats.xml
###  lnOPT tm_server.k2

  "$DIR"/tm_server start
}

stop() {
  "$DIR"/tm_server stop
  delHTML
}
