summaryrefslogtreecommitdiffstats
path: root/extra/actions/Add\ Action?type?name!.sh
blob: 6a322bc19d34307c556d6d1f1e9d0052c5a4cd05 (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
#!/bin/sh


errecho() {
    echo ${@} >&2
}

check_dir() {
    DIR=${1}

    [ -d ${DIR} ] ||
	mkdir -p ${DIR} ||
	(echo "Can't create directory: ${DIR}" && exit 1)
}

populate_file() {
    FILE=${1}

    # Don't try to overwrite existing file
    test -e ${FILE} && return



     cat > ${FILE} << EOF
#!/bin/sh

# Selected files are stored here
FILES=\${@}

# You can interate over them one by one
for FILE in \${FILES}; do
    echo \$FILE
done

# Or process them all at once
echo "\${FILES}"
EOF
}


## Starting point

FILE=${1}
MIME=`hunter -m $FILE`
STATUS=$?


# MIME detection failed, bail out unless type is base
[ $STATUS != 0 ] && [ $type != "uni" ] &&
    echo $MIME &&
    exit 1

# Laziy not using XGD here because of OSX
ACTDIR="$HOME/.config/hunter/actions/"

MIME_BASE=`echo $MIME | cut -d "/" -f 1`
MIME_SUB=`echo $MIME | cut -d "/" -f 2`


case $type in
    uni)
	AFILE="${ACTDIR}/${name}.sh"
	check_dir "${ACTDIR}"
	populate_file "${AFILE}"
	$EDITOR "${AFILE}"
	test -e "${AFILE}" && chmod +x "${AFILE}"
	;;
    base)
	BASEDIR="${ACTDIR}/$MIME_BASE"
	AFILE="${BASEDIR}/${name}.sh"
	check_dir "${BASEDIR}"
	populate_file "${AFILE}"
	$EDITOR "${AFILE}"
	test -e ${AFILE} && chmod +x "${ACTDIR}/$name"
	;;
    sub)
	SUBDIR="${ACTDIR}/${MIME_BASE}/${MIME_SUB}"
	AFILE="${SUBDIR}/${name}.sh"
	check_dir ${SUBDIR}
	populate_file "${AFILE}"
	$EDITOR "${AFILE}"
	test -e ${AFILE} && chmod+ +x ${AFILE}
    ;;
esac