GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Git mirror of the CMS Made Simple 2.0 rewrite
Homepage: http://cmsmadesimple.org
Clone URL: git://github.com/tedkulp/cmsmadesimple-2-0.git
cmsmadesimple-2-0 / find-mime
100755 155 lines (131 sloc) 3.525 kb
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
#! /bin/bash
 
#CMS - CMS Made Simple
#(c)2004 by Leendert Meyer <leen.meyer@home.nl>
#This project's homepage is: http://cmsmadesimple.sf.net
#
#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.
#
#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.
#You should have received a copy of the GNU General Public License
#along with this program; if not, write to the Free Software
#Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
VERSION="${0##*/} Version 0.1"
 
USAGE="Usage: ${0##*/} -t|--text | -e|--exec | -b|--bin | -a|--all | -h|--help -v|--version -u|--usage [path]"
 
HELP="This script displays filenames with their mime-types.
 
You can use this to execute a command on each filename:
 
\$ ${0##*/} --exec | xargs -i svn propset svn:executable '' {}
 
\$ ${0##*/} --text | xargs -i svn propset svn:eol-style native {}
"
 
#---------------------------------------
 
function mime_of()
{
  # read filenames from stdin, 1 per line
  while read F; do
    # get the mime type
    M="`file --mime --brief \"$F\"`"
    # get only the first part, before ',' (comma) ...
    M="${M%,*}"
    # ... and ';' (semicol)
    M="${M%;*}"
    # return filename and semicol on stdout
    echo "$F: $M"
  done
}
 
#---------------------------------------
 
function find_mime_types()
{
  local dir="$1"
  # find files only, but not in .svn directories
  find "$dir" \( -name ".svn" -prune -or \( -type f -and -print \) \) |
  # remove './' if line starts with it (beautify)
  sed 's:^\./::' |
  # find the mime types
  mime_of
}
 
#---------------------------------------
 
# mime types of files that contain readable text
TEXT_MIME_TYPES=(
"text/.*"
"application/x-javascript"
"application/x-perl"
"application/x-shellscript"
)
 
# mime types of files that are executable
# perhaps add mime types of .com, .exe, .bat
# (then .bat belongs in TEXT_MIME_TYPES too)
EXEC_MIME_TYPES=(
"application/x-javascript"
"application/x-perl"
"application/x-shellscript"
)
 
 
EMPTY_MIME_TYPES=(
"application/x-empty"
)
 
# perhaps add ${EXEC_MIME_TYPES[*]} too?
NOT_BIN_MIME_TYPES="${TEXT_MIME_TYPES[*]} ${EMPTY_MIME_TYPES[*]}"
 
# set a default
_MIME_TYPES="${TEXT_MIME_TYPES[*]}"
 
#---------------------------------------
# main:
 
while [ $# -gt 0 ]; do
case $1 in
    -t|--txt|--text)
      _MIME_TYPES="${TEXT_MIME_TYPES[*]}"
    REV=""
    shift
    ;;
    -e|--exe|--exec)
      _MIME_TYPES="${EXEC_MIME_TYPES[*]}"
    REV=""
    shift
    ;;
    -b|--bin)
      _MIME_TYPES="${NOT_BIN_MIME_TYPES[*]}"
    REV="-v"
    shift
    ;;
    -a|--all)
      _MIME_TYPES=".*"
    shift
    ;;
    -u|--usage)
      echo "$USAGE"
    exit 1
    ;;
    -v|--version)
      echo "$VERSION"
    exit 1
    ;;
    -h|--help)
      echo "$VERSION"
    echo
    echo "$USAGE"
    echo
    echo "$HELP"
    exit 1
    ;;
    -*)
      echo "${0##*/}: Error: Unknown switch: $1"
    echo
    echo "$USAGE"
    exit 2
    ;;
    *)    break
    ;;
  esac
done
 
# egrep is used, so no need to escape special re chars
RE=": (${_MIME_TYPES// /|})$"
 
# find all mime types
find_mime_types "${1-.}" |
# filter out the ones we need
egrep $REV "$RE" |
# print only the file name
awk -F: '{print $1}'
 
#- EOF ---------------------------------