summaryrefslogtreecommitdiffstats
path: root/r14-xdg-update
blob: de395e87f7bdf63531fe599a76a3488e23abf200 (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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#!/bin/sh
#
# A script to perform R14.0.0 XDG compliance updates.

# This script should be needed to run only once, but corner cases
# and file/directory permissions could cause incomplete updates.

# TODO: How to handle environments where files/directories are locked
#       administratively or are owned by root and can't be updated.
#       The nominal validation checks in this script provide some notice
#       but no direct remedy.

# TODO: How to update profile directories not named $HOME/.trinity and $TDEHOME
#       is not yet declared when running this script.

Wait_For_Response () {
unset response
# -r Backslash does not act as an escape character.
# -p Display "PROMPT" without a trailing newline, before attempting to read any input.
while true; do
  read -r -p "$1 (y/n): " yn
  case $yn in
    [Yy]* ) response=y; break;;
    [Nn]* ) response=n; break;;
    * ) echo "Please answer yes (y/Y) or no (n/N).";;
  esac
done
}

Proceed_From_Response () {
if [ "$response" = "n" -o "$response" = "N" ]; then
  echo "Exiting."
  echo
  exit 0
else
  echo "Continuing."
  echo
fi
}

Display_Message () {
if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
  printf "%b" "$MESSAGE" | xmessage -center -file - > /dev/null 2>/dev/null
else
  printf "%b" "$MESSAGE"
fi
}

Message_Prefix () {
if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
  printf "%s" "[r14-xdg-update] "
fi
}

Validation_Failure () {
Display_Message "$MESSAGE"
if [ "$KDEGLOBALS_KEY_VALUE" = "" ]; then
  KDEGLOBALS_KEY_VALUE="$TEST_NUM"
else
  KDEGLOBALS_KEY_VALUE="$KDEGLOBALS_KEY_VALUE;$TEST_NUM"
fi
}

# Main script:

SCRIPT_NAME="`basename \`readlink -f $0\``"

# Allow forced execution of this script regardless of the kdeglobals setting
# and allow passing a user home directory as a positional parameter.
if [ "$#" -eq "2" ]; then
  if [ "$1" = "force" ] || [ "$2" = "force" ]; then
    FORCE="true"
  fi
  if [ "$1" != "force" ]; then
    USER_DIR="$1"
  elif [ "$2" != "force" ]; then
    USER_DIR="$2"
  fi
elif [ "$#" -eq "1" ]; then
  if [ "$1" = "force" ]; then
    FORCE="true"
  else
    USER_DIR="$1"
  fi
fi

unset KDEGLOBALS_KEY_VALUE

WARNING_MESSAGE="Trinity R14 XDG compliance updates will not be performed.\n\nWithout R14 XDG compliance updates, some Trinity apps will fail to\nfunction properly.\n\nFailures include the following:\n\n* Many left-side icon lists will not populate,\n  such as the Panel and Konqueror configuration dialogs.\n\n* User-defined keyboard shortcuts fail (khotkeysrc).\n  System defined shortcuts remain functional.\n\n* User-defined app preferences fail (profilerc).\n\n* Konqueror navigation/sidebar panel won't open.\n\n* User-defined konqueror service menus, kicker customization,\n* konqueror sidebar, Recent Documents list fail.\n\nPlease exercise appropriate action.\n"

# As the user should not be logged into a Trinity session when running
# this script, or an administrator might run this script remotely, the
# $TDEHOME variable might not be set or knowable from within this script.
# We presume $USER_DIR/.trinity and provide a way to pass an environment
# variable to change that location.
USER_DIR=${USER_DIR:-"$HOME"}
if [ "$USER_DIR" != "$HOME" ]; then
  PROFILE_DIR=${PROFILE_DIR:-"$USER_DIR/.trinity"}
elif [ "$TDEHOME" = "" ]; then
  PROFILE_DIR=${PROFILE_DIR:-"$USER_DIR/.trinity"}
else
  PROFILE_DIR="$TDEHOME"
fi
if [ ! -d "$PROFILE_DIR" ]; then
  MESSAGE="Warning! Unable to find the user profile directory $PROFILE_DIR.\n\nCheck permissions, paths, and typing.\n\n${WARNING_MESSAGE}"
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
    if [ "$?" = "102" ]; then
      # User selected the Quit button: quit this script.
      unset PROFILE_DIR
    fi
  else
    Message_Prefix
    printf "%b" "$MESSAGE"
  fi
  exit 1
