#!/bin/sh
set -e

test -x /usr/bin/starplot || exit 0

if [ -z "$2" ] ; then
    exit 0
fi
 

###################################
## starplot install|remove stardata
##
###################################

case $1 in
    install)
     # update any starplot data files
     SP_DIR=/var/lib/starplot
     STD_DIR=/usr/share/stardata
     SPEC_DIR=/usr/share/starplot/specfiles
          
     if [ "$2" = "gliese" ] || [ "$2" = "yale" ] ; then
         [ ! -d "$SP_DIR" ] && mkdir $SP_DIR || true
         echo "Updating $2 starplot data sets... (in $SP_DIR)."
	 if [ -d $STD_DIR ] ; then
	     starconvert $SPEC_DIR/$2.spec $STD_DIR/$2/catalog.dat $SP_DIR/$2.stars > /dev/null 2>&1
	 fi
	 
	 if [ -f "$SP_DIR/$2.stars" ] ; then
	     linkfile=/usr/share/starplot/$2.stars
	     if [ -f "$linkfile" ] || [ -L "$linkfile" ] ; then
	        rm -f $linkfile
	     fi
	     ln -s $SP_DIR/$2.stars $linkfile
	 fi    
     fi
     
    ;;

    remove)
    if [ "$2" = "gliese" ] ; then
	echo "Removing StarPlot Gliese catalogue."
	find /var/lib/starplot/ -name "gliese.stars" -type f -print0 | xargs -0 rm -f
	find /usr/share/starplot/ -name "gliese.stars" -type l -print0 | xargs -0 rm -f
	find /usr/share/starplot/ -name "gliese.stars" -type f -print0 | xargs -0 rm -f
    fi

    if [ "$2" = "yale" ] ; then
	echo "Removing StarPlot Yale catalogue."
	find /var/lib/starplot/ -name "yale.stars" -type f -print0 | xargs -0 rm -f
	find /usr/share/starplot/ -name "yale.stars" -type l -print0 | xargs -0 rm -f
	find /usr/share/starplot/ -name "yale.stars" -type f -print0 | xargs -0 rm -f
    fi
    
    
    ;;

    *)
       exit 0
    ;;
esac

exit 0
