#!/bin/sh
#@+leo-ver=4-thin
#@+node:EKR.20040519082027.33:@file-thin ../install
#@@first

############################################
# This is a small install script to install
# leo on GNU/Linux. 
# Leo is Open Software and is distributed under the terms 
# of the Python License.  Please see:
# docs/LICENSE.TXT for details.
#
# This install script created by Frank Merenda 
# (fmerenda@yahoo.com)
#
# 06/30/2003: initial creation
# 06/28/2004: Changes made as suggested by Scott Beardsley (sbeards)
#
# 
# - minor changes to try and use a "prefix" directory
# - step 5 copies the src and config directories as 
# directories rather that file (this seems to prevent
# the need to remove ".." from the python scripts on RH9
# anyway.)
# - IMPORTANT NOTE: I did not understand step 6 so its
# commented out. Someone who knows leo better than I do
# should look at this.
# - Note: I'm a leo newbie so I don't know if this fixes plugin
# problems since I don't think I use them.
# David Holden (dh@iucr.org)
#   
#
############################################


################################
# Variables for usage in this script.
################################
LEO_PREFIX="/usr/local"
LEO_RUN_SCRIPT_SOURCE='./scripts/install/leo-script.txt'
LEO_RUN_SCRIPT_OUTPUT="${LEO_PREFIX}/bin/leo"
LEO_LIB_DIR="${LEO_PREFIX}/lib/leo"


echo ""
echo "Prefix directory set to \"$LEO_PREFIX\""

################################
# Step 1
# Verify the current user is root
################################
CURRENT_USER=$(whoami)

if [ "$CURRENT_USER" = "root" ] ; then
	echo ""
    echo Verified user is root
else
	echo ""
    echo You must be root to run this program
    exit 0;
fi    
   
################################
# Step 2
# Make the directory structure
# 06/28/2004: Rewritten by Scott Beardsley
################################
if [ ! -d $LEO_LIB_DIR ]; then
    echo ""
    echo Making directory structure $LEO_LIB_DIR

    RESULTS=`mkdir -p -m755 $LEO_LIB_DIR 2>&1`
    if [ $? -ne 0 ]; then
        echo Failure making directory $LEO_LIB_DIR!
        exit 0;
    fi
fi

if [ ! -d $LEO_PREFIX/bin ]; then
    echo ""
    echo Making directory structure $LEO_PREFIX/bin
    RESULTS=`mkdir -p -m755 $LEO_PREFIX/bin 2>&1`
    if [ $? -ne 0 ]; then
        echo Failure making directory $LEO_PREFIX/bin!
        exit 0;
    fi
fi
   
################################
# Step 3
# Copy the files
################################
echo ""
echo Copying files	.

RESULTS=`cp -rp * $LEO_LIB_DIR/ 2>&1`
if [ $? -ne 0 ]; then
    echo Failure copying files to $LEO_LIB_DIR
    exit 0;
fi

###############################
# Step 4
# Clean up the duplicate files so it's not
# confusing to the user. These files
# Are copied directly to the main 
# directory. Assume you have permissions
# since you checked above.
###############################
rm -rf $LEO_LIB_DIR/src
rm -rf $LEO_LIB_DIR/config
   
###############################
# Step 5
# Don't make a liar out of me in step 4.
# Copy the files. that you just deleted	   
###############################
cp -rp src $LEO_LIB_DIR/.
cp -rp config $LEO_LIB_DIR/.

###############################
# Step 6
# Add in symbolic links to point to 
# leoConfig and LeoDocs.   
###############################
#mkdir -p /usr/lib/config 
#chmod 777 /usr/lib/config
#ln -sf /$LEO_LIB_DIR/leoConfig.leo /usr/lib/config/leoConfig.leo
#chmod 666 /$LEO_LIB_DIR/leoConfig.leo  
#
#ln -sf /$LEO_LIB_DIR/leoConfig.txt /usr/lib/config/leoConfig.txt
#chmod 666 /$LEO_LIB_DIR/leoConfig.txt  
#
#mkdir -p /usr/lib/doc
#ln -sf $LEO_LIB_DIR/doc/LeoDocs.leo /usr/lib/doc/LeoDocs.leo
#chmod 666 $LEO_LIB_DIR/doc/LeoDocs.leo 
   
################################
# Step 7
# Create the startup script
################################
echo ""
echo Creating startup script located at: $LEO_RUN_SCRIPT_OUTPUT

# 06/28/2004: Fix by Scott Beardsley
# echo "python ${LEO_PREFIX}/lib/leo/src/leo.py \$1" > $LEO_RUN_SCRIPT_OUTPUT
echo "python ${LEO_PREFIX}/lib/leo/src/leo.py \"\$1\"" > $LEO_RUN_SCRIPT_OUTPUT

RESULTS=`chmod 755 $LEO_RUN_SCRIPT_OUTPUT`   
   
echo ""
echo "***** Leo installed successfully - "
echo "      make sure ${LEO_PREFIX}/bin is in your path then"
echo "      type 'leo filename.leo' to use it."   
echo "                                                 *****"
echo ""
exit 0;
#@nonl
#@-node:EKR.20040519082027.33:@file-thin ../install
#@-leo
