summarylogtreecommitdiffstats
path: root/mktemplate
blob: 336f5b8f0f960ab3c41c26cd310b92aafffd5cdd (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
#!/bin/bash -

declare -r PROGNAME=${0##*/}


function error() {
    printf "%s: %s\n" ${0##*/} "$@"
} >&2


# -----------------------------------------------------------------------
# Function to create temporary files
#   optional number of files to create--default is one
#   returns an array ($TEMP_FILE) containing temporary file names
# -----------------------------------------------------------------------
function makeTempFiles () {

    declare fnum=${1:-1} # number of temporary files to create

    # Use user's local tmp directory if it exists
    if [ -d ~/tmp ]; then
	TEMP_DIR=~/tmp
    else
	TEMP_DIR=/tmp
    fi

    # Temp files for this script, using paranoid method of creation to
    # ensure that file names are not predictable. This is for security to
    # avoid "tmp race" attacks.
    for (( n=0; n<$fnum; n++ )); do
	TEMP_FILE[$n]=$(mktemp -q "${TEMP_DIR}/${PROGNAME}.$$.XXXXXX")
	if [ "${TEMP_FILE[$n]}" = '' ]; then 
	    error 'cannot create temp file!'
            exit 1
	fi
    done
}

trap 'rm -f "${TEMP_FILE[*]}" >/dev/null 2>&1; exit' 0 1 2 3 13 15

function usage() {
cat >&2 << EOF
Usage: ${0##*/} filename
    filename: is the file to make into a template.
EOF
}


# ensure we have a real live file to work with
if ! [ "1" -eq "$#" ]; then
    error "required number of arguments is 1... number of arguments given: $#"
    usage
    exit 1
fi
if ! [ -e "$1" ]; then
    error "$1 does not exist"
    exit 1
fi
if ! [ -f "$1" ]; then
    error "$1 is not a regular file"
    exit 1
fi

# set base directory for template and .desktop file
declare -r DIR=$(kde4-config --localprefix)/share/templates # .desktop file directory
declare -r SOURCE="$DIR/.source"                            # template file directory

# make sure all required directories exist
if ! mkdir -p $SOURCE >/dev/null 2>&1 && ! [ -d $SOURCE ]; then
    error 'required directories cannot be created'
    exit 1
fi

# make sure the template and desktop files have sane names
declare -r FILE="$1"                                   # source file
declare saneName=$(sed 's/[[:space:]]/_/g'  <<< $FILE) # base name for template and desktop files
saneName=${saneName##*/}

# create .desktop filename
declare desktop=$DIR/$saneName
desktop=${desktop%.*}.desktop

# create template filename
declare template=$SOURCE/$saneName

# prompt for icon and menu entry text
icon=$(kdialog --title "Choose an Icon" --geticon Small MimeType 2>/dev/null)
if [ -z "$icon" ]; then
    error 'no icon selected'
    exit 1
fi
filetype=$(kdialog --title "Menu and Dialog Prompt Text" --inputbox "Document type:" "$(file "$FILE" | awk -F: '{print $2}' | awk -F, '{print $1}')" 2>/dev/null)
if [ -z "$filetype" ]; then
    error 'no prompt text entered'
    exit 1
fi

# create .desktop file content
makeTempFiles
cat > ${TEMP_FILE[0]} << EOF
[Desktop Entry]
Name=$filetype...
Comment=New $filetype...
Type=Link
URL=.source/$saneName
Icon=$icon
EOF


# install template and .desktop file
cp ${TEMP_FILE[0]} $desktop
cp "$FILE" $template

# report errors
if ! [ -e $desktop ]; then
    kdialog --error "$desktop could not be created"
fi
if ! [ -e $template ]; then
    kdialog --error "$template could not be created"
fi