fi

# The binaries for TDE are located in the same place as this script.
# To determine that location use the following method rather than presuming
# the existence of $TDEDIR. That environment variable might not be
# defined or defined to point to KDE4 binaries.
BIN_DIR="`dirname \`readlink -f $0\``"
if [ -x $BIN_DIR/tde-config ]; then
  TDEDIR=${BIN_DIR%/bin}
else
  MESSAGE="Unable to determine the TDE bin directory, where this script should be installed."
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
    if [ "$?" = "102" ]; then
      # User selected the Quit button: quit this script.
      unset PROFILE_DIR
      exit 1
    fi
  else
    Message_Prefix
    printf "%b" "$MESSAGE\n\n${WARNING_MESSAGE}"
  fi
  exit 1
fi
unset BIN_DIR

Message_Prefix
echo "Performing a profile update for Trinity release R14 XDG compliance."
Message_Prefix
echo "To run this script against a different user directory, or automated"
Message_Prefix
echo "from within another script, pass the directory path as a parameter."
Message_Prefix
echo "For example: r14-xdg-update /home/user_dir"
Message_Prefix
echo "Use the user home directory and not the profile directory."
Message_Prefix
echo "User directory: $USER_DIR"
Message_Prefix
echo "Profile directory: $PROFILE_DIR"
if [ "$USER_DIR" != "$HOME" ]; then
  Message_Prefix
  echo "Root (admin) privileges might be required to run this script"
  Message_Prefix
  echo "against other user directories."
  Message_Prefix
  echo "This script is being run against $USER_DIR."
  Message_Prefix
  echo "Your normal user directory is $HOME."
fi

# Do not update when $TDEHOME is a sym link to another profile directory. Trinity should have
# full reign within its own profile directory (limited to administrative locking), but the error
# check is a conservative approach.
TDEHOME_LINK="`readlink \"$PROFILE_DIR\"`"
if [ "$TDEHOME_LINK" != "" ]; then
  # Force this entry to ensure the updates eventually are performed should the user copy the
  # original kdeglobals file into a new Trinity profile.
  # $TDEDIR/bin/kwriteconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated --type bool 'false'
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    echo "[r14-xdg-update] Warning! The profile directory $PROFILE_DIR is a" 1>&2
    echo "                 sym link to $TDEHOME_LINK!" 1>&2
    echo "                 R14 updates will not be performed because Trinity needs its own" 1>&2
    echo "                 separate profile directory." 1>&2
    echo "                 Without R14 updates some Trinity apps will fail to function correctly." 1>&2
  fi
  MESSAGE="Oops! The profile directory $PROFILE_DIR is a sym link to $TDEHOME_LINK.\n\n${WARNING_MESSAGE}\nPossible remedies:\n\n* Contact your system administrator.\n\n* Break the sym link to force creating a fresh Trinity profile.\n\n* Use the migratekde3 script to migrate a KDE3 profile to Trinity."
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    MESSAGE="${MESSAGE}\n\nSelecting the Continue button means retaining the KDE3 profile and\nbreaking the sym link. With the sym link broken, run the migratekde3\nscript before restarting Trinity to migrate a KDE3 profile or\nallow Trinity to create a fresh profile."
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
    EXIT_CODE="$?"
    unset TDEHOME_LINK
    if [ "$EXIT_CODE" = "102" ]; then
      # User selected the Quit button: quit this script and exit X.
      unset PROFILE_DIR
      Message_Prefix
      echo "The user chose to quit."
      exit 1
    else
      # User selected the Continue button: continue this script and start TDE.
      Message_Prefix
      echo "The user chose to continue, which will break the sym link."
      BREAK_SYMLINK="true"
    fi
  else
    echo
    printf "%b" "$MESSAGE"
    echo
    Wait_For_Response "Break the sym link now?"
    Proceed_From_Response
    BREAK_SYMLINK="true"
  fi
  if [ "$BREAK_SYMLINK" = "true" ]; then
    unlink "$USER_DIR/.trinity" 2>/dev/null
    if [ "`readlink \"$USER_DIR/.trinity\"`" = "" ]; then
      MESSAGE="Sym link broken. With the sym link broken, run the migratekde3\nscript before restarting Trinity to migrate a KDE3 profile or\nallow Trinity to create a fresh profile."
      if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
        Message_Prefix
      fi
      printf "%b" "$MESSAGE"
      echo
    else
      MESSAGE="Unable to break the sym link. Check file and directory privileges. Quitting."
      if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
        Message_Prefix
      fi
      echo "$MESSAGE"
      echo
    fi
  fi
  unset PROFILE_DIR
  unset TDEHOME_LINK
  exit 1
