#!/bin/sh
#
# Purpose:  Copy IDL runtime distribution to staging area
#           for a project.  Also create SAVE file startup
#           script for runtime or embedded mode.
#

# Check number of arguments.
if [ "$#" -lt 4  -o  "$#" -gt 5 ]
then
    echo "  "
    echo "Usage: $0 [source] destination manifest savefile mode"
    echo "  "
    echo "  source      - Full path to IDL source directory that contains the files"
    echo "                to copy.  If not specified, user is prompted."
    echo "  "    
    echo "  destination - Full path to destination directory to contain the copied files."
    echo "  "    
    echo "  manifest    - Full path to file containing the list of files to copy,"
    echo "                each on its own line, specified as a relative path"
    echo "                (relative to the source and destination directories)."
    echo "  "    
    echo "  savefile    - Name of IDL SAVE file (without directory or extension)."
    echo "  "    
    echo "  mode        - IDL mode: 'rt' = runtime, 'em' = embedded."
    echo "  "    
    exit 1
fi

# Set the source directory.
if [ "$#" -eq 4 ]
then
    echo "Enter the IDL source directory: "
    read source
else
    source=$1
    shift
fi

# Check the source directory for a valid file.
if [ ! -f $source/bin/idl ]
then
    echo "$0 - Incorrect IDL source directory '$source'."
    exit 1
fi

# Set the destination directory.
destination=$1
shift

# Check the destination directory.
if [ ! -d $destination ]
then
    echo "$0 - Destination directory '$destination' does not exist."
    exit 1
fi

# Set the manifest file.
manifest=$1
shift

# Check the manifest file.
if [ ! -f $manifest ]
then
    echo "$0 - Manifest file '$manifest' does not exist."
    exit 1
fi

# Set the SAVE file name.
savefile=$1
shift

# Set the mode.
mode=$1
if [ "$mode" != "rt"  -a  "$mode" != "em" ]
then
    echo "$0 - Incorrect mode '$mode' ('rt' or 'em')."
    exit 1
fi

# Specify the output script name.
script=$destination/$savefile

# Figure out which platform we're on.
platform=`uname`

# Set up tar options.
#  Set processor type for SunOS.
case "$platform"
in
    HP-UX )         tar_options=chf;;
    AIX )           tar_options=chf;;
    Linux )         tar_options=cpf;;
    OSF1 )          tar_options=chf;;
    IRIX | IRIX64 ) tar_options=cLf;;
    SunOS )         tar_options=chf;;
    Darwin )        tar_options=cLf;;
    * ) echo "$0 - Unsupported platform '$platform'.";;
esac

# Go to the source directory.
cd $source

# Get the number of lines in manifest file.
#   Use redirection to avoid grabbing file name.
#   Use sed to remove undesired spaces.
total=`wc -l < $manifest | sed 's/ //g'`
count=$total

#-----------------------------
# Loop to process an increment's worth of lines (file names) at
# a time.  Do this to avoid tar limitation (different per platform)
# of "The parameter list is too long".
increment=100
first=1

while [ "$count" -gt 0 ]
do

    # Set the last line to copy.
    #   Use sed to remove undesired spaces.
    last=`expr $first + $increment - 1 | sed 's/ //g'`
    if [ "$last" -gt "$total" ]
    then
        last=$total
    fi

    # Grab the next increment's worth of file names.
    #   Exclude names from foreign platforms.
    case "$platform"
    in
            HP-UX ) list=`sed -n "${first},${last}p" $manifest | \
                    fgrep -v 'bin.darwin'|\
                    fgrep -v 'bin.ibm'|\
                    fgrep -v 'bin.linux'|\
                    fgrep -v 'bin.alpha'|\
                    fgrep -v 'bin.sgi'|\
                    fgrep -v 'bin.solaris2.sparc'`;;
            AIX ) list=`sed -n "${first},${last}p" $manifest | \
                    fgrep -v 'bin.darwin'|\
                    fgrep -v 'bin.hp'|\
                    fgrep -v 'bin.linux'|\
                    fgrep -v 'bin.alpha'|\
                    fgrep -v 'bin.sgi'|\
                    fgrep -v 'bin.solaris2.sparc'`;;
            Linux ) list=`sed -n "${first},${last}p" $manifest | \
                    fgrep -v 'bin.darwin'|\
                    fgrep -v 'bin.hp'|\
                    fgrep -v 'bin.ibm'|\
                    fgrep -v 'bin.alpha'|\
                    fgrep -v 'bin.sgi'|\
                    fgrep -v 'bin.solaris2.sparc'`;;
            OSF1 ) list=`sed -n "${first},${last}p" $manifest | \
                    fgrep -v 'bin.darwin'|\
                    fgrep -v 'bin.hp'|\
                    fgrep -v 'bin.ibm'|\
                    fgrep -v 'bin.linux'|\
                    fgrep -v 'bin.sgi'|\
                    fgrep -v 'bin.solaris2.sparc'`;;
            IRIX | IRIX64 ) list=`sed -n "${first},${last}p" $manifest | \
                    fgrep -v 'bin.darwin'|\
                    fgrep -v 'bin.hp'|\
                    fgrep -v 'bin.ibm'|\
                    fgrep -v 'bin.linux'|\
                    fgrep -v 'bin.alpha'|\
                    fgrep -v 'bin.solaris2.sparc'`;;
            SunOS ) list=`sed -n "${first},${last}p" $manifest | \
                    fgrep -v 'bin.darwin'|\
                    fgrep -v 'bin.hp'|\
                    fgrep -v 'bin.ibm'|\
                    fgrep -v 'bin.linux'|\
                    fgrep -v 'bin.alpha'|\
                    fgrep -v 'bin.sgi'`;;
            Darwin ) list=`sed -n "${first},${last}p" $manifest | \
                    fgrep -v 'bin.hp'|\
                    fgrep -v 'bin.ibm'|\
                    fgrep -v 'bin.linux'|\
                    fgrep -v 'bin.alpha'|\
                    fgrep -v 'bin.sgi'|\
                    fgrep -v 'bin.solaris2.sparc'`;;
            * ) echo "$0 - Unsupported platform '$platform'.";;
    esac

    # Process list if it's not empty.
    if [ "$list" != "" ]
    then
        tar $tar_options - `echo $list` | (cd $destination; tar xf -)
    fi

    # Set parameters for next time through loop.
    #   Use sed to remove undesired spaces.
    first=`expr $last + 1 | sed 's/ //g'`
    count=`expr $count - $increment`

done

#-----------------------------

# Create the runtime script.
if [ "$mode" = "rt" ]
then
    cat > $script << END
#!/bin/sh
# Run the IDL SAVE file in runtime mode.
./bin/idl -rt=$savefile.sav
END
    chmod a+x $script
fi

# Create the embedded script.
if [ "$mode" = "em" ]
then
    cat > $script << END
#!/bin/sh
# Run the IDL SAVE file in embedded mode.
./bin/idl -em=$savefile.sav
END
    chmod a+x $script
fi

#-----------------------------------------------------------------------


