		 opt	    l+,c+,d+,y+
		 opt	    ow1+,ow2+,ow3+,ow4+,ow5+,ow6+

	       INCDIR	  ":pspråk/asm/include/"
	       INCLUDE	  "exec/types.i"
	       INCLUDE	  "PopUpMenu.i"

	       xref	 @SelectOpt
	       xref	 @Mystrlen	 * In overlay root

	       xdef	 @SelectOptions
	       xdef	 @GetCommandLine
	       xdef	 @GetWBCommandLine
	       xdef	 @MyWrite

	       section	 text,code
@SelectOptions:
	       MOVE.W	 D2,D0
	       JMP	 @SelectOpt(PC)
	       RTS

***********************************************************
* UWORD  GetCommandLine(OldOptions, CommandLine, Length)  *
*			    D3	      A5	  D2	  *
* Input:						  *
*   CommandLine    - Pointer to command line		  *
*   Length	   - Length of Command line		  *
* Output:						  *
*   Selected options					  *
*							  *
***********************************************************
@GetCommandLine:
	       MOVEM.L	 D2-D4/A5/A6,-(SP)
****************************************
* D2 = Length	    A5 = Command line  *
* D3.W = Selected		       *
****************************************
Start:	       TST.L	 D2
	       BEQ.W	 Done
	       MOVEQ.L	 #0,D0

* ------------ Find first non blank
1$	       MOVE.B	 (A5)+,D0
	       SUBQ.L	 #1,D2
	       BEQ.W	 Done
	       CMP.B	 #' ',D0
	       BLE.B	 1$

* ------------ Is it a '-' ?
	       CMP.B	 #'-',D0
	       BNE.B	 Help	       * No

* ------------ Get option
	       MOVEQ.L	 #' ',D1
	       MOVE.B	 (A5)+,D0
	       SUBQ.L	 #1,D2
	       BEQ.B	 2$

* ------------ Is it followed by a '-'
	       MOVE.B	 (A5)+,D1
	       SUBQ.L	 #1,D2

* ------------ make option lowercase
2$	       OR.B	 #$20,D0   * Make small char

	       SUB.B	 #'c',D0
	       BNE.B	 optH

* ------------ Option: ClickMenues
	       MOVEQ.L	 #CLICKMENUES+8,D0
	       BRA.B	 SetOption

optH:	       SUBQ.L	 #'h'-'c',D0
	       BNE.B	 optI

* ------------ Output Helptext
Help:	       MOVEQ.L	 #4,D2
	       JSR	 @MyWrite

	       MOVEQ.L	 #0,D3
	       BRA.W	 Done

optI:	       SUBQ.L	 #'i'-'h',D0
	       BNE.B	 optK

* ------------ Option: ITEMCENTERED
	       MOVEQ.L	 #ITEMSCENTERED+8,D0
	       BRA.B	 SetOption

optK:	       SUBQ.L	 #'k'-'i',D0
	       BNE.B	 optN

* ------------ Option: HotKey
* ------------ convert first digit
	       MOVE.B	 D1,D0
	       BSR.B	 Hex2Bin
	       MOVE.B	 D0,D4
	       BMI.B	 Help	       * Not hex
	       LSL.L	 #4,D4

* ------------ is there more digits ?
	       SUBQ.L	 #1,D2
	       BMI.B	 Help	       * No

	       MOVE.B	 (A5)+,D0
	       BSR.B	 Hex2Bin
	       OR.W	 D0,D4	       * Must be word to set flags
	       BMI.B	 Help

* ------------ Save new HotKey
	       MOVE.B	 D4,D3
	       BRA.B	 Start

* ------------ Code to convert hex characters
Hex2Bin:       OR.B	 #$20,D0
	       LEA	 HexChars(PC),A0
	       MOVEQ.L	 #15,D1
1$	       CMP.B	 (A0)+,D0
	       DBEQ	 D1,1$
	       MOVE.W	 D1,D0
	       RTS

optN:	       SUBQ.L	 #'n'-'k',D0
	       BNE.B	 optO

