summaryrefslogtreecommitdiff
path: root/scripts/transfer.sh
blob: 3e73b65b84c200f7933ceb736e51683c341b4e71 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/bin/bash
PORT=$1
lname=$2
rname=$3

cat $PORT &
PRINTER_PID=$!
trap "kill $PRINTER_PID; exit" SIGINT SIGTERM

# interrupt
echo -e -n "\003" >$PORT
# Enter RAW Repl
echo -e -n "\001" >$PORT
sleep 0.5


fsize=$(du -b $lname | cut -f 1)
echo "Size of $lname $fsize"
blocksize=200
blocks=$(( $fsize / $blocksize ))
pos=0
echo -e -n "__fh = open('$rname', 'wb')\n" >$PORT
echo -e -n "written = 0\n\004" >$PORT
sleep 0.1

echo -e -n "\003" >$PORT
while [[ $pos -lt $fsize ]]; do
    encoded=$(dd if=$lname ibs=1 skip=$pos count=$blocksize 2>/dev/null | python3 -c 'import sys; print(repr(sys.stdin.buffer.read()), end="")')
	echo -n "$pos..$(( $pos + $blocksize )) "
    echo -n "written += __fh.write(${encoded})" > $PORT
    echo -e -n "\n\004" > $PORT
	pos=$(( $pos + $blocksize ))
	sleep 0.08
done
echo -e -n "__fh.close()\004" >$PORT
echo -e -n "del(__fh)\004" >$PORT
sleep 0.2

echo -e -n "print('\\\\n\\\\n%s bytes written to file $rname\\\\n' % written)\004" >$PORT

# enter normal repl
echo -e -n "\002" >$PORT

sleep 0.2

kill $PRINTER_PID