summaryrefslogtreecommitdiff
path: root/bin/play
blob: e88eb63e0187f5afe19b7c0de25ea3faee10ebd2 (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
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
#!/bin/bash

# bash script to play files from a slow network ressource, FTP folder, samba share or network drive etc.
# caches the file in /tmp and moves to $ARCHIV
# -y to skip verification
# export global variable $ARCHIV
# set the exit code right for quitting
# echo "q quit 255" >> ~/.mpv/input.conf

printhelp(){

	echo "Bash script to play and cache files from a slow network ressource"
	echo ""
	echo " -d   only remove the file in /tmp"
	echo " -g   don't ask for downloading"
	echo " -h   prints this help"
	echo " -n   don't ask for moving"
	echo " -y   move to archiv without question"
	echo " -o   ask for a different archiv"
	echo " -no-cache   Not caching the file, instead play's directly"

	exit 0
}

cache=1

while true; do
	case "$1" in 
		-no-cache|-nc)		cache=0
					shift
					;;
		-y|-Y|-yes|-j|-ja)	verify="yes"
					shift
					;;
		-r|-rm|-remove) 	verify="rm"
					shift
					;;
		-o|-other|-ot)		verify="other"
					shift
					;;
		-n|-no|-nope|-nein)	verify="no"
					shift
					;;
		-d|-download|-down)	download="yes"
					shift
					;;
		-h|--help)		printhelp
					;;
		*)			break
					;;
	esac
done

if [ $# -eq 0 ]; then
	printhelp
fi

if [ $# -gt 1 ]; then
	echo "Do not commit more than one file."
	exit 1
fi

if [ ! -f "$1" ]; then
	echo "No valid file input."
	exit
fi

player[0]=$(command -v mpv) 		#media player of choice
player[1]=$(command -v mplayer)
player[2]=$(command -v vlc)		#you shouldn't use vlc

for i in "${player[@]}"; do
	if [ ! $i = "" ]; then
		player=$i
		break
	fi
done

if [ ! $player ]; then
	echo "No media player found. Please edit this script manually. I recommend the mpv. http://mpv.io"
	exit 1
fi

if [ $cache -gt 0 ]; then

	if [ ! $ARCHIV ]; then
		echo "Note: Variable \$ARCHIV couldn't be found. It will play the file fine, but you should consider to organize your collection."
		read -p "You can export it temporarily. If you do, just type the path to the folder, if you don't, leave it empty. " path
		if [ "$path" ] && [ -d "$path" ] && [ -w "$path" ]; then
			export ARCHIV="$path"
			echo "Roger that!"
		fi
	fi

	pwd=$(pwd)

	if [ -d /tmp ] && [ -w /tmp ]; then
		tmp="/tmp"
	else
		read -p "Please name the directory to cache the file: " tmp
		if [ ! -d "$tmp" ] || [ ! -w "$tmp" ]; then
			echo "Directory doesn't exist or you don't have write permission."
			exit 1
		fi
	fi


	if [[ "$@" =~ ^https?://.+\.[a-z]{2}[a-z]* ]]; then
		read -p "Detected hyperlink. Should we try to download (caching)? Type something random to play direct with the mpv. [y/N] " download
		if [[ $download = "ja" ]] || [[ $download = "yes" ]] || [[ $download = "j" ]] || [[ $download = "y" ]] || [[ $download = "Y" ]]; then
			cd "$tmp"
			wget -q -O "file" "$@" & 
			echo "Starting download. Ready to play in 2 sec."
			sleep 2
			mpv $file
			echo "TODO: Moving to archiv. Not implemented yet."
			exit 0
		elif [[ $player =~ mpv$ ]] && [ $download ] && ( [ ! $download = "n" ] && [ ! $download = "N" ] && [ ! $download = "no" ] && [ ! $download = "nein" ] && [ ! $download = "NO" ]); then
			echo "Playing directly using mpv."
			mpv "$@"
			if [ $? -eq 0 ]; then
				echo "Have a nice day!"
				exit 0
			else
				echo "Something is broken with the media player!"
				exit 1
			fi
		else
			echo "This script is designed to cache media files from a FTP share or network drive. Exiting. "
			exit 1
		fi
	else
		file=$(basename "$@")
		if [ -f "$tmp/$file" ]; then
			skip=true
		fi
		cp -u -i "$@" "$tmp" &
		cd "$tmp"
		#file=${@##*/}

		if [ ! $skip ]; then
			echo -n "Preparing the awesome! Starting playback in.."
			echo -n " 2,"
			sleep 1
			echo -n " 1 "
			sleep 1
		fi

		echo "Start!"
	fi
else
	echo -n "Preparing the awesome! Starting playback in.."
	file="$@"
	echo -n "Now!" 
fi

$player "$file"
if [ $? -eq 0 ]; then 
	if [ -d $ARCHIV ] && [ -w $ARCHIV ] && [ $ARCHIV ]; then
		echo "Finished $file succesfull."
		if [ ! $verify ]; then
			read -p "Should we move everything to the archiv? ($ARCHIV) [Y/n/rm/other] " verify
		fi
		if [ ! $verify ] || [ $verify = "ja" ] || [ $verify = "yes" ] || [ $verify = "j" ] || [ $verify = "y" ] || [ $verify = "Y" ]; then
			echo "Moving the stuff to $ARCHIV."
			mv -v "$file" "$ARCHIV"
			if [ $? -eq 0 ]; then
				cd "$pwd"
				rm -v "$@"
			fi
		elif [ $verify = "other" ] || [ $verify = "o" ] || [ $verify = "O" ] || [ $verify = "OTHER" ]; then
			read -p "Please tell us the absolute path: " path
			if [ -d $path ] && [ -w $path ]; then
				echo "Moving to $path."
				mv -v "$file" "$path"
				if [ $? -eq 0 ]; then
					cd "$pwd"
               	                       	rm -v "$@"
                       	       	fi
			else
				echo "Sorry, \"$path\" doesn't exist."
				exit 1
			fi
		elif [ $verify = "r" ] || [ $verify = "rm" ] || [ $verify = "remove" ] || [ $verify = "RM" ] || [ $verify = "R" ]; then
			echo "Cleaning the mess up. Only removing files from $tmp."
			rm -v "$file"
		else
			echo "Okay, don't touching the files."		
		fi
	else
		echo "Can't move to \"$ARCHIV\". Directory doesn't exist or you don't have write permission."
		exit 1
	fi
else
	echo "Something is broken with the media player!"
	exit 1
fi

echo "Have a nice day!"
exit 0