option explicit

dim RADMINPATH
       
RADMINPATH = "C:\PROGRAM FILES\RADMIN\RADMIN.EXE"

dim objfso
dim objshell
dim objshortcut
dim strExePath
dim strFilename
dim strConnect
dim strThrough
dim strUpdates
dim bBatch
dim bAREncrypt
dim bARThrough
dim bARFile
dim bARLocolor
dim bARUpdates

function GetParams()
dim objArg
dim lPos
dim errmsg
        for each objArg in WScript.Arguments
                lpos = 0
                lpos = instr(1,objarg,"file:",1)
                if lpos > 0 then
                        strfilename = mid(objarg,lpos+5,len(objarg)-(lpos+4))
                end if
                lpos = instr(1,objarg,"connect:",1)
                if lpos > 0 then
                        strConnect = mid(objarg,lpos+8,len(objarg)-(lpos+7))
                end if
                lpos = instr(1,objarg,"through",1)
                if lpos > 0 then
                       
                        if mid(objarg,lpos-1,1) = "-" then
                                barthrough = 2
                        else
                                if len(objarg)>9 then   
                                        strthrough = mid(objarg,lpos+8,len(objarg)-(lpos+7))
                                        barthrough = 1
                                else
                                        strthrough = "ERROR"
                                end if
                        end if
                end if
                lpos = instr(1,objarg,"updates",1)
                if lpos > 0 then
                       
                        if mid(objarg,lpos-1,1) = "-" then
                                barupdates = 2
                        else
                                if len(objarg)>8 then   
                                        strupdates = mid(objarg,lpos+8,len(objarg)-(lpos+7))
                                        barupdates = 1
                                else
                                        strupdates = "ERROR"
                                end if
                        end if
                end if
               
                lpos = instr(1,objarg,"/locolor",1)
                if lpos > 0 then
                        barlocolor = 2
                end if
                lpos = instr(1,objarg,"/-locolor",1)
                if lpos > 0 then
                        barlocolor = 1
                end if
                lpos = instr(1,objarg,"/encrypt",1)
                if lpos > 0 then
                        barencrypt = 2
                end if
                lpos = instr(1,objarg,"/-encrypt",1)
                if lpos > 0 then
                        barencrypt = 1
                end if
               
                lpos = instr(1,objarg,"/transfer",1)
                if lpos > 0 then
                        barfile = 2
                end if
                lpos = instr(1,objarg,"/-transfer",1)
                if lpos > 0 then
                        barfile = 1
                end if
                lpos = instr(1,objarg,"/batch",1)
                if lpos > 0 then
                        bBatch = 2
                end if
        next

       
        if (bbatch = 0 and strfilename = "") or strthrough = "ERROR" or Strupdates = "ERROR"  then
                call help
        end if

end function

