#! /bin/sh
#
# emxinstall 0.5a -- Copyright (c) 2001 Eberhard Mattes
#
# This file is part of epocemx.
#
# epocemx 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.
#
# epocemx 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 epocemx; see the file COPYING.  If not, write to the
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
#

usage()
{
  echo "Usage:"
  echo "  emxinstall -copy FILE... TARGETDIR"
  echo "  emxinstall -mkdir TARGETDIR"
  exit 1
}

die()
{
  echo "$@"
  exit 1
}

epocroot()
{
  echo "Please set the environment variable EMXINSTALL_ROOT to contain"
  echo "the name of the target directory."
  echo ""
  echo "If the value of EMXINSTALL_ROOT starts with a letter followed by"
  echo "a colon, Mtools will be used or copying.  Otherwise, the value of"
  echo "EMXINSTALL_ROOT must start with a slash.  In both cases, the"
  echo "value of EMXINSTALL_ROOT must not end with a slash.  Examples:"
  echo ""
  echo "  EMXINSTALL_ROOT=f:"
  echo "  EMXINSTALL_ROOT=/mnt"
  echo "  EMXINSTALL_ROOT=/mnt/psion/D:"
  echo "  EMXINSTALL_ROOT=/tmp/epoc"
  echo "  export EMXINSTALL_ROOT"
  echo ""
  echo "To make emxinstall only show the commands instead of executing them,"
  echo "set"
  echo ""
  echo "  EMXINSTALL_CMD=echo"
  echo "  export EMXINSTALL_CMD"
  exit 1
}

checkopt()
{
  for arg
  do
    case $arg in
	-*) die "Unrecognized switch: $arg";;
	*)  ;;
    esac
  done
}

checktargetdir()
{
  test "$1" = "" && die "TARGETDIR must not be empty"
  case $1 in
    /*) die "TARGETDIR must not start with a slash";;
    */) die "TARGETDIR must not end with a slash";;
    *)  ;;
  esac
}

case $EMXINSTALL_ROOT in
  */)	epocroot
	;;
  [a-zA-Z]:*)
	mkdir="mmd"
	cp=mcopy
	rm=mdel
	isdir=true
	exdir=false
	;;
  /*)
	mkdir=mkdir
	cp=cp
	rm=
	isdir="test -d"
	exdir="test -d"
	;;
  *)	epocroot
	;;
esac

cmd=$EMXINSTALL_CMD

$isdir "$EMXINSTALL_ROOT" || \
	die "The directory pointed to by EMXINSTALL_ROOT does not exist: $EMXINSTALL_ROOT"

case "$1" in
  -mkdir)
	shift
	test $# -ge 1 || usage
	checkopt "$@"
	for dir
	do
	  checktargetdir "$dir"
	done
	r=0
	for dir
	do
	  $exdir "$EMXINSTALL_ROOT/$dir" || $cmd $mkdir "$EMXINSTALL_ROOT/$dir" || r=1
	done
	exit $r
	;;

  -copy)
	shift
	test $# -ge 2 || usage
	checkopt "$@"
	target="${!#}"
	checktargetdir "$target"
	$isdir "$EMXINSTALL_ROOT/$target" \
		|| die "Target directory does not exist: $EMXINSTALL_ROOT/$target"
	c=
	for file
	do
	  test "$file" = "$target" && c=x$c
	done
	test "$c" = "x" || die "Sorry, no FILE can equal TARGETDIR"
	for file
	do
	  test "$file" = "$target" && break
	  test -f "$file" || die "File does not exist: $file"
	done
	if test "$rm" != ""
	then
	  echo "Deleting target files"
	  for file
	  do
	    test "$file" = "$target" && break
	    b="`basename $file`"
	    $cmd $rm "$EMXINSTALL_ROOT/$target/$b"
	  done
	fi
	echo "Copying files"
	for file
	do
	  test "$file" = "$target" && break
	  b="`basename $file`"
	  $cmd $cp $file "$EMXINSTALL_ROOT/$target/$b" \
		|| die "Command failed: $cp $file $EMXINSTALL_ROOT/$target/$b"
	done
	;;
  *)
	usage
	;;

esac
