summaryrefslogtreecommitdiffstats
path: root/kapptemplate/kapptemplate.common
blob: 8cff2d60b3b93faab24d52516b382b47d41aabee (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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env bash
###########################################################################
#
# kapptemplate.common
#
# This file contains a bunch of common function that may be used by
# the various kapptemplate modules.  Each function lists which
# environment variables need to be set before the function is called
# and which variables will be set after it's done.  This file is
# intended to be included or sourced by any module that uses its
# functions.
#
###########################################################################

###########################################################################
#
# Function: Usage
#
# Display the help output (--help)
#
###########################################################################
function Usage
{
    $ECHO "Usage: kapptemplate [options]";
    $ECHO "Options:";
    $ECHO "    --help     Display this information";
    $ECHO "    --noinit   Don't run '$MAKE -f Makefile.cvs'";
    $ECHO "    --default  Use default values instead of prompting";
    $ECHO;
    $ECHO "Framework Types:";
    $ECHO "    --full-app      Create full featured KDE application";
    $ECHO "    --kpart-app     Create full KPart application";
    $ECHO "    --kpart-plugin  Create KPart plugin framework";
    $ECHO "    --existing      Convert existing source to KDE framework";
}

###########################################################################
#
# Function: ParseCommandLine
#
# Parse the commandline args (--help, --noinit, etc)
#
# OUTPUT: $NOINIT, $ALL_DEFAULTS, $WHICH_ONE
#
###########################################################################
function ParseCommandLine
{
    unset NOINIT;
    unset ALL_DEFAULTS;
    unset WHICH_ONE;
    if [ ! "$CMDLINE" ];
    then
        return;
    fi

    for CMD in $CMDLINE;
    do
        case $CMD in
        --help)
           Usage;
           exit 0;;

        --noinit)
           NOINIT=1;;
        --default)
           ALL_DEFAULTS=1;;
        --full-app)
           WHICH_ONE=1;;
        --kpart-app)
           WHICH_ONE=2;;
        --kpart-plugin)
           WHICH_ONE=3;;
        --existing)
           WHICH_ONE=4;;
        *)
           $ECHO "Unknown option $1";
           Usage;
           exit 1;;
        esac
    done
}

###########################################################################
#
# Function: GetProperName
#
# Get the application or class or plugin or whatever's proper name
#
# INPUT : $APPTYPE, $APPDEFAULT
# OUTPUT: $APP_NAME, $APP_NAME_UC, $APP_NAME_LC
#
###########################################################################
function GetProperName
{
    # Make sure we have reasonable defaults
    if [ ! "$APPTYPE" ];
    then
        APPTYPE="application";
    fi
    if [ ! "$APPDEFAULT" ];
    then
        APPDEFAULT="KMyApp";
    fi

    # See what we are getting
    if [ ! "$ALL_DEFAULTS" ];
    then
        unset APP_NAME;
        while [ ! $APP_NAME ];
        do
            $ECHO "What is the ${APPTYPE}'s proper name [default: ${APPDEFAULT}]";
            $ECHO ": \c";
            read APP_NAME;

            if [ ! $APP_NAME ];
            then
                APP_NAME=${APPDEFAULT};
                $ECHO "Going with default name '${APPDEFAULT}'";
            fi
        done
        $ECHO;
    else
        APP_NAME=${APPDEFAULT};
    fi

    # Create some variations
    APP_NAME_UC=`$ECHO $APP_NAME | tr a-z A-Z`;
    APP_NAME_LC=`$ECHO $APP_NAME | tr A-Z a-z`;
}

###########################################################################
#
# Function: GetVersion
#
# Get the application or class or plugin or whatever's version
#
# INPUT : $APPTYPE
# OUTPUT: $APP_VERSION
#
###########################################################################
function GetVersion
{
    # Make sure we have reasonable defaults
    if [ ! "$APPTYPE" ];
    then
        APPTYPE="application";
    fi

    # Get the application version
    if [ ! "$ALL_DEFAULTS" ];
    then
        unset APP_VERSION;
        while [ ! $APP_VERSION ];
        do
            $ECHO "What is the ${APPTYPE}'s version [default: 0.1]";
            $ECHO ": \c";
            read APP_VERSION;

            if [ ! $APP_VERSION ];
            then
                APP_VERSION="0.1";
                $ECHO "Going with default version '$APP_VERSION'";
            fi
        done
        $ECHO;
    else
        APP_VERSION="0.1";
    fi
}