function startshortcut()
        set objfso = createobject("scripting.filesystemobject")
        strexepath = objfso.getabsolutepathname(".")
       
       
        if bbatch = 0 then
                set objShell = createobject("WScript.shell")
                set objShortcut = objShell.createshortcut(strexepath & "\" & strfilename & " - " & strconnect & ".lnk") 
                if objshortcut.targetpath <> "
" then
                        call EditShortcut
                else
                        call Createshortcut
                end if
        else
                call BatchProcessing
        end if
end function

function Init()

        set objfso = nothing
        set objshell = nothing
        set objshortcut = nothing
        strExePath = "
"
        strFilename = "
"
        strConnect = "
"
        strThrough = "
"
        bBatch = 0
        barfile = 0
        barthrough = 0
        barencrypt =0
        barlocolor =0
        barupdates =0
       
end function

function EditShortcut()
dim strnewarguments
dim lpos
        stop
        strnewarguments = "
"
        if strconnect <> "
" then
                stop
                lpos = instr(1,objshortcut.arguments,"
/connect:",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
/connect:" & strconnect & " " & trim(right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1)))
                else
                        strnewarguments = "
/connect:" & strconnect & " " & strnewarguments
                end if
                objshortcut.arguments = strnewarguments
        end if 
        lpos = 0
        strnewarguments = "
"   
        if strthrough <> "
" then
                lpos = instr(1,objshortcut.arguments,"
/through:",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
/through:" & strthrough & " " & trim(right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1)))
                end if
                objshortcut.arguments = trim(strnewarguments)
        end if
        lpos = 0
        strnewarguments = "
"
        if barthrough = 2 then
                lpos = instr(1,objshortcut.arguments,"
/through:",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
" & right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1))
                end if
                objshortcut.arguments = trim(strnewarguments)
        end if
        lpos = 0
        strnewarguments = "
"   
        if strupdates <> "
" then
                lpos = instr(1,objshortcut.arguments,"
/updates:",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
/updates:" & strupdates & " " & trim(right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1)))
                end if
                objshortcut.arguments = trim(strnewarguments)
        end if
        lpos = 0
        strnewarguments = "
"
        if barupdates = 2 then
                lpos = instr(1,objshortcut.arguments,"
/updates:",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
" & right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1))
                end if
                objshortcut.arguments = trim(strnewarguments)
        end if
        lpos = 0
        strnewarguments = "
"
        if barencrypt = 2 then
                lpos = instr(1,objshortcut.arguments,"
/encrypt",1)
               
                if lpos = 0 then
                        objshortcut.arguments = trim(objshortcut.arguments) & "
" & "/encrypt"
                end if
        elseif barencrypt = 1 then
                lpos = instr(1,objshortcut.arguments,"
/encrypt",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
" & trim(right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1)))
                end if
                objshortcut.arguments = trim(strnewarguments)
        end if
       
        lpos = 0
        strnewarguments = "
"
        if barlocolor = 2 then
                lpos = instr(1,objshortcut.arguments,"
/locolor",1)
               
                if lpos = 0 then
                        objshortcut.arguments = trim(objshortcut.arguments) & "
" & "/locolor"
                end if
        elseif barlocolor = 1 then
                lpos = instr(1,objshortcut.arguments,"
/locolor",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
" & trim(right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1)))
                end if
                objshortcut.arguments = trim(strnewarguments)
        end if
       
        lpos = 0
        strnewarguments = "
"
        if barfile = 2 then
                lpos = instr(1,objshortcut.arguments,"
/file",1)
               
                if lpos = 0 then
                        objshortcut.arguments = objshortcut.arguments & "
" & "/file"
                end if
        elseif barfile = 1 then
                lpos = instr(1,objshortcut.arguments,"
/file",1)
               
                if lpos > 1 then
                        strnewarguments = left(objshortcut.arguments,lpos-1)
                end if
               
                lpos = instr(lpos+1,objshortcut.arguments,"
/",1)
                if lpos>0 then
                        strnewarguments = trim(strnewarguments) & "
" & trim(right(objshortcut.arguments,len(objshortcut.arguments)-(lpos-1)))
                end if
                objshortcut.arguments = trim(strnewarguments)
        end if
        objshortcut.save
end function

function createshortcut()
        objshortcut.targetpath = radminpath
       
        if strconnect <> "
" then
                objshortcut.arguments = objshortcut.arguments & "
/connect:" & strconnect & " "
        Else
                call Help
               
        end if
        if strthrough <> "
" then
                objshortcut.arguments = objshortcut.arguments & "
/through:" & strthrough & " "
        end if
        if barencrypt = 2 then
                objshortcut.arguments = objshortcut.arguments & "
/encrypt" & " "
        end if
        if barlocolor = 2 then
                objshortcut.arguments = objshortcut.arguments & "
/locolor" & " "
        end if
        if barfile = 2 then
                objshortcut.arguments = objshortcut.arguments & "
/file" & " "
        end if
        objshortcut.save       
end function

function BatchProcessing()
dim objfolder
dim objfile
dim lpos
       
        lpos = 0
        set objfolder = objfso.getfolder("
.")
       
        for each objfile in objfolder.files
                lpos = instr(1,mid(objfile.path,len(objfile.path) - 3,4),"
lnk",1)
                if lpos > 0 then
                        strfilename = objfso.getbasename(objfile.path)
                        set objShell = createobject("
WScript.shell")
                        set objShortcut = objShell.createshortcut(strexepath & "
\" & strfilename & ".lnk")
                        call editshortcut
                end if
        next


end function

function Help()
        dim strmessage
        strmessage = "
Usage Guide:" & vbcrlf & "/connect: ... Required when creating a new shortcut" & vbcrlf & "/file: ... Always required unless /batch is used" & vbcrlf & "/batch ... Performs requested processing on all shortcuts in the working folder" & vbcrlf & "/through: ... Specifies a server to connect through" & vbcrlf & "/-through ... Removes the through parameter" & vbcrlf & "/locolor ... Enables locolor mode" & vbcrlf & "/-locolor ... Removes the locolor option" & vbcrlf & "/encrypt ... Enables an encrypted link" & vbcrlf & "/-encrypt ... Removes the encrypt option" & vbcrlf & "/transfer ... Enables the file transfer mode" & vbcrlf & "/-transfer .. Removes the file transfer option" & vbcrlf & "/updates:** ... Controls the refresh rate" & vbcrlf & "/-updates ... Removes the refresh rate option" & vbcrlf & "All the switches can be used in batch mode except /file which is ignored" & vbcrlf & "as in batch mode it is not required."
        msgbox strmessage
        wscript.quit(-1)
end function


call init
call getparams
call startshortcut