set drmfilename=%1
set drmoutfnm=%2
set drmmetafnm=%3

if exist pass.msg del pass.msg
if exist aesfile.key del aesfile.key
if exist kmvs.bin del kmvs.bin
if exist out-temp*.* del out-temp*.*
if exist xaa-kmvd-uuid.bin del xaa-kmvd-uuid.bin
if exist xaa_track2.amr del xaa_track2.amr
if exist xaa_track1.h264 del xaa_track1.h264
if exist xab.bin del xab.bin
if exist xac.bin del xac.bin
if exist out.mp4 del out.mp4
if exist xaa.mp4 del xaa.mp4
if exist tmp.mp4 del tmp.mp4
if exist input.mp4 del input.mp4

SplitKMV.exe %drmfilename%
if not exist xaa.mp4 goto END
AtomicParsley.exe xaa.mp4 --extract1uuid "kmvd"
if not exist e2p.key goto END
if not exist xaa-kmvd-uuid.bin goto noDRM
checkCA.exe xaa-kmvd-uuid.bin
if not exist pass.mgs goto END
if not exist aesfile.key goto END
YAMB\MP4BoxAES.exe -raw 1 xaa.mp4
YAMB\MP4Box.exe -raw 2 xaa.mp4

YAMB\MP4Box.exe -fps 15 -add "xaa_track1.h264" -add "xaa_track2.amr" -new input.mp4
if not exist %drmmetafnm% goto END
EditMetada.exe YAMB\MP4Box.exe input.mp4 %drmmetafnm% out.mp4
if not exist kmvs.bin goto END
AtomicParsley.exe out.mp4 --meta-uuid "kmvs" file kmvs.bin
if not exist out-temp*.* goto END
move /Y out-temp*.* out.mp4
copy /b out.mp4+fake.fst tmp3.kmv

:noDRM
if not exist xaa-kmvd-uuid.bin copy %drmfilename% tmp3.kmv
move /Y tmp3.kmv %drmoutfnm%

:END
del xaa.mp4
del out.mp4
del xab.bin

if exist pass.mgs del pass.mgs
if exist aesfile.key del aesfile.key
if exist kmvs.bin del kmvs.bin
if exist out-temp*.* del out-temp*.*
if exist xaa-kmvd-uuid.bin del xaa-kmvd-uuid.bin
if exist xaa_track2.amr del xaa_track2.amr
if exist xaa_track1.h264 del xaa_track1.h264
if exist tmp.mp4 del tmp.mp4
if exist input.mp4 del input.mp4