BIN_DIR='../../bin/';
SHARE_DIR='../../share/sorter/';

BASE='sorter.base';
PROG='sorter';


# Directories to search in
dirs='/usr/local/bin/ /usr/bin/ /usr/ccs/bin/ /bin/ /usr/ucb/bin/ /sbin/ /usr/sbin/ /usr/local/sbin/ /usr/pkg/bin/'

# Find Perl
# Try 5.6.0 first
found=0;
for d in $dirs
  do if (test -x ${d}perl5.6.0) then
    if (test -n "`${d}perl5.6.0 -v 2> /dev/null | awk '/This is perl/ {print $0}'`")
then
      echo "perl found: ${d}perl5.6.0";
      echo "#!${d}perl5.6.0 -w" > $BIN_DIR$PROG;
      found=1;
      break;
    fi;
  fi;
done

# Now try perl
if (test $found -eq 0) then 
  for d in $dirs
    do if (test -x ${d}perl) then
      if (test -n "`${d}perl -v 2> /dev/null | awk '/This is perl/ {print $0}'`")
then
        echo "perl found: ${d}perl";
        echo "#!${d}perl -w" > $BIN_DIR$PROG;
        found=1;
        break;
      fi;
    fi;
  done
fi;

# If it wasn't found, then prompt for it.
if (test $found -eq 0) then
  echo 'ERROR: perl not found';
  while (test 1 -eq 1)
    do echo 'Enter Location:';
    read perlexe;
    if (test -x $perlexe) then
      if (test -n "`$perlexe -v 2> /dev/null | awk '/This is perl/ {print $0}'`"
)       then
    	echo "#!$perlexe -w" > $BIN_DIR$PROG;
        break;
      else
        echo "Perl found, but is not working.  Try another";
      fi;
    else
      echo "file not found";
    fi
  done
fi

echo "my \$SK_DIR=\"`cd ../..; pwd`/\";" >> $BIN_DIR$PROG;

# Print the current version
echo "my \$VER = \"`cat ../../patchlevel`\";" >> $BIN_DIR$PROG;

# Copy in the rest of the program and make it exec
cat $BASE >> $BIN_DIR$PROG;
chmod 0755 $BIN_DIR$PROG;

mkdir -p $SHARE_DIR;

cd sort
for FILE in *.sort 
do
	cp $FILE "../$SHARE_DIR";
done
echo "Config files copied to $SHARE_DIR";
cd ..

