]> git.sesse.net Git - remoteglot-book/blobdiff - find-pgn-split-point.sh
Store and handle the PGN start position.
[remoteglot-book] / find-pgn-split-point.sh
diff --git a/find-pgn-split-point.sh b/find-pgn-split-point.sh
new file mode 100755 (executable)
index 0000000..b23bc33
--- /dev/null
@@ -0,0 +1,31 @@
+#! /bin/sh
+FILE="$1"
+P=$2
+NUM=$3
+
+split_point() {
+       FILE="$1"
+       P=$2
+       NUM=$3
+       SIZE=$( stat -c %s "$FILE" )
+
+       if [ "$P" -eq "0" ]; then
+               echo 0
+       elif [ "$P" -eq "$NUM" ]; then
+               echo $SIZE
+       else 
+               TENTATIVE_SPLIT_POINT=$(( SIZE * P / NUM ))
+               OFFS=$( tail -c +${TENTATIVE_SPLIT_POINT} "$FILE" | grep -b '^\[Event ' | head -n 1 | cut -d: -f1 )
+               if [ -z "$OFFS" ]; then
+                       echo $SIZE
+               else
+                       echo $(( TENTATIVE_SPLIT_POINT + OFFS ))
+               fi
+       fi
+}
+
+FROM=$( split_point "$FILE" $P $NUM )
+TO=$( split_point "$FILE" $(( P + 1 )) $NUM )
+SIZE=$(( TO - FROM ))
+
+tail -c +$FROM "$FILE" | head -c $SIZE