* ------------ Option: POPUPNOQUAL
	       MOVEQ.L	 #POPUPNOQUAL+8,D0
	       BRA.B	 SetOption

optO:	       SUBQ.L	 #'o'-'n',D0
	       BNE.B	 optP

* ------------ Option: OPENWINDOW
	       MOVEQ.L	 #OPENWINDOW+8,D0
	       BRA.B	 SetOption

optP:	       SUBQ.L	 #'p'-'o',D0
	       BNE.B	 optQ

* ------------ Option: SUBITEMPOINTER
	       MOVEQ.L	 #SUBITEMPOINTER+8,D0
	       BRA.B	 SetOption

optQ:	       SUBQ.L	 #'q'-'p',D0
	       BNE.B	 optS

* ------------ Option: POPUPWITHQUAL
	       MOVEQ.L	 #POPUPWITHQUAL+8,D0
	       BRA.B	 SetOption

optS:	       SUBQ.L	 #'s'-'q',D0
	       BNE.B	 optX

* ------------ Option: SHOWSINGLEMENU
	       MOVEQ.L	 #SHOWSINGLEMENU+8,D0
	       BRA.B	 SetOption

optX:	       SUBQ.L	 #'x'-'s',D0
	       BNE.B	 Help

* ------------ Option: EXIT
	       MOVEQ.L	 #EXIT+8,D0

SetOption:     MOVEQ.L	 #1,D4
	       ASL.L	 D0,D4
	       NOT.L	 D4
	       AND.L	 D4,D3
	       CMP.B	 #' ',D1
	       BGT.B	 1$
	       NOT.L	 D4
	       OR.L	 D4,D3
	       BRA.W	 Start
1$	       CMP.B	 #'-',D1
	       BNE.W	 Help
	       BRA.W	 Start

Done:	       MOVE.W	 D3,D0
	       MOVEM.L	 (SP)+,D2-D4/A5/A6
	       RTS

****************************************
* UWORD  GetWBCommandLine(OldOptions)  *
*			    D3	       *
* Input:			       *
* Output:			       *
*   Selected options		       *
****************************************
@GetWBCommandLine:
	       MOVEM.L	 D2-D3/D5-D6/A2-A3/A6,-(SP)
	       MOVEA.L	 (A4),A6

* ------------ Open icon.library
	       LEA	 ICONNAME(PC),A1
	       MOVEQ.L	 #LIBVERSION,D0
	       JSR	 _LVOOpenLibrary(A6)
	       MOVE.L	 D0,D6
	       BEQ.B	 NoIconLib

************************************
* D3 = Old Options   A6 = ExecBase *
* D6 = IconBase 		   *
************************************
* ------------ Set current directory { CurrentDir(WBenchMsg->sm_ArgList[0].wa_Lock) }
	       MOVEA.L	 _DOSBase(A4),A6
	       MOVE.L	 _WBenchMsg(A4),A0
	       MOVE.L	 sm_ArgList(A0),A2
	       MOVE.L	 A2,D0
	       BEQ.B	 NoArgList
	       MOVE.L	 wa_Lock(A2),D1
	       JSR	 _LVOCurrentDir(A6)

* ------------ Save old directory
	       MOVE.L	 D0,D5

***************************************
* D3 = Old options	 A2 = ArgList *
* D5 = old currentdir	 A6 = DOSbase *
* D6 = IconBase 		      *
***************************************
* ------------ Get the icon.  { GetDiskObject(WbenchMsg->sm_ArgList[0].wa_Name) }
	       MOVE.L	 wa_Name(A2),A0
	       MOVE.L	  D6,A6
	       JSR	 _LVOGetDiskObject(A6)
	       MOVE.L	 D0,A3
	       TST.L	 D0
	       BEQ.B	 NoDiskObject

*****************************************
* D3 = Old options	A2 = ArgList	*
* D5 = Old current dir	A3 = DiskObject *
* D6 = DOSBase		A6 = IconBase	*
*****************************************
* ------------ Find the 'ARG=' string
	       MOVE.L	 do_ToolTypes(A3),A0
	       LEA	 ARGTEXT(PC),A1
	       JSR	 _LVOFindToolTypes(A6)
	       TST.L	 D0
	       BEQ.B	 NoArgs

