; AutoIt Version: 3.3.0
; Language:       English
; Platform:       Win9x/NT
; Author:         yanni

#Include <ScreenCapture.au3>
#include <ClipBoard.au3>
#Include <String.au3>
#Include <File.au3>
#notrayicon

$hBmp = _ClipBoard_GetData($CF_BITMAP )

if $cmdline[0]=0 then
selector()
exit
else
goon()
endif

func selector()
$imgeditor=FileOpenDialog("Select your preferred image editor", @ProgramFilesDir, "Image Editors (*.exe)", 1)
If @error then
exit
else
IniWrite(@ScriptDir & "\imgedplus.ini", "imgedpath", 1, $imgeditor)
exit
endif
endfunc

func goon()
If FileExists(@ScriptDir & "\imgedplus.ini") Then
getprotocol()
else
fract()
endif
endfunc

func fract()
$imgeditor=FileOpenDialog("Select your preferred image editor", @ProgramFilesDir, "Image Editors (*.exe)", 1)
If @error then
msgbox(64, "error", "no image editor has been selected, can not parse your command. ")
exit
else
IniWrite(@ScriptDir & "\imgedplus.ini", "imgedpath", 1, $imgeditor)
getprotocol()
endif
endfunc

func getprotocol()
$imgprotocol=Stringleft($CmdLineRaw, 4)
If $imgprotocol =("file") Then
processlocal()
else
processweb()
endif
endfunc

func processlocal()
$pendpath1=StringReplace($CmdLineRaw ,"file:///" ,"" ,1)
$pendpath2=StringReplace($pendpath1 ,"/" ,"\" ,0)
$pendpath3=StringReplace($pendpath2 ,"%20" ," " ,0)
$editor=IniRead(@ScriptDir & "\imgedplus.ini", "imgedpath", "1", "default")
$image=$pendpath3
$valeditor=("""" & $editor & """")
$valimage=("""" & $image & """")
$command=($valeditor & " " & $valimage)
run($command)
exit
endfunc

func processweb()
$hBmp = _ClipBoard_GetData($CF_BITMAP )
$fucker=($cmdline[1])
$shitter=StringInStr($fucker, "/",0,-1)
$holycrap=StringTrimLeft ($fucker, $shitter)
$filenamepend=StringReplace($holycrap, " ", "-")
$clean1=StringReplace($filenamepend, "|", "-")
$clean2=StringReplace($clean1, ":", "-")
$clean3=StringReplace($clean2, "?", "")
$clean4=StringReplace($clean3, "/", "")
$clean5=StringReplace($clean4, "\", "")
$clean6=StringReplace($clean5, "*", "")
$clean7=StringReplace($clean6, ">", "")
$clean8=StringReplace($clean7, "<", "")
$clean9=StringReplace($clean8, "=", "")
$clean10=StringReplace($clean9, "$", "")
$clean11=StringReplace($clean10, "*", "")
$clean12=StringReplace($clean11, "&", "")
$clean13=StringReplace($clean12, "%20", "_")
$clean14=StringReplace($clean13, "@", "")
$clean15=StringReplace($clean14, "!", "")
$clean16=StringReplace($clean15, "%", "")
$imgname=StringReplace($clean16, "~", "")
$chkext=StringRight($imgname, 4)
$chkext2=StringRight($imgname, 5)

if $chkext=(".jpg") or $chkext=(".png") or $chkext=(".bmp") or $chkext=(".jpg") or $chkext2=(".jpeg") then
$filename=($imgname)

$finalname=StringReplace($clean8, "=", "-")
elseif $chkext=(".gif") then
$remext=stringtrimright($imgname, 4)
$filename=($remext & "-rg.png")
else
$remext=stringtrimright($imgname, 4)
$filename=($remext & ".jpg")
endif
_ScreenCapture_SaveImage (@tempdir & "\" & $filename, $hBmp)
$editor=IniRead(@ScriptDir & "\imgedplus.ini", "imgedpath", "1", "default")
$valeditor=("""" & $editor & """")
$image=(@tempdir & "\" & $filename)
$valimage=("""" & $image & """")
$command=($valeditor & " " & $valimage)
run($command)
exit
endfunc
