#!/usr/bin/env python
# b3dtomosetexts - Utility to output results of findRootAxisAndExtensions on a dataset
#
# Author: David Mastronarde
#
# $Id: b3dtomosetexts,v 937343107256 2023/02/19 22:44:16 mast $

progname = 'b3dtomosetexts'
prefix = 'ERROR: ' + progname + ' - '

# load System Libraries
import sys, os, os.path

#
# Setup runtime environment (bare minimum)
if os.getenv('IMOD_DIR') != None:
   IMOD_DIR = os.environ['IMOD_DIR']
   if sys.platform == 'cygwin' and sys.version_info[0] > 2:
      IMOD_DIR = IMOD_DIR.replace('\\', '/')
      if IMOD_DIR[1] == ':' and IMOD_DIR[2] == '/':
         IMOD_DIR = '/cygdrive/' + IMOD_DIR[0].lower() + IMOD_DIR[2:]
   sys.path.insert(0, os.path.join(IMOD_DIR, 'pylib'))
else:
   sys.stdout.write(prefix + " IMOD_DIR is not defined!\n")
   sys.exit(1)

#
# load IMOD Libraries
from imodpy import *

if len(sys.argv) > 1 and sys.argv[1].startswith('-h'):
   prnstr("""Usage: b3dtomosetexts [directory]
  Examines data set files in the current directory or the optionally specified one.
  Outputs all on one line ("none" is output if a string is undetermined):
    type extension (naming style): old, mrc, hdf, or none
    raw stack extension or none
    root name of dataset or none
    2 for dual axis, 0 for single axis, or -1 if not determined
    com file extension: com, pcm, or none""")
   sys.exit(0)

if len(sys.argv) > 1:
   try:
      os.chdir(sys.argv[1])
   except Exception:
      prnstr('Error changing directory to ' + sys.argv[1])
      sys.exit(1)

(comExt, dualNum, root, typeExt, stackExt) = findRootAxisAndExtensions()
if not comExt:
   comExt = 'none'
if typeExt == None:
   typeExt = 'none'
if typeExt == '':
   typeExt = 'old'
if not stackExt:
   stackExt = 'none'
if not root:
   root = 'none'
prnstr(fmtstr('{} {} {} {} {}', typeExt, stackExt, root, dualNum, comExt))
sys.exit(0)