* ------------ GetCommandLine(OldOptions, CommandLine, strlen(CommandLine))
	       MOVE.L	 D0,A5
	       MOVE.L	 D0,A0
	       JSR	 @Mystrlen
	       MOVE.L	 D0,D2
	       JSR	 @GetCommandLine(PC)
	       TST.W	 D0
	       BEQ.B	 WrongOptions
	       MOVE.L	 D0,D3

*******************************************
* D5 = Old currentdir	 A2 = ArgList	  *
* D6 = DOSBase		 A3 = DiskObject  *
* D3 = New options	 A6 = IconBase	  *
*******************************************
* ------------ Return the disk object
WrongOptions:
NoArgs:        MOVEA.L	 A3,A0
	       JSR	 _LVOFreeDiskObject(A6)

* ------------ Restore current directory
NoDiskObject:  MOVE.L	  _DOSBase(A4),A6
	       MOVE.L	 D5,D1
	       BEQ.B	 NoCurrentDir
	       JSR	 _LVOCurrentDir(A6)

* ------------ Close library
NoCurrentDir:
NoArgList:     MOVE.L	 D6,A1
	       MOVEA.L	 (A4),A6
	       JSR	 _LVOCloseLibrary(A6)

NoIconLib:     MOVE.L	 D3,D0
	       MOVEM.L	 (SP)+,D2-D3/D5-D6/A2-A3/A6
	       RTS

***********************************************************
* VOID MyWrite(TextNumber, Options)  print text to stdout *
*		 D2	    D3				  *
*  Input:						  *
*    TextNumber    0 - Install				  *
*		   1 - Options				  *
*		   2 - Quit				  *
*		   3 - Error				  *
*		   4 - Help				  *
*    Options	   only used if TextNumber = 1		  *
***********************************************************
@MyWrite:      MOVEM.L	  D2-D6/A6,-(SP)
	       MOVE.L	  D2,D4
	       MOVE.L	  D3,D5

* ------------ Find stdout
	       MOVEA.L	  _DOSBase(A4),A6
	       JSR	  _LVOOutput(A6)
	       MOVE.L	  D0,D6
	       BEQ.W	  NoStdout

* ------------ Output PopUpMenu text
	       MOVE.L	  D6,D1
	       LEA	  POPUPMSG(PC),A0
	       MOVE.L	  A0,D2
	       MOVEQ.L	  #POPUPMSG_sizeof,D3
	       JSR	  _LVOWrite(A6)

* ------------ Find what to print
	       SUBQ.L	  #1,D4
	       BMI.B	  InstallText	  * 0
	       BEQ.B	  OptionsText	  * 1
	       SUBQ.B	  #2,D4
	       BMI.W	  QuitText	  * 2
	       BEQ.W	  FailedText	  * 3

* ------------ print helptext
HelpText:      LEA	  HELPMSG(PC),A0
	       MOVE.L	  #HELPMSG_sizeof,D3
	       BRA.W	  Print

* ------------ print installed
InstallText:   LEA	  INSTALLMSG(PC),A0
	       MOVEQ.L	  #INSTALLMSG_sizeof,D3
	       BRA.W	  Print

* ------------ Build option string
OptionsText:   LEA	  OptionBuffer2(PC),A0
	       MOVEQ.L	  #OptionBuffer2-OptionBuffer+1,D3   * 'Current options: ...  \n'
	       BTST	  #CLICKMENUES+8,D5
	       BEQ.B	  1$
	       MOVE.L	  #' -C ',(A0)+
	       ADDQ.L	  #4,D3
1$	       BTST	  #SUBITEMPOINTER+8,D5
	       BEQ.B	  2$
	       MOVE.L	  #' -P ',(A0)+
	       ADDQ.L	  #4,D3
2$	       BTST	  #POPUPNOQUAL+8,D5
	       BEQ.B	  3$
	       MOVE.L	  #' -N ',(A0)+
	       ADDQ.L	  #4,D3
