-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgotektool
206 lines (150 loc) · 5.11 KB
/
gotektool
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/bin/bash
# User-configurable parameters:
MOUNT="/mnt/tmp"
DEVICE="/dev/sdc"
# Read command line arguments:
SRCDIR=$1
FLOPPY=$2
MODE=$3
ORIGINALFLOPPY=$FLOPPY
FILE=1
ALREADYONNEXT=FALSE
# Show usage information if started without parameters:
if [ "$MODE" == "" ]; then
echo ""
echo " GotekTool - A Batch File Copier for Gotek USB Floppy Disk Emulators"
echo " Copyright 2017 Sebastian Mate"
echo ""
echo " Usage: gotektool SOURCEDIRECTORY STARTFLOPPY MODE"
echo ""
echo " SOURCEDIRECTORY: The base directory to be copied."
echo " STARTFLOPPY: The number of the first virtual floppy."
echo " MODE: The mode of file handling, which is:"
echo " 1 = Flatten directories and copy all files to the root directory of the virtual floppies."
echo " 2 = As 1, but also change floppy for each directory."
echo " 3 = Preserve original directory structures."
echo " 4 = Move contents from root directories to separate virtual floppies."
echo ""
echo " WARNING: This tool does erase data on the target device and is potentially dangerous."
echo " Use on your own risk!"
echo ""
exit 1
fi
# Translate command line arguments into settings:
if [ "$MODE" == "1" ]; then
KEEPFOLDER=FLATTEN
fi
if [ "$MODE" == "2" ]; then
KEEPFOLDER=FLATTEN
NEXTFLOPPY=ALLDIRS
fi
if [ "$MODE" == "3" ]; then
KEEPFOLDER=PRESERVE
fi
if [ "$MODE" == "4" ]; then
KEEPFOLDER=PRESERVE
NEXTFLOPPY=ROOTDIRS
fi
function nextFloppy { # Switch to the next virtual floppy
FILE=1
FLOPPY=`echo "$FLOPPY + 1" | bc`
FINALFLOPPY=$FLOPPY
echo ""
echo "" >> Contents.tmp
umount $MOUNT
sleep 2
mount -o loop,offset=$[$FLOPPY*1536]k,sizelimit=1440k,users,rw,umask=000 $DEVICE $MOUNT
sleep 2
rm -r $MOUNT/* 2> /dev/null
}
function copyFile { # Copy a file from $1 to $MOUNT/$2
PRETTYFLOPPY=$(printf "%03d" $FLOPPY)
echo -ne "$PRETTYFLOPPY: $2"
DirToMake=$(dirname "$2")
if [ ! -d "$DirToMake" ]; then
#echo "Creating directory" $DirToMake
mkdir -p "$MOUNT/$DirToMake"
fi
# return # uncomment this for debugging!
if ! cp "$1" "$MOUNT/$2" 2> /dev/null; then # No more space on virtual floppy left!
echo " [ERROR]"
# Delete the incomplete file on the target device:
rm "$MOUNT/$2" 2> /dev/null
# Continue with next floppy:
echo "=== No more space left, switching to next floppy ==="
nextFloppy
# Try to copy again:
if ! cp "$1" "$MOUNT/$2" 2> /dev/null; then # Still fails. The file is probably too big!
echo "Sorry, could not copy the file. Too big!"
rm "$MOUNT/$2" 2> /dev/null
else
PRETTYFLOPPY=$(printf "%03d" $FLOPPY)
echo "$PRETTYFLOPPY: $2 [SUCCESS]"
echo -e "$FLOPPY\t$FILE\t$f" >> Contents.tmp
fi
else
echo
echo -e "$FLOPPY\t$FILE\t$f" >> Contents.tmp
fi
}
# Delete original "Contents" (if present) file to start a new one:
rm Contents.tmp 2> /dev/null
# Mount the first virtual floppy:
mount -o loop,offset=$[$FLOPPY*1536]k,sizelimit=1440k,users,rw,umask=000 $DEVICE $MOUNT
# Delete all files on the virtual floppy:
rm -r $MOUNT/* 2> /dev/null
# Find and iterate over all files in the source directory:
find "$SRCDIR" -type f | sort --version-sort | while read f
do
FileNameOnly=$(basename "$f")
SourceDirectoryNameOnly=$(dirname "$f")
TargetDirectoryNameOnly="${SourceDirectoryNameOnly/$SRCDIR/}"
if [[ $TargetDirectoryNameOnly == /* ]]; then # remove leading "/" if present.
TargetDirectoryNameOnly=${TargetDirectoryNameOnly:1}
fi
if [ "$NEXTFLOPPY" == "ALLDIRS" ]; then
FlipDirectory=$TargetDirectoryNameOnly
fi
if [ "$NEXTFLOPPY" == "ROOTDIRS" ]; then
FlipDirectory=$(echo "$TargetDirectoryNameOnly" | cut -d "/" -f1)
TargetDirectoryNameOnly="${TargetDirectoryNameOnly/$FlipDirectory/}"
if [[ $TargetDirectoryNameOnly == /* ]]; then # remove leading "/" if present.
TargetDirectoryNameOnly=${TargetDirectoryNameOnly:1}
fi
fi
# Check if we need to change the "floppy":
if [ ! "$FILE" == "1" ]; then
if [ "$NEXTFLOPPY" == "ALLDIRS" ]; then
if [ ! "$LastFlipDirectory" == "$FlipDirectory" ]; then
echo "=== Directory '$LastFlipDirectory' processed, switching to next floppy ==="
nextFloppy
fi
fi
if [ "$NEXTFLOPPY" == "ROOTDIRS" ]; then
if [ ! "$LastFlipDirectory" == "$FlipDirectory" ]; then
echo " === Root directory '$LastFlipDirectory' processed, switching to next floppy ==="
nextFloppy
fi
fi
fi
# Copy the file:
if [ "$KEEPFOLDER" == "FLATTEN" ]; then
copyFile "$f" "$FileNameOnly"
fi
if [ "$KEEPFOLDER" == "PRESERVE" ]; then
if [ "$TargetDirectoryNameOnly" == "" ]; then
copyFile "$f" "$FileNameOnly"
else
copyFile "$f" "$TargetDirectoryNameOnly/$FileNameOnly"
fi
fi
# Remember the the directory base for the next interation:
LastFlipDirectory=$FlipDirectory
# Increase file counter for Contents file:
FILE=`echo "$FILE + 1" | bc`
done
# Unmount the virtual floppy:
umount $MOUNT
# Copy the temporary "Contents" file to the final one:
ORIGINALFLOPPY=$(printf "%03d" $ORIGINALFLOPPY)
mv Contents.tmp "Contents $ORIGINALFLOPPY.txt"