;将此文件拖入ACAD绘图区即会自动安装迷你建筑工具
(if (wcmatch (strcase (getvar 'platform)) "*WINDOWS*")
	(print (getvar "lastprompt"))
)
(setq temp_path (strcase (getvar "lastprompt")))
(vl-load-com)
(setq temp_path (strcase (vl-string-translate "\\" "/" temp_path)))


(vl-catch-all-apply
	'(lambda ( )
		(and
			(wcmatch temp_path "*/*.LSP*")
			(setq
				temp_path (substr temp_path (+ (vl-string-search "/" temp_path) (if (wcmatch (strcase (getvar 'platform)) "*WINDOWS*") -1 1)))
				temp_path (substr temp_path 1 (vl-string-search ".LSP" temp_path))
				temp_path (vl-filename-directory temp_path)
			)
		)
	)
	nil
)

(if (getvar "secureload") (setvar "secureload" 0));关闭加载警告
(if (getvar "acadlspasdoc") (setvar "acadlspasdoc" 1))    ;reload autolisp between drawings
(if (getvar "lispinit") (setvar "lispinit" 1))


(vl-catch-all-apply
	'(lambda ( / str2lst add_path tmp_getdir Popup nn str html bak n chk0 chk1 chk2 chk3 chk4 )
	
		(defun str2lst ( txt str / loa txt1 lst )
			(while
				(and
					txt
					(/= txt "")
					(setq loa (vl-string-search str txt))
				)
				(setq
					txt1 (substr txt 1 loa)
					txt (substr txt (+ 2 loa))
					lst (cons txt1 lst)
				)
			)
			(vl-remove nil (vl-remove "" (reverse (cons txt lst))))
		)
			
		(defun add_path ( main_path sub_path / acad_path temp_d path_ch )

			(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)
			)
			; (setq temp_d (strcat temp_d "\\") )
			(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)
				acad_path (vl-remove nil (mapcar '(lambda( x ) (findfile (vl-string-right-trim "\\/" x))) acad_path))
				acad_path (apply 'strcat (cons (car acad_path) (mapcar '(lambda(x)(strcat ";" x)) (cdr acad_path))))
				acad_path (if (wcmatch (strcase (getvar 'platform)) "*WINDOWS*") acad_path (vl-string-translate "\\" "/" acad_path))
			)
			(setenv "ACAD" acad_path)
			(princ)
		)

		(defun tmp_getdir ( msg / WinShell shFolder path catchit end_path)
			(cond
				((wcmatch (strcase (getvar 'product)) "*BRICSCAD*")
					(setq end_path (dos_getdir msg nil))
				)

				(T
					(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)
						)
					)
					(vlax-release-object winshell)
					end_path
				)
			)
			(if end_path (vl-string-right-trim "/\\"  end_path))
		)


		(defun Popup ( title msg flags / wsh res fix_dir)
			(if (wcmatch (strcase (getvar 'product)) "*BRICSCAD*")
				(vle-alert title msg flags)
				(progn
					(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
				)
			)
		)

		(cond
			(	(not temp_path))
			
			(	(= temp_path ""))
			
			(	(vl-file-directory-p temp_path) (setq mini_path temp_path))

			(
				(and ;(wcmatch (strcase (getvar 'product)) "*AUTOCAD*")
					(wcmatch (strcase (getvar 'platform)) "*WINDOWS*")
					(progn
						(setq mini_path nil)
						(while (= (logand (getvar "CmdActive") 1) 1) (command ""))
						(princ "\n")
						(command "_.copyhist")
						(setq html (vl-catch-all-apply 'vlax-create-object (list "htmlfile")))
					)
					(not (vl-catch-all-error-p html))
					(setq nn (vl-catch-all-apply 'vlax-get (list html 'parentwindow)))
					(not (vl-catch-all-error-p nn))
					(setq nn (vl-catch-all-apply 'vlax-get (list nn 'clipboarddata)))
					(not (vl-catch-all-error-p nn))
					(setq temp_path (vl-catch-all-apply 'vlax-invoke (list nn 'getdata "text")))
					(not (vl-catch-all-error-p temp_path))
					(setq temp_path (strcase (if (= (type temp_path) 'variant) (vlax-variant-value temp_path) temp_path)))
					(setq temp_path (vl-string-trim " " temp_path))
					(progn
						(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)))
						)
						(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)))
					(progn (and html (vlax-release-object html)) T)
					(setq mini_path (if (or (findfile temp_path) (vl-file-directory-p temp_path)) temp_path))
				)
			)

			(	(setq mini_path (tmp_getdir "选择迷你所在文件夹"))
				(setq mini_path (vl-string-right-trim "\//" (vl-string-translate "/" "\\" mini_path)))
			)
		)

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

			(	(not (findfile (strcat mini_path "\\IMINI.slb")))
				(Popup "终止安装【谜你iMini工具】" "无法在此文件夹找到谜你iMini工具核心文件" 16)
				(exit)
			)
		)

		(setq
			chk0 (wcmatch mini_path "*(*)*")
			chk1 (wcmatch mini_path "* *")
			chk2 (= "C:" (substr mini_path 1 2))
			chk4 (> (strlen mini_path) 20)
			temp_path mini_path
			chk3 (1- (length (str2lst (vl-string-trim "\//" temp_path) "/")))
		)
		; (while (/= (setq nn (vl-filename-directory temp_path)) (vl-string-right-trim "\//" 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工具】" nn 36)
				)
		)

		(cond
			((= nn 7) (princ "\n建议将谜你iMini工具放在【D:\\iMini】文件夹下。") (exit))

			(T
				(and
					(> (strlen (setq chk4 (vl-filename-base mini_path))) 12)
					(= 6 (Popup "感谢安装【谜你iMini工具】" (strcat "当前安装文件夹名字【" chk4 "】过长，容易导致ACAD崩溃，别问为什么\n\n★是否将此文件夹改名为【iMini】后继续安装？") 36))
					(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" "menu"))
				(setq
					bak (strcase (getvar 'product))
					nn (cond
							(	(findfile (strcat mini_path "\\1_lsp\\_-load.lsp")))
							(	(wcmatch bak "*BRICSCAD*")
								(findfile (strcat mini_path "\\iMini.des"))
							)
							(	(wcmatch bak "*AUTOCAD*,*GSTARCAD*")
								(findfile (strcat mini_path "\\iMini.fas"))
							)
							(	(wcmatch bak "*ZWCAD*")
								(findfile (strcat mini_path "\\iMini.zelx"))
							)
						)
				)
				(and nn (load nn))
				(if (setq nn (findfile (strcat mini_path "\\pattern")))
					(add_path nn (mai_dos_subdir nn nil))
				)
				(mai_change_user (car (mai_get_current_user)))
				(cond
					(	(getvar "menubar") (setvar "menubar" 1))
					(	(and
							(wcmatch bak "*ZWCAD*")
							(getvar "wscurrent")
						)
						(setvar "wscurrent" "ZWCAD 经典")
					)
				)
				(cond
					((wcmatch bak "*BRICSCAD*") (mai_bcad_loadmenu "imini" (findfile "imini-bcad.mnu")))
					((wcmatch bak "*GSTARCAD*") (command "menuload" (findfile "imini-gcad.mnu")))
					((wcmatch bak "*ZWCAD*") (mai_bcad_loadmenu "imini" (findfile "imini-zwcad.mnu")))
					((wcmatch bak "*AUTOCAD*") (c:minimenu))
				)
				(princ (strcat "\n成功安装【谜你iMini工具】."))
				(princ "\n首次运行谜你iMini工具，建议运行【mini_config】命令修改谜你设置文件【myconfig.set】.")
			)
		)

	)
	nil
)





