fi
unset TDEHOME_LINK

R14_UPDATED="`$TDEDIR/bin/kreadconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated`"
if [ "$R14_UPDATED" != "true" ] || [ "$FORCE" = "true" ]; then
  if [ "$R14_UPDATED" != "true" ] && [ "$R14_UPDATED" != "false" ] && [ "$R14_UPDATED" != "" ]; then
    Message_Prefix
    echo "The r14-xdg-update script has been run at least once."
    Message_Prefix
    echo "The error code is $R14_UPDATED."
    echo
    MESSAGE="The r14-xdg-update script has been run at least once.\n\nThe script is not successfully updating.\n\nThe script will run with each login until corrected.\n\nPlease contact an administrator or take appropriate\nadmininstrative action to correct the problem.\n\nThe error code is $R14_UPDATED."
    # Are we in X? Display an X dialog explaining breakage.
    if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
      printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
      if [ "$?" = "102" ]; then
        # User select the Quit button: quit this script.
        unset PROFILE_DIR
        unset R14_UPDATED
        exit 1
      fi
    fi
  fi
else
  echo "This script has been run at least once previously. To run manually pass the 'force' parameter."
  exit 0
fi

# Trap when the user runs this script while in a Trinity session.
# Most files can be updated "live" but some can't, such as kdeglobals.
if [ "$USER_DIR" = "$HOME" ]; then
  if [ "$TDE_FULL_SESSION" != "" ] || [ "$TDE_SESSION_UID" != "" ]; then
    MESSAGE="You are running this script from while a Trinity session is active.\n\nMost files can be updated \"live\" but some cannot, such as kdeglobals.\n\nThis script might complete successfully and might not."
    Message_Prefix
    printf "%b" "$MESSAGE\n\n${WARNING_MESSAGE}"
    Wait_For_Response "Continue?"
    Proceed_From_Response
  fi
fi

Message_Prefix
echo "Updating temp file locations."
# All three directories are for temporary files. The cache directory is
# intended for persistent temporary data (is expected to remain across reboots
# and shutdowns). The other two directories are for non-persistent data and
# can be deleted across reboots and shutdowns.
CACHE_DIR="`readlink $PROFILE_DIR/cache-\`uname -n\``"
SOCKET_DIR="`readlink $PROFILE_DIR/socket-\`uname -n\``"
TMP_DIR="`readlink $PROFILE_DIR/tmp-\`uname -n\``"
# Delete the non-persistent temporary directories. This is safe at any time.
unlink $PROFILE_DIR/socket-`uname -n` 2>/dev/null
unlink $PROFILE_DIR/tmp-`uname -n` 2>/dev/null
if [ "$SOCKET_DIR" != "" ]; then
  rm -fr $SOCKET_DIR 2>/dev/null
fi
if [ "$TMP_DIR" != "" ]; then
  rm -fr $TMP_DIR 2>/dev/null
fi
# Remember that this script may be run more than once. The new directory
# might already exist.
if [ -n "$CACHE_DIR" ] && [ -d "$CACHE_DIR" ]; then
  # Flush the obsolete ksycoca cache files.
  rm -f ${CACHE_DIR}/ksycoca* 2>/dev/null
  # Flush the tdesycoca cache files. This is safe at any time.
  rm -f ${CACHE_DIR}/tdesycoca* 2>/dev/null
  # Old cache directory: /var/tmp/kde*cache-$USER
  # New cache directory: /var/tmp/tdecache-$USER
  # Rename/move the directory name but only when the new name does not exist.
  if [ "`echo $CACHEDIR | grep tdecache`" = "" ]; then
    Message_Prefix
    echo "Renaming the temporary cache directory."
    unlink $PROFILE_DIR/cache-`uname -n` 2>/dev/null
    mv -f $CACHE_DIR `dirname $CACHE_DIR`/tdecache-$USER 2>/dev/null
    Message_Prefix
    echo "Creating a sym link for the temporary cache directory."
    ln -s `dirname $CACHE_DIR`/tdecache-$USER $PROFILE_DIR/cache-`uname -n`
  fi
