; Copyright (c) 1998-2011 A.P. Hitchcock  All rights reserved
;+
;NAME:
;	AXIS_INI
;
;LAST CHANGED: ----------------------------------- 19-Nov-11 (aph)
;
;PURPOSE:
;	This function reads and writes default values for AXIS.
;
;CATEGORY:
;	AXIS: utility
;
;ROUTINES:
;	 test = AXIS_INI (filename = filname)  - code that writes parameter file
;
;CALLING SEQUENCE:
;	test = AXIS_INI(file=file, out=out, set = set)
;
;CALLED FROM AXIS:
;	Utilities->Set preferences
; and at start up and Quit
;
;INPUTS: none
;
;KEYWORDS:
;	FILE - initialization file name
;	/OUT - write initialization file
;	/SET - interactive setting of default values
;
;OUTPUTS:
;	axis.ini (or user defined file name)
;
;COMMON BLOCKS:
;	@AXIS_COM	standard set of common blocks
;
;PROCEDURE:
; looks up  default values from AXIS.INI or a user-defined file
; if no file, establish defaults
; on exit from AXIS writes out default values to AXIS.INI or a user-specified file
;
;MODIFICATION HISTORY:
; (28-jun-98 aph) first version
; (12-dec-98 aph) introduce font switching for widget (to adapt IDL 4, IDL 5)
; (13-dec-98 aph) add /set option
; (29-dec-98 aph) use AXIS_OPTIONS to establish defaults (IDL 5.2)
; (03-Jan-99 aph) set default WIN font to Arial*14; switch on 5.2 NOT 5
; (11-May-99 aph) establish axis_ini_save subroutine;
;				  allowed 'Printer' to be full command line, not just language
; (24-may-99 aph) extend to include HELP file command
; (16-nov-99 aph) 'loc' -> 'location' to see if this is why it is not preserved
; (02-jan-00 aph) AXIS standard documentation
; (31-jan-00 aph) add  net browser command
; (27-feb-00 aph) add groupID to get_text
; (07-apr-00 aph) modify to force saving to codepath area by passing on file to axis_ini_save
; (30-nov-00 cgz) added statements to ensure that no leading or trailing spaces are
;				included in parameters once read in from *.ini file
; (05-dec-00 cgz) reversed order of AXIS_INI and AXIS_INI_SAVE
; (09-dec-00 cgz) added conversion of float(strtrim(gr_scale,2))
; (23-jan-01 cgz) added dialog to axis_ini_save, also cleaned up logical path
; (31-jan-01 aph) change prompt
; (22-apr-01 aph) fix crash when load using axis.ini from separate directory - drop axis_options
; (07-oct-01 aph) re-activate axis_options
; (28-oct-04 aph) allow for rescue if user pushes 'cancel' on 'exit'
; (08-nov-04 aph) fix problem that axis2000 does NOT use saved last_file / path information
;            problem was LPath not DefPath was saved, but previously it worked; probably was change in axis_ini_save
; (24-jan-05 aph) replace test for read-in which failed in IDL5.2
; (11-Jan-10 aph) removed the  axis_ini_save code to its own pro file (isolated axis_ini.pro)
; (19-Nov-11 aph) automatically save to default axis.ini file location, but ask if cannot find it
;-

FUNCTION AXIS_INI, file=file, out=out, set = set, _extra_e
@axis_com
on_error,2

test = ''
; -------- go directly to SET part ------------
if keyword_set(set) then goto, user_define

; --------- WRITE CURRENT DEFAULT VALUES --------------------
if keyword_set(out) then begin
	 if keyword_set(file) then test = axis_ini_save(filename=file) else test = axis_ini_save()

endif else BEGIN	; ASSUMES ONLY 2 actions - write or read !!