###########################################################################
#
# Function: GetLocationRoot
#
# Get the directory where this all should be installed
#
# INPUT : $DEFAULT_ROOT, $APP_NAME_LC, $APP_VERSION
# OUTPUT: $LOCATION_ROOT
#
###########################################################################
function GetLocationRoot
{
    if [ ! "$ALL_DEFAULTS" ];
    then
        unset LOCATION_ROOT;
        while [ ! $LOCATION_ROOT ];
        do
            $ECHO "Where should I create this [default: $DEFAULT_ROOT/$APP_NAME_LC-$APP_VERSION]";
            $ECHO ": \c";
            read LOCATION_ROOT;

            if [ ! $LOCATION_ROOT ];
            then
                LOCATION_ROOT="$DEFAULT_ROOT/$APP_NAME_LC-$APP_VERSION";
                $ECHO "Going with default root '$LOCATION_ROOT'";
            fi

            # We overwrite anything if the directory already exists
            if [ -d $LOCATION_ROOT ];
            then
                $ECHO "This directory already exists.  Will overwrite.";
                $ECHO;
            fi
        done
        $ECHO;
    else
        LOCATION_ROOT="$DEFAULT_ROOT/$APP_NAME_LC-$APP_VERSION";
    fi
        
}

###########################################################################
#
# Function: GetAuthorName
#
# Get the name of the author
#
# INPUT: $DEFAULT_AUTHOR
#
###########################################################################
function GetAuthorName
{
    unset AUTHOR;
    while [ ! "$AUTHOR" ];
    do
        if [ "$ALL_DEFAULTS" -a "$DEFAULT_AUTHOR" ];
        then
            AUTHOR="$DEFAULT_AUTHOR";
        else
            if [ "$DEFAULT_AUTHOR" ];
            then
                $ECHO "What is your name [default: $DEFAULT_AUTHOR]";
            else
                $ECHO "What is your name [no default]";
            fi
            $ECHO ": \c";
            read AUTHOR;

            if [ "$DEFAULT_AUTHOR" -a ! "$AUTHOR" ];
            then
                AUTHOR="$DEFAULT_AUTHOR";
                $ECHO "Going with default author '$AUTHOR'";
            fi
        fi
    done
    $ECHO;
}

###########################################################################
#
# Function: GetAuthorEmail
#
# Get the email of the author
#
# INPUT: $DEFAULT_EMAIL
#
###########################################################################
function GetAuthorEmail
{
    unset EMAIL;
    while [ ! "$EMAIL" ];
    do
        if [ "$ALL_DEFAULTS" -a "$DEFAULT_EMAIL" ];
        then
            EMAIL="$DEFAULT_EMAIL";
        else
            if [ "$DEFAULT_EMAIL" ];
            then
                $ECHO "What is your email address [default: $DEFAULT_EMAIL]";
            else
                $ECHO "What is your email address [no default]";
            fi
            $ECHO ": \c";
            read EMAIL;

            if [ "$DEFAULT_EMAIL" -a ! "$EMAIL" ];
            then
                EMAIL="$DEFAULT_EMAIL";
                $ECHO "Going with default email address '$EMAIL'";
            fi
        fi
    done
    $ECHO;
}

###########################################################################
#
# Function: GetFileList
#
# Generic utility function to get a list of files in a directory
#
# INPUT : $DIRECTORY
# OUTPUT: $FILES
#
###########################################################################
function GetFileList
{
    unset FILES;
    if [ -d "$DIRECTORY" ];
    then
#        FILES=`/bin/ls -1 -I "no-exe" -I*~ $DIRECTORY`;
         FILES=`cd $DIRECTORY; find . ! -name "*~" -maxdepth 1 -type f -print | sed 's,^\./,,' | sort`;

        if [ ! "$FILES" ];
        then
            $ECHO "Cannot find files in $DIRECTORY!";
            exit 1;
        fi
    else
        $ECHO "$DIRECTORY directory does not exist!";
        exit 1;
    fi
}

###########################################################################
#
# Function: BuildModuleLists
#
# Build a list of files that correspond with each module
#
# INPUT : $SHARE_DIR
#
###########################################################################
function BuildModuleLists
{
    # Make sure all the modules exist first
    if [ ! -f "$INCLUDE_DIR/kapptemplate.module" ];
    then
        $ECHO "The 'kapptemplate.module' file can't be found.";
        $ECHO;
        exit 1;
    fi

    if [ ! -f "$INCLUDE_DIR/kpartplugin.module" ];
    then
        $ECHO "The 'kpartplugin.module' file can't be found.";
        $ECHO;
        exit 1;
    fi

    if [ ! -f "$INCLUDE_DIR/existing.module" ];
    then
        $ECHO "The 'existing.module' file can't be found.";
        $ECHO;
        exit 1;
    fi

    # Find the 'admin' files
    DIRECTORY=$SHARE_DIR/admin;
    GetFileList
    ADMIN_FILES=$FILES;

    # Find the 'appframework' files
    DIRECTORY=$SHARE_DIR/appframework;
    GetFileList
    APPFRAMEWORK_FILES=$FILES;

    # Find the 'kapp' files
    DIRECTORY=$SHARE_DIR/kapp;
    GetFileList
    KAPP_FILES=$FILES;

    # Find the 'kpartplugin' files
    DIRECTORY=$SHARE_DIR/kpartplugin;
    GetFileList
    KPARTPLUGIN_FILES=$FILES;

    # Find the 'existing' files
    DIRECTORY=$SHARE_DIR/existing;
    GetFileList
    EXISTING_FILES=$FILES;

    # Find the 'kpartapp' files
    DIRECTORY=$SHARE_DIR/kpartapp;
    GetFileList
    KPARTAPP_FILES=$FILES;
}