fi
# Housekeeping: the old locations are no longer needed.
if [ "$CACHE_DIR" != "" ]; then
  rm -fr `dirname 2>/dev/null $CACHE_DIR`/kde*cache-$USER
fi
if [ "$SOCKET_DIR" != "" ]; then
  rm -fr `dirname 2>/dev/null $SOCKET_DIR`/ksocket-$USER
fi
if [ "$TMP_DIR" != "" ]; then
  rm -fr `dirname 2>/dev/null $TMP_DIR`/kde-$USER
fi

Message_Prefix
echo "Updating *.desktop files."
find "$PROFILE_DIR" "$USER_DIR/.local" -name "*.desktop" -print0 2>/dev/null | \
  xargs -r0 grep -ZIl "\(X-KDE-\|KDE;\)" | \
  xargs -r0 sed -i -e "s|X-KDE-|X-TDE-|g" -e "s|KDE;|TDE;|g"
Message_Prefix
echo "Updating references of $TDEDIR/share/applications/kde to share/applications/tde."
# Exclude KMail mail files --- we don't want to touch those files.
find "$PROFILE_DIR" \
     -path $PROFILE_DIR/share/apps/amarok/albumcovers -prune -o \
     -path $PROFILE_DIR/share/apps/basket/baskets -prune -o \
     -path $PROFILE_DIR/share/apps/juk/covers -prune -o \
     -path $PROFILE_DIR/share/apps/kget/logs -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/autosave -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/dimap -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/imap -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/mail -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/search -prune -o \
     -path $PROFILE_DIR/share/apps/knotes -prune -o \
     -path $PROFILE_DIR/share/apps/kopete/logs -prune -o \
     -type f -print0 2>/dev/null | \
  xargs -r0 grep -ZIFl "$TDEDIR/share/applications/kde" | \
  xargs -r0 sed -i "s|$TDEDIR/share/applications/kde|$TDEDIR/share/applications/tde|g"

# Preserve keyboard shortcuts and input actions.
if [ -r "$PROFILE_DIR/share/config/khotkeysrc" ]; then
  Message_Prefix
  echo "Updating user-defined keyboard shortcuts in khotkeysrc."
  sed -i 's|CommandURL=kde-|CommandURL=tde-|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's|K Menu - kde-|TDE Menu - tde-|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's|Name=K Menu|Name=TDE Menu|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's|in KDE stands|in TDE stands|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's| use KDE| use TDE|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  Message_Prefix
  echo "Updating some text strings in khotkeysrc."
  sed -i 's|Go to KDE Website|Go to TDE Website|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's|www\.kde\.org|www\.trinitydesktop\.org|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's|KDE3\.1|TDE|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's|kde32b1|trinity2b1|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  sed -i 's|kde321|trinity21|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
fi
# Fix the some of the same text strings in kglobalshortcutsrc.
Message_Prefix
echo "Updating some text strings in kglobalshortcutsrc."
sed -i 's|Go to KDE Website|Go to TDE Website|g' "$PROFILE_DIR/share/config/kglobalshortcutsrc" 2>/dev/null
sed -i 's|www\.kde\.org|www\.trinitydesktop\.org|g' "$PROFILE_DIR/share/config/kglobalshortcutsrc" 2>/dev/null
sed -i 's|KDE3\.1|TDE|g' "$PROFILE_DIR/share/config/kglobalshortcutsrc" 2>/dev/null

# Preserve app preferences.
if [ -r $PROFILE_DIR/share/config/profilerc ]; then
  Message_Prefix
  echo "Updating user-defined app preferences in profilerc."
  sed -i 's|Application=kde-|Application=tde-|g' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
  sed -i 's|khtml|tdehtml|' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
  sed -i 's|kfile_|tdefile_|' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
  sed -i 's|kfilereplace|tdefilereplace|' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
fi

# Preserve kicker/panel icons.
if [ -r $PROFILE_DIR/share/config/kickerrc ]; then
  Message_Prefix
  echo "Updating kicker/panel customizations in kickerrc."
  if [ -r "$PROFILE_DIR/share/config/kickerrc" ]; then
    sed -i 's|StorageId\[\$e\]=kde-|StorageId\[\$e\]=tde-|g' "$PROFILE_DIR/share/config/kickerrc" 2>/dev/null
  else
    Message_Prefix
    echo "kickerrc does not exist."
  fi
