Added missing /bin/bash at the start, bug 696 still occurring for me
[dev-env-setup.git] / rm-deb-chroot
1 #!/bin/bash
2 if [ "$EUID" -ne 0 ]
3 then echo "Please run as root"
4 exit
5 fi
6 if [ -z "$1" ]
7 then echo "Need arg $1 to specify which chroot to delete"
8 exit
9 fi
10
11 chrootdir="$1"
12 chroot_path=/opt/chroot/$chrootdir
13 chroot_config_path=/etc/schroot/$chrootdir
14
15 schroot_main_config=/etc/schroot/schroot.conf
16 backup_schroot_config="/tmp/schroot.conf.orig"
17 sleep_time=1
18
19 echo "----------------------------------------------"
20 echo "WARNING! If the script finds directories:"
21 echo "$chroot_path"
22 echo "$chroot_config_path"
23 echo "It will DELETE them!"
24 echo ""
25 echo "The [$chrootdir] chroot profile from:"
26 echo "$schroot_main_config"
27 echo "will also be removed."
28 echo ""
29 echo "The script will wait for $sleep_time second/s before starting"
30 echo "Press Ctrl-C now to cancel."
31 echo "----------------------------------------------"
32 sleep $sleep_time
33
34 if [ -e $chroot_path ]; then
35 echo "Deleting $chroot_path"
36 rm -rf $chroot_path
37 else
38 echo "$chroot_path not found, skipping."
39 fi
40
41 if [ -e $chroot_config_path ]; then
42 echo "Deleting $chroot_config_path configuration directory"
43 rm -rf $chroot_config_path
44 else
45 echo "$chroot_config_path not found, skipping."
46 fi
47
48 # Find where the entry for given scroot profile is in the file.
49 # Return line number corresponding to the start.
50 lin_num_start=$(grep -n "\[$chrootdir\]" $schroot_main_config | cut -d : -f 1)
51
52 if [ -z $lin_num_start ]; then
53 echo "The [$chrootdir] chroot profile in $schroot_main_config does not exist"
54 echo "Skipping $schroot_main_config"
55 else
56 echo "The [$chrootdir] chroot profile found in $schroot_main_config"
57 # Can't think of a good way to make this clean
58 # Assumption: the profile always ends with "profile=$chrootdir",
59 # which will indicate the last line to delete of the old chroot
60 lin_num_end=$(grep -n "profile=$chrootdir" $schroot_main_config | cut -d : -f 1)
61 lines="$lin_num_start,$lin_num_end"
62 echo "Line range to delete from $schroot_main_config: $lines"
63
64 echo "Creating backup copy of $schroot_main_config in $backup_schroot_config"
65 cp $schroot_main_config $backup_schroot_config
66 echo "Script will re-write $schroot_main_config by"
67 echo "removing the old $chrootdir profile"
68 sed -e "$lines"'d' $backup_schroot_config > $schroot_main_config
69 fi