###########################################################################
#
# Function: CreateAppFramework
#
# Create the initial directory structure of the application or plugin
# and put in the common admin and configure files
#
# INPUT : $LOCATION_ROOT, $APP_NAME_LC, $SHARE_DIR
#
###########################################################################
function CreateAppFramework
{
    # Create the directory tree
    $ECHO;
    $ECHO "Creating directory $LOCATION_ROOT...";
    $MKDIR $LOCATION_ROOT || exit 1;

    $ECHO "Creating directory $LOCATION_ROOT/admin...";
    $MKDIR $LOCATION_ROOT/admin || exit 1;

    $ECHO "Creating directory $LOCATION_ROOT/$APP_NAME_LC...";
    $MKDIR $LOCATION_ROOT/$APP_NAME_LC || exit 1;

    $ECHO "Creating directory $LOCATION_ROOT/po...";
    $MKDIR $LOCATION_ROOT/po || exit 1;

    for FILE in $ADMIN_FILES;
    do
        $ECHO "Copying $LOCATION_ROOT/$FILE...";
        cp -p $SHARE_DIR/admin/$FILE $LOCATION_ROOT/admin/$FILE || exit 1;
    done

    for FILE in $APPFRAMEWORK_FILES;
    do
        . $SHARE_DIR/appframework/$FILE || exit 1;
    done
}

###########################################################################
#
# Function: GetInitialDefaults
#
# This is run the first time a user uses kapptemplate.  It gets a few
# default values and installs them into $HOME/.kapptemplate
# 
# INPUT: $KAPPTEMPLATEVERSION
#
###########################################################################
function GetInitialDefaults
{
  $ECHO "I see that this is your first time using KAppTemplate.  Excellent!";
  $ECHO "To get started, I need you to answer the following questions.  Your";
  $ECHO "answers will be used as the default values every time you use this";
  $ECHO "program in the future.";
  unset THE_AUTHOR;
  while [ ! "$THE_AUTHOR" ];
  do
    $ECHO "What is your full name? \c";
    if [ "$DEFAULT_AUTHOR" ];
    then
      $ECHO "[default: $DEFAULT_AUTHOR]";
    else
      $ECHO "[no default]";
    fi
    $ECHO ": \c";
    read THE_AUTHOR;

    if [ "$DEFAULT_AUTHOR" -a ! "$THE_AUTHOR" ];
    then
      THE_AUTHOR="$DEFAULT_AUTHOR";
    fi
  done
  $ECHO;

  unset THE_EMAIL;
  while [ ! "$THE_EMAIL" ];
  do
    $ECHO "What is your email address? \c";
    if [ "$DEFAULT_EMAIL" ];
    then
      $ECHO "[default: $DEFAULT_EMAIL]";
    else
      $ECHO "[no default]";
    fi
    $ECHO ": \c";
    read THE_EMAIL;

    if [ "$DEFAULT_EMAIL" -a ! "$THE_EMAIL" ];
    then
      THE_EMAIL="$DEFAULT_EMAIL";
    fi
  done
  $ECHO;

  unset THE_ROOT;
  $ECHO "Where should I construct apps? \c";
  if [ ! "$DEFAULT_ROOT" ];
  then
    DEFAULT_ROOT="$HOME/src";
  fi
  $ECHO "[default: $DEFAULT_ROOT]";
  $ECHO ": \c";
  read THE_ROOT;

  if [ ! "$THE_ROOT" ];
  then
    THE_ROOT="$DEFAULT_ROOT";
  fi
  $ECHO;

  $ECHO "Constructing .kapptemplaterc...";
  cat << ENDORC > $HOME/.kapptemplaterc
DEFAULT_AUTHOR="$THE_AUTHOR";
DEFAULT_EMAIL="$THE_EMAIL";
DEFAULT_ROOT="$THE_ROOT";
VERSION="$KAPPTEMPLATEVERSION";
ENDORC
}