;--------- predefine defaults (this is needed to set data types even if they will be replaced) etc -----------
	if !version.os EQ 'Win32' then begin
	   LPath = ''
	   CodePath = 'c:\rsi\idl52\idl52\axis2d0\'
	   gr_dev = 'WIN'
	   Print_Dev = 'PS'
	   DefPath='c:\axis2000'
	   gr_scale=1.0
	   ColTbl = 0
	   location = 'mac'				; location - to allow switching location specific code
	   Print_file = 'idl.ps'	; default print file name (postscript)
	   BufLblFont = 'Arial*14'  ; default font
	   PR_Command = 'copy idl.win lpt1:'  ; command to transfer file to printer
	   Help_cmd = 'c:\progra~1\acrobat3\reader\acroRd32 c:\axis\axis.pdf'   ; bl7-dow command to spawn to view HELP file
	   Net_command = 'c:\progra~1\netscape\communicator\program\netscape'
	endif else begin
	   LPath = '' 				; ASSUMES alternate system is UNIX @ ALS
	   CodePath = '/home/bl7data/axisdir/'
	   gr_dev = 'X'
	   Print_Dev = 'PS'
	   DefPath='/home/bl7data/stxmdata/after6197/text'
	   gr_scale=1.5
	   ColTbl = 0
	   location = 'ALS'				; location - to allow switching location specific code
	   Print_file = 'idl.win'	; default print file name (postscript)
	   BufLblFont = '6x10'   	; default font
	   PR_Command = 'lpr -S XRAYS-1 -P 6-BL7LJ4 idl.ps' ; command to transfer file to printer
	   Help_cmd = 'acroread c:\axis2000\axis.pdf'   ; command to spawn to view HELP file
	   Net_command = 'c:\progra~1\netscape\communicator\program\netscape'
	endelse

; check user really wants this as source of defaults  -----
	if n_elements(CodePath) EQ 0 then CodePath = 'c:\axis2000\'
	filename = CodePath + 'axis.ini'
	if keyword_set(file) then filename = file
	test = findfile(filename,count=ctest)
	ini_file = filename
	if ctest NE 0 then BEGIN