fi

# Preserve Quick Launch icons.
# There should only be one configuration file, but old KDE3 remnant files might exist ofr users who
# migrated from KDE3. We don't care about those files, but we still need to update the correct configuration file.
Message_Prefix
echo "Updating Quick Launch applet."
if [ -r "$PROFILE_DIR/share/config/kickerrc" ]; then
  QUICK_LAUNCH_CONFIG="`grep launcher_panelapplet $PROFILE_DIR/share/config/kickerrc | awk -F = '{print $2}'`"
  if [ "$QUICK_LAUNCH_CONFIG" != "" ]; then
    sed -i 's|,kde-|,tde-|g' "$PROFILE_DIR/share/config/$QUICK_LAUNCH_CONFIG" 2>/dev/null
  else
    Message_Prefix
    echo "Quick Launch is not installed."
  fi
else
  Message_Prefix
  echo "Quick Launch is not installed."
fi

Message_Prefix
echo "Updating Quanta Plus plugins.rc."
if [ -r $PROFILE_DIR/share/apps/quanta/plugins.rc ]; then
  sed -i 's|FileName=kde3|FileName=trinity|g' "$PROFILE_DIR/share/apps/quanta/plugins.rc" 2>/dev/null
else
  Message_Prefix
  echo "$PROFILE_DIR/share/apps/quanta/plugins.rc does not exist."
fi

# Update sym link files in $USER_DIR/.trinity/Autostart.
if [ -d "$PROFILE_DIR/Autostart" ]; then
  ( cd "$PROFILE_DIR/Autostart"
    Message_Prefix
    echo "Updating Autostart files."
    for i in `find . -type l`; do
      LINK="`readlink $i`"
      LINK_PATH="`dirname $LINK`"
      LINK_NAME="`basename $LINK`"
      if [ -n "`echo $LINK_PATH | grep \"$TDEDIR/share/applications/kde\"`" ]; then
        NEW_LINK_PATH="`echo \"$LINK_PATH\" | sed 's|/share/applications/kde|/share/applications/tde|'`"
      fi
      unlink $i
      ln -sf "$NEW_LINK_PATH/$LINK_NAME" "$LINK_NAME"
      if [ "$?" != "0" ]; then
        Message_Prefix
        echo "There was an error with creating a new sym link for $LINK." 1>&2
        KDEGLOBALS_KEY_VALUE="autostart"
      fi
    done
  )
else
  Message_Prefix
  echo "Autostart directory not found."
fi

# Update the user's customized menu.
if [ -r $USER_DIR/.config/menus/applications-kmenuedit.menu ]; then
  sed -i 's|<Filename>kde-|<Filename>tde-|g' $USER_DIR/.config/menus/applications-kmenuedit.menu
fi

# Ensure all KDED services are accounted for in the user's profile. Any that are missing
# are defaulted to not auto-loading (false). Refer to bug report 1210. This same test is
# performed in the migratekde3 script, but notice the migratekde3 script uses the key of
# X-KDE-Kded-autoload whereas X-TDE-Kded-autoload is used here.
if [ -d $PROFILE_DIR/share/services/kded ]; then
  if [ "`find $PROFILE_DIR/share/services/kded -name *.desktop`" != "" ]; then
    Message_Prefix
    echo "Validating KDED services."
    for i in `/bin/ls -1 $PROFILE_DIR/share/services/kded/*.desktop`; do
      SERVICE_NAME=`basename $i`
      if [ ! -f $TDEDIR/share/services/kded/$SERVICE_NAME ]; then
        $TDEDIR/bin/kwriteconfig --file $i --group "Desktop Entry" --key "X-TDE-Kded-autoload" --type bool "false"
      fi
    done
  fi
fi

# Try to update sessions.
sed -i 's|kwin|twin|g' $PROFILE_DIR/share/config/ksmserverrc
for i in `/bin/ls -1 $PROFILE_DIR/share/config/session/kwin_* 2>/dev/null`; do
  mv $i $PROFILE_DIR/share/config/session/`basename $i | sed 's|kwin_|twin_|'`
done

