; Copyright 2004-2005 Christopher Evans
; email:   tekno1911@yahoo.com
; Website: http://nxdos.sourceforge.net/
;                                                                           ;
;       This program is copyrighted, but may be freely distributed          ;
;       under the terms of the Free Software Foundation's GNU General       ;
;       Public License v2 (or later.)  See the file COPYING for the         ;
;       legalities.  If you did not receive a copy of COPYING, you          ;
;       may request one from the Free Software Foundation, Inc.,            ;
;       59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           ;
;       ABSOLUTELY NO WARRANTY -- use it at your own risk!                  ;
;                                                                           ;

code            segment byte public
                assume  cs:code, ds:code
		org	100h
main          proc    far

start:        jmp     initialize
;
Proggy    db 'move',0
progvers  dw 0100h
progdesc  db 'Moves file(s) to new location.',0ffh,0
;
include       \nxdos\src\toolbox.asm
;
initialize:
              _print proggy, 1
              _echo ' '
              _echo 'v'
              mov  ax, progvers
              call wrdec
              _echo '-'
              _echo ' '
              _print progdesc, 1
              _newline
              ;

;              if (isswitch('/?')==1) {
;                        show_help;
;                 } else {
;                        ...
;                 }
              mov  bh, '/'
              mov  bl, '?'
              call scancmdline
              mov  _argc, cx
              cmp  al, 1
              jne  _nohelp
              _print syntax, 1
              jmp  exitptr
_nohelp:
              mov  bh, '/'
              mov  bl, 'O'
              call scancmdline
              cmp  al, 1
              jne  _nooverwrite
              inc  overwrite
_nooverwrite:
              mov  bh, '/'
              mov  bl, 'C'
              call scancmdline
              cmp  al, 1
              jne  _nodeletesource
              inc  delsource
_nodeletesource:
              mov  ax, _argc
              cmp  ax, 3
              jb   _insufparam
              call cs:program
              ;
              cmp  al, 2
              jne  _chkerr3
              _print nofilemsg, 1
              jmp  exitptr
_chkerr3:
              cmp  al, 3
              jne  _chkerr4
              _print ctovrwrte, 1
              jmp  exitptr
_chkerr4:
              cmp  al, 4
              jne  exitptr 
              _print cpfileerr, 1
              jmp  exitptr
_insufparam:  _print insufmsg, 1
exitptr:      call dosexit
              int 20h      

; ______   .______.     ._________.   ______
;(_Init_)--| Main |-----| Program |--(_exit_)
;          `------'     `---------'

main          endp

; ===========================================================================
extract_filenames proc far
; ===========================================================================
              mov  si, 82h  ; PSP
              mov  di, offset sourcefile
              mov  bx, _extralen$
              mov  dh, ' '
              mov  dl, 0dh
              call strcopy

              mov  di, offset destfile
              mov  bx, _extralen$
              mov  dh, ' '
              mov  dl, 0dh
              call strcopy
              ret
extract_filenames endp

; ===========================================================================
program proc far
; ===========================================================================

;        extract_filenames
;        if (fileexist(source) {
;           if (fileexist(dest) && (overwrite)) {
;              filecopy(source,dest)
;              if (!copyonly) delete(source)
;              }
;           }

        call extract_filenames
        _fileexist sourcefile                                ; ds:dx filename
        jc   _filecopy
        mov  al, 2
_pext:  ret
_nodest:  
        mov  al, 3
        jmp  short _pext   
_filecopy:
                mov  al, overwrite
                or   al, al
                jnz  __overwrite
                _fileexist destfile
                jc   _nodest
__overwrite:
                        _print sourcefile, 1
                        _echo ' '
                        _echo '>'
                        _echo ' '
                        _print destfile, 1
                        mov  si, offset sourcefile
                        mov  di, offset destfile
                        push ds
                        pop  es
                        call filecopy
                        jc   _nocopy
                        _echo ' '
                        _echo 'O'
                        _echo 'K'
                        _newline
                        clc
                        mov  al, delsource
                        or   al, al
                        jz   _pexit
                        mov  dx, offset sourcefile
                        call ferase
                        clc
                        mov  al, 0
                        jmp  short _pexit
_nocopy:  
        mov  al, 4
_perror:
        stc
_pexit: ret
program endp

; ===========================================================================
; === Data Segment                                                        ===
; ===========================================================================

_argc     dw 0
syntax    db 'Syntax: ',0ffh
          db '        MOVE [source] [destination] [/O] [/C] ... Copies and deletes source.',0ffh
          db '        /O ...................................... Overwrites destination.',0ffh
          db '        /C ...................................... Copy only.',0ffh,0
          db '',0
insufmsg  db 'Required parameters criteria not met.',0ffh,0
nofilemsg db 'Source file is not found.',0ffh,0
ctovrwrte db 'Cannot overwrite destination. (Use /O)',0ffh,0
cpfileerr db 'I/O Error.',0ffh,0
overwrite db 0
delsource db 0

sourcefile db 127 dup(0),0
destfile   db 127 dup(0),0
          _compile

code            ends

ProgramEnd      EQU $-01h

		end	start
