-
Notifications
You must be signed in to change notification settings - Fork 26
/
50_configure_filebot.sh
executable file
·161 lines (119 loc) · 4.75 KB
/
50_configure_filebot.sh
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
#!/bin/bash
function ts {
echo [`date '+%b %d %X'`]
}
#-----------------------------------------------------------------------------------------------------------------------
function initialize_configuration {
echo "$(ts) Creating /config/filebot.conf.new"
cp /files/filebot.conf /config/filebot.conf.new
if [ ! -f /config/filebot.conf ]
then
echo "$(ts) Creating /config/filebot.conf"
cp /files/filebot.conf /config/filebot.conf
chmod a+w /config/filebot.conf
fi
# Clean up carriage returns
tr -d '\r' < /config/filebot.conf > /tmp/filebot.conf
mv -f /tmp/filebot.conf /config/filebot.conf
# Create filebot.sh unless there is already one
if [ ! -f /config/filebot.sh ]
then
echo "$(ts) Creating /config/filebot.sh and exiting"
cp /files/filebot.sh /config/filebot.sh
exit 1
fi
}
#-----------------------------------------------------------------------------------------------------------------------
function check_filebot_sh_version {
USER_VERSION=$(grep '^VERSION=' /config/filebot.sh 2>/dev/null | sed 's/VERSION=//')
CURRENT_VERSION=$(grep '^VERSION=' /files/filebot.sh | sed 's/VERSION=//')
echo "$(ts) Comparing user's filebot.sh at version $USER_VERSION versus current version $CURRENT_VERSION"
if [ -z "$USER_VERSION" ] || [ "$USER_VERSION" -lt "$CURRENT_VERSION" ]
then
echo "$(ts) ERROR: The container's filebot.sh is newer than the one in /config."
echo "$(ts) Copying the new script to /config/filebot.sh.new."
echo "$(ts) Compare your filebot.sh and filebot.sh.new. Save filebot.sh to reset its timestamp,"
echo "$(ts) then restart the container."
cp /files/filebot.sh /config/filebot.sh.new
exit 1
fi
}
#-----------------------------------------------------------------------------------------------------------------------
function create_conf_and_sh_files {
# Create the config file for monitor.py
cat <<"EOF" > /files/FileBot.conf
if [[ -z "$INPUT_DIR" ]]
then
INPUT_DIR=/input
fi
if [[ -z "$OUTPUT_DIR" ]]
then
OUTPUT_DIR=/output
fi
EOF
tr -d '\r' < /config/filebot.conf >> /files/FileBot.conf
# Literal $INPUT_DIR
cat <<"EOF" >> /files/FileBot.conf
WATCH_DIR="$INPUT_DIR"
EOF
# Interpolate $USER_ID, $GROUP_ID, and $UMASK
cat <<EOF >> /files/FileBot.conf
COMMAND="bash /files/filebot.sh"
IGNORE_EVENTS_WHILE_COMMAND_IS_RUNNING=0
USER_ID=$USER_ID
GROUP_ID=$GROUP_ID
UMASK=$UMASK
EOF
# Strip \r from the user-provided filebot.sh
tr -d '\r' < /config/filebot.sh > /files/filebot.sh
chmod a+wx /files/filebot.sh
}
#-----------------------------------------------------------------------------------------------------------------------
function validate_configuration {
. /files/FileBot.conf
if [[ ! -d "$INPUT_DIR" ]]; then
echo "$(ts) INPUT_DIR=$INPUT_DIR does not exist"
exit 1
fi
if [[ ! -d "$OUTPUT_DIR" ]]; then
echo "$(ts) OUTPUT_DIR=$OUTPUT_DIR does not exist"
exit 1
fi
if find "$INPUT_DIR" -inum $(stat -c '%i' "$OUTPUT_DIR") 2>/dev/null | grep . 1>/dev/null 2>&1; then
echo "$(ts) OUTPUT_DIR=$OUTPUT_DIR can not be a subdirectory of INPUT_DIR=$INPUT_DIR"
exit 1
fi
}
#-----------------------------------------------------------------------------------------------------------------------
function setup_opensubtitles_account {
. /files/FileBot.conf
if [ "$OPENSUBTITLES_USER" != "" ]; then
echo "$(ts) Configuring for OpenSubtitles user \"$OPENSUBTITLES_USER\""
echo -en "$OPENSUBTITLES_USER\n$OPENSUBTITLES_PASSWORD\n" | /files/runas.sh $USER_ID $GROUP_ID $UMASK filebot -script fn:configure
else
echo "$(ts) No OpenSubtitles user set. Skipping setup..."
fi
}
#-----------------------------------------------------------------------------------------------------------------------
function configure_java_prefs {
. /files/FileBot.conf
echo "$(ts) Creating user for ID $USER_ID and group for ID $GROUP_ID if necessary"
# Running command as user "user_99_100"...
runas_user=$(/files/runas.sh $USER_ID $GROUP_ID $UMASK true | grep Running | sed 's/.*"\(.*\)".*/\1/')
mkdir -p /config/java_prefs /$runas_user/.java/.userPrefs/net
chown -R $USER_ID:$GROUP_ID /config/java_prefs /$runas_user/.java
rm -f /$runas_user/.java/.userPrefs/net/filebot
echo Before creating symlink
ls -al /config/java_prefs /$runas_user/.java/.userPrefs/net
ln -s /config/java_prefs /$runas_user/.java/.userPrefs/net/filebot
echo After creating symlink
ls -al /config/java_prefs /$runas_user/.java/.userPrefs/net
}
#-----------------------------------------------------------------------------------------------------------------------
echo "$(ts) Starting FileBot container"
initialize_configuration
check_filebot_sh_version
create_conf_and_sh_files
validate_configuration
setup_opensubtitles_account
configure_java_prefs