Message_Prefix
echo "Renaming some configuration files and directories."
# Note: The only rebranding that occured before starting the R14 branch was krita. All other
# rebranding updates belong in this script.
# Don't force renaming in case this script is used to update an existing Trinity profile. That is,
# always check whether the new config file already exists.
if [ ! -f $PROFILE_DIR/share/config/tdeprintrc ] && [ -f $PROFILE_DIR/share/config/kdeprintrc ] || [ -d $PROFILE_DIR/share/apps/kdeprint ]; then
  Message_Prefix
  echo "  kdeprint->tdeprint"
  mv $PROFILE_DIR/share/config/kdeprintrc $PROFILE_DIR/share/config/tdeprintrc 2>/dev/null
  mv $PROFILE_DIR/share/apps/kdeprint $PROFILE_DIR/share/apps/tdeprint 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdesurc ] && [ -f $PROFILE_DIR/share/config/kdesurc ]; then
  Message_Prefix
  echo "  kdesurc->tdesurc"
  mv $PROFILE_DIR/share/config/kdesurc $PROFILE_DIR/share/config/tdesurc 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdeveloprc ] && [ -f $PROFILE_DIR/share/config/kdeveloprc ]; then
  Message_Prefix
  echo "  kdevelop->tdevelop"
  mv $PROFILE_DIR/share/config/kdeveloprc $PROFILE_DIR/share/config/tdeveloprc 2>/dev/null
fi
# kwin/twin is the Trinity window manager. kwin4/twin4 is a game.
if [ ! -f $PROFILE_DIR/share/config/twinrc ] && [ -f $PROFILE_DIR/share/config/kwinrc ] || [ -d $PROFILE_DIR/share/apps/kwinrulesrc ]; then
  # Do not include kwinrules_update because that is an auto-generated file.
  Message_Prefix
  echo "  kwin->twin"
  mv $PROFILE_DIR/share/config/kwinrc $PROFILE_DIR/share/config/twinrc 2>/dev/null
  mv $PROFILE_DIR/share/config/kwinrc.eventsrc $PROFILE_DIR/share/config/twinrc.eventsrc 2>/dev/null
  mv $PROFILE_DIR/share/config/kwinrc $PROFILE_DIR/share/config/twinrc 2>/dev/null
  mv $PROFILE_DIR/share/apps/kwinrulesrc $PROFILE_DIR/share/apps/twinrulesrc 2>/dev/null
  mv $PROFILE_DIR/share/apps/kwin_rules_dialogrc $PROFILE_DIR/share/apps/twin_rules_dialogrc 2>/dev/null
  sed -i 's|PluginLib=kwin_|PluginLib=twin_|' $PROFILE_DIR/share/config/twinrc
fi
if [ ! -f $PROFILE_DIR/share/config/twin4rc ] && [ -f $PROFILE_DIR/share/config/kwin4rc ]; then
  Message_Prefix
  echo "  kwin4->twin4"
  mv $PROFILE_DIR/share/config/kwin4rc $PROFILE_DIR/share/config/twin4rc 2>/dev/null
fi
if [ ! -d $PROFILE_DIR/share/config/tderesources ] && [ -d $PROFILE_DIR/share/config/kresources ]; then
  Message_Prefix
  echo "  kresources->tderesources"
  mv $PROFILE_DIR/share/config/kresources $PROFILE_DIR/share/config/tderesources 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdeab2tdeabcrc ] && [ -f $PROFILE_DIR/share/config/kab2kabcrc ]; then
  Message_Prefix
  echo "  kab2kabcrc->tdeab2tdeabcrc"
  mv $PROFILE_DIR/share/config/kab2kabcrc $PROFILE_DIR/share/config/tdeab2tdeabcrc 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdeconf_updaterc ] && [ -f $PROFILE_DIR/share/config/kconf_updaterc ]; then
  Message_Prefix
  echo "  kconf_updaterc->tdeconf_updaterc"
  mv $PROFILE_DIR/share/config/kconf_updaterc $PROFILE_DIR/share/config/tdeconf_updaterc 2>/dev/null
  mv $PROFILE_DIR/share/apps/kconf_update $PROFILE_DIR/share/apps/tdeconf_update 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdeio_httprc ] && [ -f $PROFILE_DIR/share/config/kio_httprc ]; then
  Message_Prefix
  echo "  kio_httprc->tdeio_httprc"
  mv $PROFILE_DIR/share/config/kio_httprc $PROFILE_DIR/share/config/tdeio_httprc 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdeioslaverc ] && [ -f $PROFILE_DIR/share/config/kioslaverc ]; then
  Message_Prefix
  echo "  kioslaverc->tdeioslaverc"
  mv $PROFILE_DIR/share/config/kioslaverc $PROFILE_DIR/share/config/tdeioslaverc 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdelaunchrc ] && [ -f $PROFILE_DIR/share/config/klaunchrc ]; then
  Message_Prefix
  echo "  klaunchrc->tdelaunchrc"
  mv $PROFILE_DIR/share/config/klaunchrc $PROFILE_DIR/share/config/tdelaunchrc 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tderandrtrayrc ] && [ -f $PROFILE_DIR/share/config/krandrtrayrc ]; then
  Message_Prefix
  echo "  krandrtrayrc->tderandrtrayrc"
  mv $PROFILE_DIR/share/config/krandrtrayrc $PROFILE_DIR/share/config/tderandrtrayrc 2>/dev/null
