	SetCompressor /SOLID lzma
	CRCCheck on
	!include "MUI.nsh"
	!include "WinMessages.nsh"
	!include "LogicLib.nsh"

	!define STYPE_DISKTREE    	 		 	    0
	!define ACCESS_READ										0x01
	!define ACCESS_WRITE									0x02
	!define ACCESS_CREATE									0x04
	!define ACCESS_EXEC										0x08
	!define ACCESS_DELETE									0x10
	!define ACCESS_ATRIB									0x20
	!define ACCESS_PERM										0x40
	!define ACCESS_ALL										0x7F

	!define GENERIC_READ									0x80000000
	!define GENERIC_WRITE          				0x40000000
	!define GENERIC_EXECUTE								0x20000000
	!define GENERIC_ALL             	  	0x10000000
	!define NO_INHERITANCE								0x0
	!define SECURITY_DESCRIPTOR_REVISION	1

; ACCESS_MODE values
	!define NOT_USED_ACCESS												0
	!define GRANT_ACCESS													1
	!define SET_ACCESS														2
	!define DENY_ACCESS														3
	!define REVOKE_ACCESS													4
	!define SET_AUDIT_SUCCESS											5
	!define SET_AUDIT_FAILURE											6

; MULTIPLE_TRUSTEE_OPERATION values
	!define NO_MULTIPLE_TRUSTEE										0
	!define TRUSTEE_IS_IMPERSONATE								1

; TRUSTEE_FORM values
	!define TRUSTEE_IS_SID												0
	!define TRUSTEE_IS_NAME												1
	!define TRUSTEE_BAD_FORM											2
	!define TRUSTEE_IS_OBJECTS_AND_SID						3
	!define TRUSTEE_IS_OBJECTS_AND_NAME						4

; TRUSTEE_TYPE values
	!define TRUSTEE_IS_UNKNOWN										0
	!define TRUSTEE_IS_USER												1
	!define TRUSTEE_IS_GROUP											2
	!define TRUSTEE_IS_DOMAIN											3
	!define TRUSTEE_IS_ALIAS											4
	!define TRUSTEE_IS_WELL_KNOWN_GROUP						5
	!define TRUSTEE_IS_DELETED										6
	!define TRUSTEE_IS_INVALID										7
	!define TRUSTEE_IS_COMPUTER										8

; Structure Definitions
	!define strSHARE_INFO_502 '(w,i,w,i,i,i,w,w,i,i)i'
	!define strEXPLICIT_ACCESS '(i,i,i,i,i,i,i,i)i'

; Define install options
	Name "Create Shares"
	OutFile "CreateShares.exe"
	InstallDir "$PLUGINSDIR"
	ShowInstDetails nevershow

; MUI Settings
	XPStyle on
	!define MUI_ABORTWARNING

Section # "PostInstall"
SectionEnd

!macro LookupAccountName  USERNAME USER_SID_OUT USER_SID_BUFFER_OUT
# Get the SID of the active user in a buffer and a variable
	StrCpy ${USER_SID_OUT} ""
	StrCpy ${USER_SID_BUFFER_OUT} ""
	System::Call /NOUNLOAD '*(&w${NSIS_MAX_STRLEN})i.R4'
	System::Call /NOUNLOAD 'advapi32::LookupAccountNameA(,t "${USERNAME}",i R4,*i ${NSIS_MAX_STRLEN},t .R9,*i ${NSIS_MAX_STRLEN},*i .r4)i.r5'
	StrCpy ${USER_SID_BUFFER_OUT} $R4
	System::Call /NOUNLOAD 'advapi32::ConvertSidToStringSid(i R4,*t .R8)i.r5'
	StrCpy ${USER_SID_OUT} $R8
	System::Free $R4
	System::Free 0
; remember to free the SID buffer
!macroend

!macro CreateNewShare USER SHARENAME SHARE_TYPE SHARE_COMMENT SHARE_PERMISSIONS ACL_ACCESS MAX_USERS CURRENT_USES SHARE_PATH SHARE_PASS
; Will generate a share with Share Level Security

; Get the SID of the user and place it as a text at $R8 and the buffer with the SID to $R9
	StrCpy $R8 ""
	StrCpy $R9 ""
	!insertmacro LookupAccountName ${USER} $R8 $R9

; Create the EXPLICIT_ACCESS structure and place it on $R6
	System::Call /NOUNLOAD '*${strEXPLICIT_ACCESS}(${ACL_ACCESS},${SET_ACCESS},${NO_INHERITANCE},0,${NO_MULTIPLE_TRUSTEE},${TRUSTEE_IS_SID},${TRUSTEE_IS_USER},$R9).R6'

; Now create the ACL for the share and place it in R5
	System::Call /NOUNLOAD 'advapi32::SetEntriesInAclA(i 1,i R6,i 0,*i .R5)i.r1'
	MessageBox MB_OK $1$\n$R5$\n$R9$\n$R8

; Create an empty security descriptor (http://msdn2.microsoft.com/en-us/library/aa378863.aspx) and place it in R4
	#System::Call /NOUNLOAD 'advapi32::InitializeSecurityDescriptor(*i.R4,i ${SECURITY_DESCRIPTOR_REVISION})i.r1'

; Add the ACL to the security descriptor (http://msdn2.microsoft.com/en-us/library/aa379583.aspx)
	#System::Call /NOUNLOAD 'advapi32::SetSecurityDescriptorDacl(i R4,i 1,i R5,i 0)i.r1'

; Generate the structure that holds the share info and place it on $R0
	#System::Call /NOUNLOAD '*${strSHARE_INFO_502}("${SHARENAME}",${SHARE_TYPE},"${SHARE_COMMENT}",${SHARE_PERMISSIONS},${MAX_USERS},${CURRENT_USES},"${SHARE_PATH}","${SHARE_PASS}",0,R4).R0'
	#System::Call /NOUNLOAD 'netapi32::NetShareAdd(, i 502, i R0, *i .R1) i .r1 ?e'
	#Pop $9

; Cleanup the memory now
	System::Free $R4
	System::Free $R5
	System::Free $R6
	System::Free $R7
	System::Free $R9
	System::Free $R0
!macroend

Function .onInit
; Make sure the program is not running
	System::Call /NOUNLOAD 'kernel32::CreateMutexA(i n, i 0, t "CreateShares") i .r1 ?e'
		Pop $R0
		StrCmp $R0 0 +3
		MessageBox MB_OK|MB_ICONEXCLAMATION "Another instance of this program is already running!$\nAborting execution!"
		Abort

; Share 'MyDocuments' for user jdoe and give him full access
	!insertmacro CreateNewShare "kichik" "My Documents" ${STYPE_DISKTREE} "jdoe's Documents" ${ACCESS_ALL} ${GENERIC_READ}|${GENERIC_EXECUTE} -1 0 "$DOCUMENTS" ""

; Finish the process
	SetPluginUnload manual
	Banner::destroy
	System::Free 0
	Quit
FunctionEnd

Function .onGUIEnd
	SetPluginUnload manual
	Banner::destroy
	System::Free 0
FunctionEnd