aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/switch-themes
blob: 083ea953b3722f00e6f8f751cf1acf78ab1dce77 (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
#!/bin/sh
# -*- Mode: shell-script -*-
# Copyright (C) 2002 by Chmouel Boudjnah <chmouel@mandrakesoft.com>
# Redistribution of this file is permitted under the terms of the GNU 
# Public License (GPL)
# 	$Id$	

bootsplashdir=/usr/share/bootsplash
sysconfig=/etc/sysconfig/bootsplash
bootdir=/boot

function list_available_themes () {
    local basename=
    for i in $bootsplashdir/themes/*;do
	basename=${i##*/}
	echo $basename
    done
}

function get_current_theme () {
    local opt= theme=
    while read opt ;do
	[[ $opt != THEME=* ]] && continue
	theme=${opt##*=}
    done < $sysconfig
    #[[ -z $theme ]] && theme="Mandriva"
    echo $theme
}

function read_link () {
    perl -e '{print readlink shift, "\n"}' $1
}

function lilo_switch_themes () {
    local theme=$1
    local message_from_theme=$bootsplashdir/themes/$theme/lilo/message
    local message_from_current=$bootdir/lilo/message

    #checking
    if [[ ! -f $message_from_theme ]];then
	return;
    fi
    
    #Make sure to point on lilo-graphic
    if [[ -L $bootdir/message ]];then
	point_to=$(read_link $bootdir/message)
	if [[ $point_to != message-graphic ]];then
	    rm -f $bootdir/message
	    ln -s message-graphic $bootdir/message
	fi
    fi

    if ! cmp -s $message_from_theme $message_from_current;then
	cp -f $message_from_theme $bootdir/message-graphic
    fi
}

function grub_switch_themes () {
    if [ -x /usr/sbin/grub-gfxmenu ]; then
	/usr/sbin/grub-gfxmenu --update-theme
    fi
}

function check_it () {
    local theme=$1
    local ok= c=

    for c in $(list_available_themes);do
	[[ $c == $theme ]] && ok=yes
    done
    if [[ -z $ok ]];then
	echo -e "Themes $theme doen't exist\n";
	bash $0 -l
	exit;
    fi
}

function update () {
    local theme=$1;

    rm -f /usr/share/gfxboot/themes/current
    ln -s $theme /usr/share/gfxboot/themes/current

    rm -f $bootsplashdir/current
    ln -s $theme $bootsplashdir/current
}

function switch_theme () {
    local toswitch=$1
    local current_theme=$(get_current_theme)
    local ok= c=

    #Seeding
    if [[ $current_theme != $toswitch ]];then
	ok=
	tmpfile=$(mktemp /tmp/.bootsplash.XXXXXXX)
	cp -f $sysconfig $tmpfile && rm -f $sysconfig
	while read line;do
	    if [[ $line == SPLASH=* ]];then 
		echo "SPLASH=auto" >> $sysconfig
		continue;
	    fi
	    if [[ $line == THEME=* ]];then 
		echo "THEME=$toswitch" >> $sysconfig
		ok=yes
		continue;
	    fi
	    echo $line >> $sysconfig
	done < $tmpfile
	if [[ -z $ok ]];then
	    echo "THEME=$toswitch" >> $sysconfig
	fi
	rm -f $tmpfile
	if [[ -d /etc/bootsplash/themes/$toswitch ]]; then 
	    rm -f /etc/bootsplash/themes/current
	    ln -s $toswitch /etc/bootsplash/themes/current
        fi
	update $toswitch
    fi
    #lilo_switch_themes $toswitch
    grub_switch_themes $toswitch
}

function update_boot () {
    if [[ -x /usr/sbin/bootloader-config ]]; then 
        /usr/sbin/bootloader-config --action update-splash --kernel-version $(uname -r)
    fi 
}

function usage () {
    basename=`basename $0`
    cat <<EOF
     $basename OPTIONS THEMES

OPTIONS:
       -c:  Tell you the current themes
       -l:  List themes available.
       -u:  Update current theme.
EOF
    exit 1
}
    
update=
while getopts uhlc opt;do
  case "$opt" in
      u) 
	  update=yes ;;
      c) 
	  echo -n "The current theme is: ";get_current_theme; exit;;
      l) 
	  echo "Available themes are :"
	  for i in $(list_available_themes);do echo -e "\t\t\t$i";done;
	  exit;
	  ;;
      h) usage;;
      *) usage;;
  esac
done
shift $((OPTIND - 1))

theme=$1

if [[ -n $update ]];then
    if [[ -n $theme ]];then
	echo "No argument when updating";
	echo
	usage
    fi
    update $(get_current_theme)
    exit;
fi

[[ -z $theme ]] &&  usage;

check_it $theme
switch_theme $theme
update_boot