#!/bin/bash
# -*- coding: utf-8 -*-
# z0librc installer
#
THIS=$(basename "$0")
TDIR=$(readlink -f $(dirname $0))
PYPATH=$(echo -e "import sys\nprint(str(sys.path).replace(' ','').replace('\"','').replace(\"'\",\"\").replace(',',':')[1:-1])"|python)
for d in $TDIR $TDIR/.. $TDIR/../.. $HOME/dev $HOME/tools ${PYPATH//:/ } /etc; do
  if [ -e $d/z0librc ]; then
    . $d/z0librc
    Z0LIBDIR=$d
    Z0LIBDIR=$(readlink -e $Z0LIBDIR)
    break
  elif [ -d $d/z0lib ] && [ -e $d/z0lib/z0librc ]; then
    . $d/z0lib/z0librc
    Z0LIBDIR=$d/z0lib
    Z0LIBDIR=$(readlink -e $Z0LIBDIR)
    break
  fi
done
if [ -z "$Z0LIBDIR" ]; then
  echo "Library file z0librc not found!"
  exit 2
fi

__version__=0.2.8


##############################################################################
#
#    Copyright (C) SHS-AV s.r.l. (<http://ww.zeroincombenze.it>)
#    All Rights Reserved
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either versiofn 3 of the
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

OPTOPTS=(h        i         n            t        V           v)
OPTDEST=(opt_help opt_xtl   opt_dry_run  opt_test opt_version opt_verbose)
OPTACTI=(1        "*>"      "1>"         "*>"     "*>"        1)
OPTDEFL=(1        ""        0            ""       ""          0)
OPTMETA=("help"   "install" "do nothing" "test"   "version"   "verbose")
OPTHELP=("this help"\
 "install z0librc library in (copy in /etc)"\
 "do nothing (dry-run)"\
 "execute regression tests"\
 "show version"\
 "verbose mode")
OPTARGS=(src tgt)

parseoptargs "$@"

if [ "$opt_version" ]
then
  echo "$__version__"
  exit 0
fi
if [ $opt_help -gt 0 ]
then
  print_help "z0lib"\
  "(C) 2015-2018 by zeroincombenze(R)\nhttp://wiki.zeroincombenze.org/en/Linux/dev\nAuthor: antoniomaria.vigliotti@gmail.com"
  exit 0
fi

version=$(grep "#__version__" $Z0LIBDIR/z0librc|head -n1|awk -F\= '{print $2}')
if [ "$version" != "$__version__" ]; then
  echo "Warning: command version $__version__ and library version $version differ!"
fi
if [ "$opt_xtl" ]; then
  CWD=$PWD
  cd $Z0LIBDIR
  if [ $opt_dry_run -ne 0 ]; then
    _install_z0librc "-n"
  elif [ $EUID -eq 0 ]; then
    _install_z0librc
    echo "Library z0librc V$__version__ installed"
  else
    echo "This command must be executed by root"
    exit 1
  fi
  cd $CWD
elif [ "opt_test" ]; then
  if [ -d ./tests -a -f ./tests/all_tests ]; then
    ./tests/all_tests
  else
    echo "Test environment not found!"
    exit 1
  fi
fi
exit 0
