#!/usr/bin/env zsh

dirname "$1" | read -r dir
basename "$1" | read -r name
sed -re 's/.* - ([0-9]+).*/\1/' <<< "$name" | read -r num
targetdir="$dir/.."
targetname="[FGT] SKRS - $num.ass"
# echo "$dir/$name"
# echo "$targetdir/$targetname"
# exit

target="$targetdir/$targetname"
echo Writing "$target"
<"$1" sed -rf <(<< EOF
/^Style:.*-furigana,/ d
# /^Comment:/ d
# /^Dialogue:.*,(Default|Alt),/ d
/^Dialogue:.*,(Hybrid|Tenebris)-signs,/ d
/^Dialogue:.*,Insert/ d
/^Dialogue:.*,([bB]oth|Futaba|Kaoru|Hikari2?|Nana2?|Suns|Stars|Karen2?|TENDOMAYA|Several|Junna|Mahiru|Kaoruko|Badgirl team|Hero team|whatshername2?|Claudine|Duet|End|bluecoat),,/ d
/^Dialogue:.*,(Kaoru|Futaba|Both),(Kaoru|Futaba|Both),/ d
/^Dialogue:.*,(JapOP|EngOP),/ d
/^Dialogue:.*,(English|Romaji),/ d
EOF
) > "$target"

target="$targetdir/${targetname%%.ass}_typeset.ass"
echo Writing "$target"
<"$1" sed -rf <(<< EOF
/^Style:.*-furigana,/ d
/^Comment:/ d
/^Dialogue:.*,(Default|Alt),/ d
# /^Dialogue:.*,(Hybrid|Tenebris)-signs,/ d
/^Dialogue:.*,Insert/ d
/^Dialogue:.*,([bB]oth|Futaba|Kaoru|Hikari2?|Nana2?|Suns|Stars|Karen2?|TENDOMAYA|Several|Junna|Mahiru|Kaoruko|Badgirl team|Hero team|whatshername2?|Claudine|Duet|End|bluecoat),,/ d
/^Dialogue:.*,(Kaoru|Futaba|Both),(Kaoru|Futaba|Both),/ d
/^Dialogue:.*,(JapOP|EngOP),/ d
/^Dialogue:.*,(English|Romaji),/ d
EOF
) > "$target"

target="$targetdir/${targetname%%.ass}_insert.ass"
echo Writing "$target"
<"$1" sed -rf <(<< EOF
/^Style:.*-furigana,/ d
/^Comment:/ d
/^Dialogue:.*,(Default|Alt),/ d
/^Dialogue:.*,(Hybrid|Tenebris)-signs,/ d
# /^Dialogue:.*,Insert/ d
# /^Dialogue:.*,([bB]oth|Futaba|Kaoru|Hikari2?|Nana2?|Suns|Stars|Karen2?|TENDOMAYA|Several|Junna|Mahiru|Kaoruko|Badgirl team|Hero team|whatshername2?|Claudine|Duet|End|bluecoat),,/ d
# /^Dialogue:.*,(Kaoru|Futaba|Both),(Kaoru|Futaba|Both),/ d
/^Dialogue:.*,(JapOP|EngOP),/ d
/^Dialogue:.*,(English|Romaji),/ d
EOF
) > "$target"

target="$targetdir/${targetname%%.ass}_ED.ass"
echo Writing "$target"
<"$1" sed -rf <(<< EOF
/^Style:.*-furigana,/ d
/^Comment:/ d
/^Dialogue:.*,(Default|Alt),/ d
/^Dialogue:.*,(Hybrid|Tenebris)-signs,/ d
/^Dialogue:.*,Insert/ d
/^Dialogue:.*,([bB]oth|Futaba|Kaoru|Hikari2?|Nana2?|Suns|Stars|Karen2?|TENDOMAYA|Several|Junna|Mahiru|Kaoruko|Badgirl team|Hero team|whatshername2?|Claudine|Duet|End|bluecoat),,/ d
/^Dialogue:.*,(Kaoru|Futaba|Both),(Kaoru|Futaba|Both),/ d
/^Dialogue:.*,(JapOP|EngOP),/ d
# /^Dialogue:.*,(English|Romaji),/ d
EOF
) > "$target"
