;将此文件拖入ACAD绘图区即会自动安装迷你建筑工具
(print (getvar "lastprompt"))
(setq temp_path (strcase (getvar "lastprompt")))
(vl-catch-all-apply
   '(lambda ( )
        (vl-load-com)
        (and (wcmatch temp_path "*\(LOAD \"*\"\)")
             (setq temp_path (vl-string-translate "/" "\\" temp_path)
                   temp_path (substr temp_path (+ 2 (vl-string-search "\"" temp_path)))
                   temp_path (substr temp_path 1 (vl-string-search "\"" temp_path))
                   temp_path (vl-filename-directory temp_path)
             )
        )
    )
    nil
)
(vl-load-com)
(if (getvar "secureload") (setvar "secureload" 0))



(vl-catch-all-apply
     '(lambda ( / dos_getdir Popup add_path
                     nn str html bak n Popup chk0 chk1 chk2 chk3 chk4
              )


 ;0 确定
 ;1 确定取消
 ;2 终止重试忽略
 ;3 是否取消
 ;4 是否
 ;5 重试取消
 ;6 取消重试继续

 ;16红色X
 ;32白色？
 ;48黄色三角！
 ;64白色i
 (progn
    (defun Popup ( title flags msg / wsh res )
        (vl-catch-all-apply
          '(lambda ()
              (setq wsh (vlax-create-object "WScript.Shell")
                    res (vlax-invoke-method wsh 'popup msg 0 title flags)
              )
            )
        )
        (if wsh (vlax-release-object wsh))
        res
    )


    (defun add_path ( main_path sub_path / str2lst acad_path temp_d path_ch )

        (defun str2lst ( txt / loa txt1 lst )
           (while (and txt
                       (/= txt "")
                       (setq loa (vl-string-search ";" txt))
                  )
                   (setq txt1 (substr txt 1 loa)
                         txt (substr txt (+ 2 loa))
                         lst (cons txt1 lst)
                   )
            )
            (vl-remove "" (reverse (cons txt1 lst)))
        )

      (setq acad_path (vl-string-translate "/" "\\" (strcase (getenv "ACAD") T))
            acad_path (reverse (str2lst acad_path))
            main_path (vl-string-translate "/" "\\" (strcase main_path T))
            main_path (vl-string-right-trim "\\" main_path)
            acad_path (cons main_path acad_path)
      )
      (foreach temp_d sub_path
           (setq temp_d (vl-string-translate "/" "\\" (strcase temp_d T))
                 temp_d (strcat main_path "\\" (vl-string-trim "\\" temp_d))
           )
           (and (vl-file-directory-p temp_d)
                (not (member temp_d acad_path))
                (setq acad_path (cons temp_d acad_path))
           )
      )
      (setq acad_path (reverse acad_path))
      (setenv "ACAD" (apply 'strcat (cons (car acad_path) (mapcar '(lambda(x)(strcat ";" x)) (cdr acad_path)))))
      (princ)
    )


    (defun dos_getdir ( msg / WinShell shFolder path catchit end_path)
        (setq winshell (vlax-create-object "Shell.Application")
              shFolder (vlax-invoke-method WinShell 'BrowseForFolder 0 msg 1)
        )
        (if shFolder
            (progn (setq catchit (vl-catch-all-apply
                                        '(lambda ()
                                               (setq shFolder (vlax-get shFolder 'self)
                                                     path (vlax-get shFolder 'path)
                                               )
                                         )
                                 )
                         end_path (if (not (vl-catch-all-error-p catchit)) path)
                   )
                   (vlax-release-object shFolder)
                   (if (/= "\\" (substr end_path (strlen end_path)))
                       (setq end_path (strcat end_path "\\"))
                   )
            )
        )
        (vlax-release-object winshell)
        (vl-string-rigth-trim "\\" end_path)
    )
)


     (cond ((vl-file-directory-p temp_path) (setq mini_path temp_path))
           ((and (wcmatch (strcase (getvar 'product)) "*AUTOCAD*")
                 (progn (while (= (logand (getvar "CmdActive") 1) 1) (command ""))
						(princ "\n")
						(command "_.copyhist")
						(and (setq html (vlax-create-object "htmlfile"))
							 (setq temp_path (vlax-invoke-method (vlax-get (vlax-get html 'parentwindow) 'clipboarddata) 'getdata "text")
								   temp_path (strcase (if (= (type temp_path) 'variant) (vlax-variant-value temp_path) temp_path))
							 )
							 (progn
							  (setq temp_path (vl-string-trim " " temp_path))
							  (while (vl-string-search "\r\n" temp_path)
									 (setq temp_path (vl-string-subst "" "\r\n" temp_path))
							  )
							  (while (setq nn (vl-string-search "(LOAD \"" temp_path))
									 (setq temp_path (substr temp_path (+ nn 8)))
							  )
							  (and (setq nn (vl-string-search "\")" temp_path))
								   (setq temp_path (substr temp_path 1 nn)) ;file name
							  )
							 )
							 (setq temp_path (vl-filename-directory (vl-string-translate "/" "\\" temp_path)))
							 (vl-file-directory-p temp_path)
							 (setq mini_path temp_path)
						)
						(and html (vlax-release-object html))
                        mini_path
				 )
             )
           )
           ((setq mini_path (dos_getdir "选择迷你所在文件夹")))
      )

      (cond ((or (not mini_path) (= "" mini_path) (not (vl-file-directory-p mini_path)))
             (Popup "终止安装【谜你iMini工具】" 16 "无法获取目录名,终止安装，\n建议将谜你iMini工具放在【D:\\iMini】文件夹下")
             (exit)
            )
            ((not (findfile (strcat mini_path "/IMINI.slb")))
             (Popup "终止安装【谜你iMini工具】" 16 "无法在此文件夹找到谜你iMini工具核心文件【IMINI.fas】")
             (exit)
            )
      )

      (setq chk0 (wcmatch mini_path "*(*)*")
            chk1 (wcmatch mini_path "* *")
            chk2 (= "C:" (substr mini_path 1 2))
            chk3 0
            chk4 (> (strlen mini_path) 20)
            temp_path mini_path
      )
      (while (/= (setq nn (vl-filename-directory temp_path)) temp_path) (setq chk3 (1+ chk3) temp_path nn))
      (setq nn (strcat "检测到谜你iMini工具安装文件夹在：" mini_path "\n"
                       (if chk0
                           "\n●安装文件夹中含有括号【..(..)..】,会导致谜你iMini工具某些命令出错\n"
                           ""
                       )
                       (if chk1
                           "\n●安装文件夹中含有空格[... ...],会导致谜你iMini工具某些命令出错\n"
                           ""
                       )
                       (if chk2
                           "\n●建议不要将如此重要的工具安装在 C 盘。\n"
                           ""
                       )
                       (if (> chk3 1)
                           (strcat "\n●当前安装文件夹有【" (itoa chk3) "】重嵌套，容易导致CAD崩溃。\n")
                           ""
                       )
                       (if chk4
                           "\n●当前安装文件夹名字过长，容易导致 CAD 崩溃。\n"
                           ""
                       )
                       (if (or chk0 chk1 chk2 chk4 (> chk3 1))
                           "\n★建议将谜你iMini工具文件移动到【D:\\iMini】文件夹下再继续安装，别问为什么，不解释\n★是否不作任何修改继续安装？"
                           ""
                       )
                )
            nn  (if (or chk0 chk1 chk2 chk4 (> chk3 1))
                    (Popup "感谢安装【谜你iMini工具】" 36 nn)
                )
      )

      (cond ((= nn 7) (princ "\n建议将谜你iMini工具放在【D:\\iMini】文件夹下。") (exit))
            (T
             (and (> (strlen (setq chk4 (vl-filename-base mini_path))) 12)
                  (= 6 (setq nnn (Popup "感谢安装【谜你iMini工具】" 36 (strcat "当前安装文件夹名字【" chk4 "】过长，容易导致ACAD崩溃，别问为什么\n\n★是否将此文件夹改名为【iMini】后继续安装？"))))
                  (progn (setq nn 0
                               bak (strcat (substr mini_path 1 2) "\\iMini")
                               chk0 bak
                         )
                         (while (vl-file-directory-p chk0)
                             (setq nn (1+ nn) chk0 (strcat bak (itoa nn)))
                         )
                         (if (vl-file-rename mini_path chk0)
                             (progn (setq mini_path chk0)
                                    (princ "\n******更改文件夹名字成功，继续安装.")
                             )
                             (princ "\n******更改文件夹名字出错，保留原来名字继续安装.")
                         )
                  )
             )
             (setenv "ACAD" (strcat mini_path ";" (getenv "ACAD")))
             (add_path mini_path (list "用户设置" "Dwgs_lib" "fonts" "autoload" "autoload_user" "pattern" "Plus_lib" "1_lsp"))
			 (cond ((setq nn (findfile (strcat mini_path "/1_lsp/_-load.lsp"))))
				   ((wcmatch (strcase (getvar 'product)) "*BRICSCAD*")
					(setq nn (findfile (strcat mini_path "/iMini.des")))
				   )
				   ((wcmatch (strcase (getvar 'product)) "*AUTOCAD*")
					(setq nn (findfile (strcat mini_path "/iMini.fas")))
				   )
			 )
             (and nn (load nn))
             (setq nn (strcat mini_path "\\pattern"))
             (add_path nn (mai_dos_subdir nn nil))
             (mai_change_user (car (mai_get_current_user)))
             (princ (strcat "\n感谢安装【谜你iMini工具】，成功安装."))
             (or (wcmatch (strcase (getvar 'product)) "*AUTOCAD*")
                 (vl-catch-all-apply '(lambda()(and (getvar "wscurrent") (> (atof (getvar "ACADVER")) 18.) (setvar "wscurrent" "AutoCAD 经典"))) nil)
             )
             (if (getvar "menubar") (setvar "menubar" 1))
			 (cond ((wcmatch (strcase (getvar 'product)) "*BRICSCAD*")
					(command "menuload" "imini-bcad.mnu")
				   )
				   ((wcmatch (strcase (getvar 'product)) "*AUTOCAD*")
					(c:minimenu)
				   )
			 )

             (princ "\n首次运行谜你iMini工具，建议运行【mini_config】命令修改谜你设置文件【myconfig.set】.")
             ; (and (not (findfile (strcat str "/acad.pgp")))
                  ; (mai_auto_input_pgp)
                  ; (princ "\n成功导入快捷键.")
             ; )
            )
     )
   )
   nil
)
(setq temp_path nil)
(princ)

























