#!/bin/bash

################################################################################
#      This file is part of OpenELEC - http://www.openelec.tv
#      Copyright (C) 2009-2012 Stephan Raue (stephan@openelec.tv)
#      Copyright (C) 2010-2011 Roman Weber (roman@openelec.tv)
#
#  This Program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#
#  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 General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with OpenELEC.tv; see the file COPYING.  If not, write to
#  the Free Software Foundation, 51 Franklin Street, Suite 500, Boston, MA 02110, USA.
#  http://www.gnu.org/copyleft/gpl.html
################################################################################

. config/options $1

STAMP=$STAMPS_INSTALL/$1/install

if [ -z "$1" ]; then
  echo "usage: $0 package_name"
  exit 1
fi

if [ -z "$INSTALL" ] ; then
  echo "error: '\$INSTALL' not set! this script is not intended to be run manually"
  exit 1
fi
mkdir -p $STAMPS_INSTALL/$1

if [ -f $STAMP -a $PKG_DIR/install -nt $STAMP ]; then
  rm -f $STAMP
fi

[ -f $STAMP -a $FORCE_INSTALL = "no" ] && exit 0

if [ -n "$PKG_ARCH" -a ! "$PKG_ARCH" = "any" ]; then
  echo "$PKG_ARCH" | grep -q "$TARGET_ARCH" || exit 0
  echo "$PKG_ARCH" | grep -q "\-$TARGET_ARCH" && exit 0
fi

$SCRIPTS/build $@

printf  "%${BUILD_INDENT}c INSTALL  $1\n" ' '>&$SILENT_OUT
export BUILD_INDENT=$((${BUILD_INDENT:-1}+$BUILD_INDENT_SIZE))

for p in $PKG_DEPENDS $PKG_DEPENDS_TARGET; do
  $SCRIPTS/install $p
done

if [ -f $PKG_DIR/package.mk ]; then
  # unset functions
    unset -f pre_install
    unset -f post_install

  # include buildfile
    . $PKG_DIR/package.mk

  # set PKG_BUILD variable
    if [ -z "$PKG_BUILD" ]; then
      if [ -d "$BUILD/${PKG_NAME}-${PKG_VERSION}" ]; then
        PKG_BUILD="$BUILD/${PKG_NAME}-${PKG_VERSION}"
      elif [ -d "$BUILD/${PKG_NAME}_${PKG_VERSION}" ]; then
        PKG_BUILD="$BUILD/${PKG_NAME}_${PKG_VERSION}"
      elif [ -d "$BUILD/${PKG_NAME}.${PKG_VERSION}" ]; then
        PKG_BUILD="$BUILD/${PKG_NAME}.${PKG_VERSION}"
      elif [ -d "$BUILD/${PKG_NAME}${PKG_VERSION}" ]; then
        PKG_BUILD="$BUILD/${PKG_NAME}${PKG_VERSION}"
      elif [ -d "$BUILD/${PKG_NAME}" ]; then
        PKG_BUILD="$BUILD/${PKG_NAME}"
      fi
    fi

  # install
    if [ "$(type -t pre_install)" = "function" ]; then
      pre_install
    fi
    if [ -d $PKG_BUILD/.install_pkg ]; then
      mkdir -p $INSTALL
      cp -PR $PKG_BUILD/.install_pkg/* $INSTALL
    fi
    if [ "$(type -t post_install)" = "function" ]; then
      post_install
    fi
elif [ -f $PKG_DIR/install ]; then
  if [ -d "$BUILD/${PKG_NAME}-${PKG_VERSION}" ]; then
    PKG_BUILD="$BUILD/${PKG_NAME}-${PKG_VERSION}"
  elif [ -d "$BUILD/${PKG_NAME}_${PKG_VERSION}" ]; then
    PKG_BUILD="$BUILD/${PKG_NAME}_${PKG_VERSION}"
  elif [ -d "$BUILD/${PKG_NAME}.${PKG_VERSION}" ]; then
    PKG_BUILD="$BUILD/${PKG_NAME}.${PKG_VERSION}"
  elif [ -d "$BUILD/${PKG_NAME}${PKG_VERSION}" ]; then
    PKG_BUILD="$BUILD/${PKG_NAME}${PKG_VERSION}"
  elif [ -d "$BUILD/${PKG_NAME}" ]; then
    PKG_BUILD="$BUILD/${PKG_NAME}"
  fi
  $PKG_DIR/install $@ >&$VERBOSE_OUT
fi

if [ -d $PKG_DIR/init.d ]; then
  mkdir -p $INSTALL/etc/init.d
  cp $PKG_DIR/init.d/* $INSTALL/etc/init.d/
fi

if [ -d $PKG_DIR/profile.d ]; then
  mkdir -p $INSTALL/etc/profile.d
  cp $PKG_DIR/profile.d/*.conf $INSTALL/etc/profile.d/
fi

if [ -d $PKG_DIR/tmpfiles.d ]; then
  mkdir -p $INSTALL/usr/lib/tmpfiles.d
  cp $PKG_DIR/tmpfiles.d/*.conf $INSTALL/usr/lib/tmpfiles.d
fi

if [ -d $PKG_DIR/udev.d ]; then
  mkdir -p $INSTALL/lib/udev/rules.d
  cp $PKG_DIR/udev.d/*.rules $INSTALL/lib/udev/rules.d
fi

if [ -d $PKG_DIR/init.network ]; then
  mkdir -p $INSTALL/etc/init.d/network
  cp $PKG_DIR/init.network/* $INSTALL/etc/init.d/network/
fi

for i in `sed -n "s/^\([^#].*\)=\".*$/\1/p" $PROJECT_DIR/$PROJECT/options | grep -v "#"`; do
  eval val=\$$i
  echo "STAMP_$i=\"$val"\" >> $STAMP
done