; ---------- get defaults from AXIS.ini file -----------------
		openr,iunit,filename,/get_lun
	  	on_ioerror, user_define
  		readf, iunit, CodePath
		readf, iunit, Gr_Dev
		readf, iunit, Print_Dev
		readf, iunit, DefPath
		readf, iunit, gr_scale
		readf, iunit, ColTbl
		readf, iunit, location
		readf, iunit, Print_file
		readf, iunit, BufLblFont
		readf, iunit, Pr_Command
		readf, iunit, Help_cmd
		readf, iunit, Net_command
		print, 'Initialization info taken from ', ini_file
		close, iunit
		CodePath = strtrim(CodePath,2)
		Gr_Dev = strtrim(Gr_Dev,2)
		Print_Dev = strtrim(Print_Dev,2)
		DefPath = strtrim(DefPath,2)
		gr_scale = float(strtrim(gr_scale,2))
		ColTbl = strtrim(ColTbl,2)
		location = strtrim(location,2)
		Print_file = strtrim(Print_file,2)
		BufLblFont = strtrim(BufLblFont,2)
		Pr_Command = strtrim(Pr_Command,2)
		Help_cmd = strtrim(Help_cmd,2)
		Net_command = strtrim(Net_command,2)
  	endif else BEGIN
  		user_define:
  		IF strmid(!version.release,0,1) NE '5' then begin
	; ----- for IDL 4.0 -----------
			print, ' '
			print, 'WARNING: AXIS STARTUP file not found ; or selected to be changed '
			print, ' '
			print, ' Define AXIS parameters'
			print, ' '
			print,  '; ********** Initialization file for AXIS Widget *******************'
			print,  '; '
			print,  '; (aph 31-Jan-00)  Please adapt to your hardware '
			print,  '; Items set                    devices currently supported  '
			print,  '; ------------------------   ------------------------------------- '
			print,  '0 CodePath                   (e.g. c:\axis\) - include final separator'
			print,  '1 Screen display device      (WIN, X)  '
			print,  '2 Printer file format       (PCL, PS, or command line entry) '
			print,  '3 Default data directory    any - last path is saved on exit of AXIS  '
			print,  '4 graphics scale factor     (0.5 - 2) (1 = 360x360 pixels)  '
			print,  '5 color table               any IDL supports (0 = B/W; 3 = red temperature)'
			print,  '6 location                  NSLS has special meaning (spooler switch) '
			print,  '7 spooler or print file     (c:\tmp\x1a at nsls - special !!) '
			print,  '8 Font for buffer labels    (windows: Arial*14; unix - 6x10 or 8x13: '
			print,  '9 Printer command line      (command line needed to transfer to printer)'
			print, '10 Command to view Help pdf  (get acrobat reader free from www.adobe.com)'
			print, '11 command to run browser   (WIN: must be executable from MS-DOS window)'
	;----- go get them ! ----
			print,'Choose subdirectory containing AXIS code - any filename is OK'
			CodePath = pickfile2(/read,filter='*.pro')
	    	if !VERSION.OS_FAMILY EQ 'unix' then sep = '/' else sep = '\'
	    	sep_pos = rstrpos(codepath,sep)
	    	CodePath = strmid(codepath,0,sep_pos+1)
			print, ' Select the location and name of the file to save AXIS default paramaters'
	    	ini_file = pickfile2(/write,filter='*.ini')
			Gr_dev = get_text(prompt='Disp (X, WIN)',VAL=Gr_dev, group = axis_ID)
	;		if strpos(tGr_dev,' ') LT strlen(tGr_dev) then Gr_dev = strmid(tGr_dev,0,strpos(tGr_dev,' '))
	;		print, tgr_dev, '->', Gr_dev
			Print_dev = get_text(prompt='Print (PS, PCL) or command line',VAL=Print_dev)
	;		if strpos(Pr_dev,' ') LT strlen(Pr_dev) then Print_dev = strmid(Pr_dev,0,strpos(Pr_dev,' '))
	;		print, pr_dev, '->', Print_dev
	    	Gr_scale = float(get_num(prompt='size(0.5-2)',VAL=Gr_scale))
			location = get_text(prompt='location',VAL=location, group = axis_ID)
	;		if strpos(location,' ') LT strlen(location) then location = strmid(location,0,strpos(location,' '))
			ColTbl = fix(get_num(prompt='Color Table',VAL=ColTbl))
			print,'Choose initial directory for data'
	    	first = pickfile2(filter='*.*', /lpath)
	    	if !VERSION.OS_FAMILY EQ 'unix' then sep = '/' else sep = '\'
	    	sep_pos = rstrpos(first,sep)
	    	DefPath = strmid(first,0,sep_pos)
	    	print, 'Default font (WIN = Arial*14, X = 6x10 (Unix))'
	    	BufLblFont = get_text(prompt='Font',VAL=BufLblFont, group = axis_ID)
    		print, 'Give command line needed to transfer file to printer'
    		PR_Command = get_text(prompt= 'Printer command', VAL=PR_Command, group = axis_ID)
    		Help_cmd = get_text(prompt= 'Help command', VAL=Help_cmd, group = axis_ID)
    		Net_command = get_text(prompt= 'Net_command', VAL=Net_command, group = axis_ID)

; ------------ store these in new (default) *.ini file --------------------
			test = axis_ini_save()
		ENDIF ELSE BEGIN
; -------- call widget to let user define all parameters in the axis.ini fole
; ****** April 2001 - this is NOT working so just print a messge --
			if !VERSION.OS_FAMILY EQ 'Windows' then begin
				axis_options
			endif else begin
				print, ' Please open the text file axis.ini and modify to suit your environment'
			endelse
		ENDELSE
			if keyword_set(set) then WIDGET_CONTROL, UPROMPT, SET_VALUE="Restart Axis for the changes to take effect."
	endelse
endelse

; ------------------- REPORT TO USER THE CURRENT VALUES -----------------
 if strlen(test(0)) NE 0 then begin  	; only print if the ini file has been written or read
  	print, ' '
	print, 'Ini_file = ',ini_file
	print, 'CodePath = ', CodePath
	print, 'Graphics device = ', gr_dev
	print, 'Printer device/command = ', Print_Dev
	print, 'Default Data Path = ', DefPath
	print, 'Graphics scale factor = ', strcompress(string(gr_scale))
	print, 'Initial color table = ', strcompress(string(ColTbl))
	print, 'Location = ', location
	print, 'Print file (or spooler) name = ', Print_file
	print, 'Font for buffer labels = ', BufLblFont
	print, 'Printer command line = ', Pr_command
	print, 'Help command line = ', Help_cmd
	print, 'Net command line = ', Net_command
	print, '______________________________________________________ '

	LPath = DefPath		; bring back last path used when running AXIS
	WritePath=DefPath
 endif
return, test
end

