#!/bin/sh

# StarPlot - A program for interactively viewing 3D maps of stellar positions.
# Copyright (C) 2000-2008  Kevin B. McCarty
#
# 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
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.


# This shell script makes it easier to install and update StarPlot data sets
# using starconvert.

printusage() {
    cat << EOF
starpkg usage:

starpkg [ --dataset ] <data-set> [ --dest <destination> ]
	Assuming that \`data-set' contains a valid StarPlot data set (either a
	directory or a tar.gz file), this command converts it to a
	StarPlot-formatted data file.  If the \`--dest' option is omitted, the
	default location of the output file is the first of the directories
	$DEFAULTDIR, \$PWD, \$HOME in which the user has write
	permissions.  If \`--dest' is omitted or \`destination' is a directory,
	the default name of the output file is \${specfilename%.spec}.stars
	The \`--dataset' flag may be omitted.

starpkg --recurse <dir> [ --dest <dest-dir> ]
	Executes the above action for every file and directory in \`dir',
	putting the resulting .stars files into \`dest-dir'.  If the \`--dest'
	option is omitted, the default destination directory is chosen
	as above.

--quiet / --verbose: Print only errors or all starconvert output, respectively.
--starconvert-option <option>: Pass <option> through to starconvert.

starpkg: version 0.95.5 (C) 2000-2008 Kevin B. McCarty
starpkg comes with ABSOLUTELY NO WARRANTY; for details, see the file
COPYING that should have come with your distribution of StarPlot.
EOF
}

input=""
dest=""
action=dataset
verbosity=1
spoptions=""
scoptions=""
prefix="/usr/pkg"
datarootdir="${prefix}/share"
DEFAULTDIR="${datarootdir}/starplot"

starpkg="$0"
case "$starpkg" in
	*/*)	starconvert="$(dirname "$starpkg")/starconvert" ;;
	*)	starconvert="starconvert" ;;
esac

# select a default destination in case user doesn't specify one
[ -n "$HOME" ] && [ -w "$HOME" ] && dest="$HOME"
[ -n "$PWD" ] && [ -w "$PWD" ]   && dest="$PWD"
[ -w "$DEFAULTDIR" ] && [ -d "$DEFAULTDIR" ] && dest="$DEFAULTDIR"

# read command-line opts
[ $# -eq 0 ] && printusage && exit 0
while [ $# -gt 0 ] ; do
	case "$1" in
		--dataset|-s)
			shift; input="$1" ;;
		--recurse|-r)
			action=update
			shift; input="$1" ;;
		--dest|-d)
			shift; dest="$1" ;;
		--quiet|-q)
			verbosity=0 ; spoptions="$spoptions $1" ;;
		--verbose|-v)
			verbosity=2 ; spoptions="$spoptions $1" ;;
		--starconvert-option)
			shift; scoptions="$scoptions \"$1\"" ;
			       spoptions="$spoptions --starconvert-option \"$1\"";;
		-*)
			printusage; exit 0 ;;
		*)
			input="$1" ;;
	esac ;
	shift
done

# sanity checks on input
[ -z "$input" ] && printusage && exit 0
if [ ! -e "$input" ] ; then
	echo "*** Requested input file or directory does not exist."
	exit 1
fi
if [ ! -r "$input" ] ; then
	echo "*** You do not have read permissions to requested input file."
	exit 1
fi

# sanity checks on destination
if [ -z "$dest" ] ; then
	echo "*** No destination specified, and you have no write permissions"
	echo "    to any of $DEFAULTDIR, \$PWD, \$HOME!"
	exit 1
fi
if [ ! -d "$dest" ] && [ ! -e "`dirname "$dest"`" ] ; then
	echo "*** Requested directory of destination does not exist."
	exit 1
fi
if [ -d "$dest" ] && [ ! -w "$dest" ] || \
   [ ! -d "$dest" ] && [ ! -w "`dirname "$dest"`" ]; then
	echo "*** You do not have write permissions to that destination."
	exit 1
fi

if [ $action = update ] ; then
	for file in $input/* ; do
		[ "$verbosity" -eq 0 ] || echo "Examining $file:"
		eval \""$starpkg"\" --dataset \""$file"\" --dest \""$dest"\" $spoptions
		[ "$verbosity" -eq 0 ] || echo
	done
	exit 0
fi

# if we have gotten this far, then action is for a single data set
if [ -d "$input" ] ; then
	# test for spec file and catalog.dat
	num_specfiles=`find "$input" -name '*.spec' | wc -l`
	if [ $num_specfiles -eq 0 ] ; then
		echo "*** Does not contain starconvert spec file."; exit 1
	elif [ $num_specfiles -gt 1 ] ; then
		echo "*** Contains $num_specfiles starconvert spec files (should only be one)."
		exit 1
	fi
	num_catalogs=`find "$input" -name catalog.dat | wc -l`
	if [ $num_catalogs -eq 0 ] ; then
		echo "*** Does not contain catalog.dat data file."; exit 1
	elif [ $num_catalogs -gt 1 ] ; then
		echo "*** Contains $num_catalogs catalog.dat data files (should only be one)."
		exit 1
	fi

	specname="`find "$input" -name '*.spec'`"
	catalog="`find "$input" -name catalog.dat`"
	dataname="`basename "$specname"`"
	dataname="${dataname%.spec}.stars"
	
	[ -d "$dest" ] && dest="$dest/$dataname"
	[ "$verbosity" -eq 0 ] || echo "Will create file $dest"
	if [ "$verbosity" -ge 2 ] ; then
		eval \""$starconvert"\"$scoptions \""$specname"\" \""$catalog"\" \""$dest"\"
		result=$?
	else
		eval \""$starconvert"\"$scoptions \""$specname"\" \""$catalog"\" \""$dest"\" > /dev/null 2>&1
		result=$?
	fi
	if [ "$result" -ne 0 ] ; then
		echo 1>&2
		echo -n "*** starconvert failed" 1>&2
		if [ "$verbosity" -ge 2 ] ; then
			echo "." 1>&2
		else
			echo "; use the -v flag to starpkg" 1>&2
			echo "*** to see the starconvert error output." 1>&2
		fi
		exit 1
	else
		[ "$verbosity" -eq 0 ] || echo "Done!"
	fi

else # if we get here, we expect $input to be a tar.gz file
	[ -n "$TMPDIR" ] || TMPDIR=/tmp
	if [ ! -d "$TMPDIR" ] ; then
		echo "*** Your temporary directory $TMPDIR is not a directory!"
		exit 1
	elif [ ! -w "$TMPDIR" ] ; then
		echo "*** Your temporary directory $TMPDIR is not writable!"
		exit 1
	fi
	# create a temporary directory in a safe and portable way
	tmpfile=""
	until mkdir "$tmpfile" > /dev/null 2>&1 ; do
		RND="`head -n 10 /dev/urandom | tr -d -c "[:alnum:]" \
			| cut -b 1-6`"
		tmpfile="$TMPDIR/starpkg.$RND"
	done
	# uncompress tarball into the temp dir
	(gunzip -c "$input" > "$tmpfile"/temp.tar) > /dev/null 2>&1
	if [ "$?" -ne 0 ] ; then
		echo "*** File does not seem to be a tar.gz archive."
		rm -rf "$tmpfile"
		exit 1
	fi
	cd "$tmpfile"
	tar -xf temp.tar
	if [ "$?" -ne 0 ] && [ "$verbosity" -gt 0 ] ; then
		echo "*** Error untarring archive; will try to proceed anyway."
	fi
	rm -f temp.tar
	# some shells output the new directory when executing "cd -"
	cd - > /dev/null 2>&1
	input="$tmpfile/`ls -1 "$tmpfile"`"
	eval \""$starpkg"\" --dataset \""$input"\" --dest \""$dest"\" $spoptions
	rm -rf "$tmpfile"
fi

