#!/bin/sh
# $Id: instfiles,v 6.3 2001/06/23 08:02:49 cher Exp $

# Copyright (C) 1997-2001 Alexander Chernov <cher@ispras.ru>

#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA

# synopsys:
#   instfiles [options] target-dir files...
#   options are:
#     -m mode    - set the specified permissions on installed files
#     -g group   - set the specified group as owner of the inst. files
#     -u user    - set the specified user as owner of the inst. files
#     -S         - strip symbol table from installed files
#                - (this options can be used for binaries only)
#     -r         - remove destination file if already exists
#     -1         - copy one source file to one destination file

SETMODE=
SETUSER=
SETGROUP=
DOSTRIP=0
DORM=0
ONEFILE=0

while true
do
  case $1 in
    -1) ONEFILE=1; shift;;
    -r) DORM=1; shift 1;;
    -m) SETMODE=${2:?}; shift 2;;
    -g) SETGROUP=${2:?}; shift 2;;
    -u) SETUSER=${2:?}; shift 2;;
    -S) DOSTRIP=1; shift 1;;
    *) break;;
  esac
done

target=${1:?}
shift

if [ x"$ONEFILE" = x1 ]
then
  #extract target dir
  args=$1
  distname=`basename $target`
  target=`dirname $target`
else
  args=$*
  distname=
fi

p1=$target
p2=

while true
do
  if [ -r $p1 -a ! -d $p1 ]
  then
    echo $p1 " is not a directory" >&2
    exit 1
  fi

  if [ -d $p1 ]
  then
    break
  fi

  p2=$p2/`basename $p1`
  p1=`dirname $p1`
done

while true
do
  if [ "$p2" = "" -o "$p2" = "/" ]
  then
    break
  fi
  p1=$p1/`basename $p2`
  p2=`dirname $p2`

  if [ -d $p1 ]
  then
    :
  else
    mkdir $p1
    rc=$?
    if [ $rc != 0 ]
    then
      exit $rc
    fi
  fi

  if [ "$SETMODE" != "" ]
  then
    chmod $SETMODE $p1
    rc=$?
    if [ $rc != 0 ]
    then
      exit $rc
    fi
    chmod a+x $p1
    rc=$?
    if [ $rc != 0 ]
    then
      exit $rc
    fi
  fi

  if [ "$SETGROUP" != "" ]
  then
    chgrp $SETGROUP $p1
    rc=$?
    if [ $rc != 0 ]
    then
      exit $rc
    fi
  fi

  if [ "$SETUSER" != "" ]
  then
    chown $SETUSER $p1
    rc=$?
    if [ $rc != 0 ]
    then
      exit $rc
    fi
  fi
done

for i in $args
do
  bb=$distname
  [ x"$bb" != x ] || bb=`basename $i`

  # do not copy if the file is not changed
  if [ -r $target/$bb ] && cmp $i $target/$bb >/dev/null 2>&1
  then
    #echo "instfiles: file $i is not changed, skipping"
    :
  else
    [ "$DORM" = 1 ] \
      && mv -f ${target}/${bb} ${target}/${bb}.old 2>/dev/null \
      && rm -f ${target}/${bb}.old 2>/dev/null
    cp $i $target/$bb
    [ "$DOSTRIP" = 1 ]    && strip ${target}/$bb
    [ "$SETMODE" != "" ]  && chmod $SETMODE ${target}/$bb
    [ "$SETGROUP" != "" ] && chgrp $SETGROUP $target/$bb
    [ "$SETUSER" != "" ]  && chown $SETUSER $target/$bb
  fi
done

exit 0