fi
if [ ! -f $PROFILE_DIR/share/config/tdewalletrc ] && [ -f $PROFILE_DIR/share/config/kwalletrc ]; then
  Message_Prefix
  echo "  kwalletrc->tdewalletrc"
  mv $PROFILE_DIR/share/config/kwalletrc $PROFILE_DIR/share/config/tdewalletrc 2>/dev/null
  mv $PROFILE_DIR/share/apps/kwallet $PROFILE_DIR/share/apps/tdewallet 2>/dev/null
fi
if [ ! -d $PROFILE_DIR/share/apps/tdefile ] && [ -d $PROFILE_DIR/share/apps/kfile ]; then
  Message_Prefix
  echo "  kfile->tdefile"
  mv $PROFILE_DIR/share/apps/kfile $PROFILE_DIR/share/apps/tdefile 2>/dev/null
fi

# Disable some features new to R14, otherwise users will see an unfamiliar desktop.
if [[ $($TDEDIR/bin/kreadconfig --file kickerrc --group "General" --key "ShowDeepButtons" --default false) == "false" ]]; then
  $TDEDIR/bin/kwriteconfig --file kickerrc --group "General" --key "ShowDeepButtons" --type bool "false"
fi
if [[ $($TDEDIR/bin/kreadconfig --file kickerrc --group "General" --key "UseResizeHandle" --default false) == "false" ]]; then
  $TDEDIR/bin/kwriteconfig --file kickerrc --group "General" --key "UseResizeHandle" --type bool "false"
fi
if [[ $($TDEDIR/bin/kreadconfig --file kickerrc --group "General" --key "MenubarPanelBlurred" --default false) == "false" ]]; then
  $TDEDIR/bin/kwriteconfig --file kickerrc --group "General" --key "MenubarPanelBlurred" --type bool "false"
fi

# Perform some nominal update validations.
# First clean house from any previous failures.
if [ -z "$CACHE_DIR" ]; then
  CACHE_DIR=$PROFILE_DIR
fi
if [ -d "$CACHE_DIR" ]; then
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test*.txt 2>/dev/null
else
  # Create this directory in case the migratekde3 script was run immediately
  # before this script, which means the cache directory will not yet exist.
  mkdir "$CACHE_DIR"