3$	       BTST	  #POPUPWITHQUAL+8,D5
	       BEQ.B	  4$
	       MOVE.L	  #' -Q ',(A0)+
	       ADDQ.L	  #4,D3
4$	       BTST	  #SHOWSINGLEMENU+8,D5
	       BEQ.B	  5$
	       MOVE.L	  #' -S ',(A0)+
	       ADDQ.L	  #4,D3
5$	       BTST	  #ITEMSCENTERED+8,D5
	       BEQ.B	  6$
	       MOVE.L	  #' -I ',(A0)+
	       ADDQ.L	  #4,D3
6$	       MOVE.W	  #'-K',(A0)+

	       LEA	  HexChars(PC),A1
	       MOVEQ.L	  #0,D0
	       MOVE.B	  D5,D0
	       LSR.B	  #4,D0
	       NEG.W	  D0
	       MOVE.B	  $0f(A1,D0.W),(A0)+
	       AND.W	  #$0f,D5
	       NEG.W	  D5
	       MOVE.B	  $0f(A1,D5.W),(A0)+
	       ADDQ.L	  #4,D3

	       MOVE.B	  #$0a,(A0)

	       LEA	  OptionBuffer(PC),A0
	       BRA.B	  Print

* ------------ print removed message
QuitText:      LEA	  QUITMSG(PC),A0
	       MOVEQ.L	  #QUITMSG_sizeof,D3
	       BRA.B	  Print

* ------------ print failed message
FailedText:    LEA	  ERRORMSG(PC),A0
	       MOVEQ.L	  #ERRORMSG_sizeof,D3

Print:	       MOVE.L	  D6,D1
	       MOVE.L	  A0,D2
	       JSR	  _LVOWrite(A6)

NoStdout:      MOVEM.L	  (SP)+,D2-D6/A6
	       RTS

ICONNAME       dc.b	  "icon.library",0
	       EVEN
ARGTEXT        dc.b	  "ARG",0
	       EVEN
HexChars:      dc.b	  "fedcba9876543210"

	       EVEN	  *0 2 4 6 8 0 2 4 6
OptionBuffer:  dc.b	  " Current options ="
OptionBuffer2: dc.b	  " -C  -S  -N  -Q  -S  -T -K##\n"

	       EVEN
POPUPMSG:      dc.b	  $0a,$9b,"0;33m","PopUpMenu",$9b,"0m "
POPUPMSG_sizeof:   EQU	  *-POPUPMSG
	       EVEN
INSTALLMSG:    dc.b	  "ver 4.2 installed.",$0a,$a9," Martin Adrian 1991",$0a
INSTALLMSG_sizeof: EQU	  *-INSTALLMSG
	       EVEN
QUITMSG:       dc.b	  "removed.",$0a
QUITMSG_sizeof:    EQU	  *-QUITMSG
	       EVEN
ERRORMSG:      dc.b	  "failed !!",$0a
ERRORMSG_sizeof:   EQU	  *-ERRORMSG

	       EVEN
HELPMSG:       dc.b	 " ver 4.2",$0a,$0a
	       dc.b	 "Usage: [Run] PopUpMenu [Options]",$0a
	       dc.b	 " -H     Help (this text).",$0a
	       dc.b	 " -C[-]  Clickmenues.",$0a
	       dc.b	 " -P[-]  show sub-item Pointer.  (default)",$0a
	       dc.b	 " -S[-]  Show single menu.",$0a
	       dc.b	 " -I[-]  display Items centered around menu.",$0a
	       dc.b	 " -N[-]  pop-up menues when No qualifier pressed.  (default)",$0a
	       dc.b	 " -Q[-]  pop-up menues when Qualifier pressed.",$0a
	       dc.b	 " -O[-]  Open options window.  (default)",$0a
	       dc.b	 " -K##   set hot-Key [Hex].  (default 19 = 'P')",$0a
	       dc.b	 " -X     eXit PopUpMenu.",$0a
	       dc.b	 " '-' after option will disable it",$0a
HELPMSG_sizeof:  EQU	 *-HELPMSG



