#!/bin/perl
# *************************************************************
#  
#  Licensed to the Apache Software Foundation (ASF) under one
#  or more contributor license agreements.  See the NOTICE file
#  distributed with this work for additional information
#  regarding copyright ownership.  The ASF licenses this file
#  to you under the Apache License, Version 2.0 (the
#  "License"); you may not use this file except in compliance
#  with the License.  You may obtain a copy of the License at
#  
#    http://www.apache.org/licenses/LICENSE-2.0
#  
#  Unless required by applicable law or agreed to in writing,
#  software distributed under the License is distributed on an
#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
#  KIND, either express or implied.  See the License for the
#  specific language governing permissions and limitations
#  under the License.
#  
# *************************************************************
#
# spose - start pose
#

use Getopt::Std;

# Location of needed files
#
$pose2_exe = $ENV{'POSE2_EXE'};
$pose3_exe = $ENV{'POSE3_EXE'};
$pose_prc = $ENV{'POSE_PRC'};


if (getopts('23qmwo:r:d:v') != 1)
{
   &usage();
}

$apps_load = "";

if ($opt_q)
{
   &add_app("$pose_prc/Quickword.PRC");
}
if ($opt_m)
{
   &add_app("$pose_prc/MiniCalc.prc");
}
if ($opt_w)
{
   &add_app("$pose_prc/WordSmith.PRC");
}
if ($opt_o)
{
   &add_app("$opt_o");
}
if ($opt_r)
{
   $run_prog .= "-run_app $opt_r";
}
if ($opt_d)
{
   $directory = $opt_d;
   @files = `/bin/ls -1 $directory/*.pdb`;

   for ($i=0; $i <= $#files; $i++)
   {
      $add_file = "$files[$i]";
      chomp $add_file;
      &add_app("$add_file");
   }
}

if ($opt_3)
{
   $pose_exe = $pose3_exe;
}
else
{
   $pose_exe = $pose2_exe;
}
if ($pose_exe eq "")
{
    print "\nPose not found: Please set \n       POSE2_EXE\n    or POSE3_EXE\n"; 
    exit 0;
}
if ($opt_v)
{
   print ("\n$pose_exe $apps_load $run_prog &\n\n");
}
else
{
   system ("$pose_exe $apps_load $run_prog &");
}

exit 0;

sub usage
{
   print "\nUsage: getopt [ -m ] [ -q ] [ -w ] [ -o <PrcFile> ] [ -r <RunProg> ]\n";
   print " -2            Runs pose version 3.2 [ current default ]\n";
   print " -3            Runs pose version 3.3\n";
   print " -d            Load all PDB files in specified directory\n";
   print " -m            Load MiniCalc PRC file\n";
   print " -q            Load QuickWord PRC file\n";
   print " -w            Load WordSmith PRC file\n";
   print " -o <PrcFile>  Other PRC files to load\n";
   print " -r <RunProg>  Program to run on startup\n";
   print " -v            Display the command instead of running\n\n";
   exit(-1);
}

sub add_app
{
   my $new_app = $_[0];

   if ($apps_load ne "")
   {
      $apps_load .= ",";
   }
   else
   {
      $apps_load = "-load_apps ";
   }

   $apps_load .= "$new_app";
}