fi
# This first test includes *.desktop files in the profile Autostart directory.
TEST_NUM="1"
R14_UPDATE_TEST1=""
find "$PROFILE_DIR" -name "*.desktop" -print0 2>/dev/null | \
  xargs -r0 grep -IFl "X-KDE-" >${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
  R14_UPDATE_TEST1="failed"
  MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check *.desktop files for 'X-KDE' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
  Validation_Failure
else
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
fi

TEST_NUM="2"
R14_UPDATE_TEST2=""
find "$PROFILE_DIR" -name "*.desktop" -print0 2>/dev/null | \
  xargs -r0 grep -IFl "KDE;" >${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
  R14_UPDATE_TEST2="failed"
  MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check *.desktop files for 'KDE;' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
  Validation_Failure
else
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
fi
TEST_NUM="3"
R14_UPDATE_TEST3=""
find "$PROFILE_DIR" \
     -path $PROFILE_DIR/share/apps/amarok/albumcovers -prune -o \
     -path $PROFILE_DIR/share/apps/basket/baskets -prune -o \
     -path $PROFILE_DIR/share/apps/juk/covers -prune -o \
     -path $PROFILE_DIR/share/apps/kget/logs -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/autosave -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/dimap -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/imap -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/mail -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/search -prune -o \
     -path $PROFILE_DIR/share/apps/knotes -prune -o \
     -path $PROFILE_DIR/share/apps/kopete/logs -prune -o \
     -type f -print0 2>/dev/null | \
  xargs -r0 grep -IFl "$TDEDIR/share/applications/kde" >${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
  R14_UPDATE_TEST3="failed"
  MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check files for '$TDEDIR/share/applications/kde' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
  Validation_Failure
else
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
fi
TEST_NUM="4"
R14_UPDATE_TEST4=""
if [ -r "$PROFILE_DIR/share/config/khotkeysrc" ]; then
  grep "CommandURL=kde-" "$PROFILE_DIR/share/config/khotkeysrc" &>${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST4="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check khotkeysrc for 'CommandURL=kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="5"
R14_UPDATE_TEST5=""
if [ -r "$PROFILE_DIR/share/config/khotkeysrc" ]; then
  grep "K Menu - kde-" "$PROFILE_DIR/share/config/khotkeysrc" &>${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST5="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check khotkeysrc for 'K Menu - kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="6"
R14_UPDATE_TEST6=""
if [ -r $PROFILE_DIR/share/config/profilerc ]; then
  grep "Application=kde-" "$PROFILE_DIR/share/config/profilerc" &>${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST6="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check profilerc for 'Application=kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="7"
R14_UPDATE_TEST7=""
if [ -r $PROFILE_DIR/share/config/kickerrc ]; then
  grep "StorageId\[\$e\]=kde-" "$PROFILE_DIR/share/config/kickerrc" &>${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST7="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check kickerrc for 'StorageId[$e]=kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="8"
R14_UPDATE_TEST8=""
if [ "$QUICK_LAUNCH_CONFIG" != "" ]; then
  grep "kde-" "$PROFILE_DIR/share/config/$QUICK_LAUNCH_CONFIG" &>${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST8="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check $QUICK_LAUNCH_CONFIG for 'kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="9"
R14_UPDATE_TEST9=""
if [ -r $USER_DIR/.config/menus/applications-kmenuedit.menu ]; then
  grep "<Filename>kde-" "$USER_DIR/.config/menus/applications-kmenuedit.menu" &>${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST9="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\n\n(Check applications-kmenuedit.menu for '<Filename>kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt.)"
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
if [ "$R14_UPDATE_TEST1" = "" ] && [ "$R14_UPDATE_TEST2" = "" ] && [ "$R14_UPDATE_TEST3" = "" ] \
  && [ "$R14_UPDATE_TEST4" = "" ] && [ "$R14_UPDATE_TEST5" = "" ] && [ "$R14_UPDATE_TEST6" = "" ] \
  && [ "$R14_UPDATE_TEST7" = "" ] && [ "$R14_UPDATE_TEST8" = "" ] && [ "$R14_UPDATE_TEST9" = "" ]; then
  $TDEDIR/bin/kwriteconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated --type bool "true"
  Message_Prefix
  echo "R14 XDG updates completed successfully."
else
  # Don't use the --type parameter here because the value no longer is boolean.
  $TDEDIR/bin/kwriteconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated "$KDEGLOBALS_KEY_VALUE"
  MESSAGE="The r14-xdg-update script did not complete successfully.\n\nThe script will run with each login until corrected.\n\nPlease contact an administrator or take appropriate\nadmininstrative action to correct the problem.\n\nThe error code is $KDEGLOBALS_KEY_VALUE.\n\nBe sure to check file and directory permissions."
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons OK > /dev/null 2>/dev/null
  else
    Message_Prefix
    printf "%b" "$MESSAGE"
  fi
fi

unset USER_DIR
unset PROFILE_DIR
unset R14_UPDATED
unset R14_UPDATE_TEST1
unset R14_UPDATE_TEST2
unset R14_UPDATE_TEST3
unset R14_UPDATE_TEST4
unset R14_UPDATE_TEST5
unset R14_UPDATE_TEST6
unset KDEGLOBALS_KEY_VALUE
unset TEST_NUM
exit 0