aboutsummaryrefslogtreecommitdiff
path: root/nx-X11/programs/Xserver/XpConfig/C/print/models/PSspooldir/spooltodir.sh
blob: aba14e1b3ca026d75d576fbc2a0faa4411d08440 (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
#!/bin/sh
PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH

verbose_msgs="false"
DEFAULT_SPOOLDIR=/tmp/Xprintjobs

usage()
{
    printf "Usage: ${0}: [options]\n"
    printf "-v\tbe verbose\n"
    printf "-d dirname\tdefine spool dir\n"
    printf "-p string\tname of printer selected by user\n"
    printf "-c integer\tnumber of copies\n"
    printf "-t string\tjob title\n"
    printf "-s string\tfile name suffix\n"
    printf "-o string\tspooler options\n"
    printf "-u mask\tpermission mask for new files (see umask)\n"
    exit 2
}

verbose()
{
    if ${verbose_msgs} ; then
        echo "$1"
    fi
}

spooldir="${DEFAULT_SPOOLDIR}"
printername=
num_job_copies=
job_title=
filename_suffix=
spooler_options=
permmask=
while getopts va:b:d:p:c:t:s:o:u: i
do
    case $i in
        v)  
            verbose_msgs="true"
            ;;
        d)  
            spooldir="$OPTARG"
            ;;
        p)  
            printername="$OPTARG"
            ;;
        c)  
            num_job_copies="$OPTARG"
            ;;
        t)  
            job_title="$OPTARG"
            ;;
        s)  
            filename_suffix="$OPTARG"
            ;;
        o)  
            spooler_options="$OPTARG"
            ;;
        u)  
            permmask="$OPTARG"
            ;;
        ?)  usage
            ;;
    esac
done

verbose "# spooldir=\"$spooldir\""
verbose "# printername=\"$printername\""
verbose "# num_job_copies=\"$num_job_copies\""
verbose "# job_title=\"$job_title\""
verbose "# spooler_options=\"$spooler_options\""
verbose "# umask=\"$permmask\""

if [ ! -d "${DEFAULT_SPOOLDIR}" ] ; then 
  mkdir "${DEFAULT_SPOOLDIR}"
  chmod a+rwxt "${DEFAULT_SPOOLDIR}"
fi

if [ "${permmask}" != "" ] ; then
    umask ${permmask}
fi

if [ ! -d "$spooldir" ] ; then
    echo "$0: spooldir \"$spooldir\" does not exits." >&2
    exit 1
fi
if [ ! -w "$spooldir" ] ; then
    echo "$0: Cannot write to spooldir \"$spooldir\"." >&2
    exit 1
fi

# Create first part of the output file name (prefix and an "unique"
# id(=date and time))...
filename="Xpjob_`date +%Y%m%d%H%M%S`"

# ... then add options ...
if [ "${printername}" != "" ] ; then
    filename="${filename}_${printername}"
fi
if [ "${num_job_copies}" != "" -a "${num_job_copies}" != "1" ] ; then
    filename="${filename}_copies_${num_job_copies}"
fi
if [ "${job_title}" != "" ] ; then
    filename="${filename}_title_${job_title}"
fi

# ... mangle output file name and filter chars (like whitespaces)
# which may screw-up further processing by other shell scripts ...
filename="`echo \"${filename}\" | tr '[:blank:]' '_' | tr -c -d '[:alnum:]_.-'`"

# ... add path and suffix ...
filename="${spooldir}/${filename}${filename_suffix}"

verbose "# File name is \"$filename\"."

# ... and finally capture stdin to the file.
cat >"${filename}"

if ${verbose_msgs} ; then
    printf "# File is " ; ls -l "${filename}"
fi

verbose "# Done."

exit 0
# EOF.