1#!/usr/bin/bin/perl -w 2# 3# resetenv.pl v1.0 4# 5# (C)1998 University of Minnesota, Department of Computer Science 6# 7# Authors: Mark A. Bentley <bentlema@cs.umn.edu> 8# Kent Mein <mein@cs.umn.edu> 9# Scott M. Dier <sdier@cs.umn.edu> 10# 11# Description: resetenv is a simple perl script to restore a set of 12# default dotfiles to users home directory. The dotfiles are stored 13# in a ~template account, and when resetenv is envoked, it copies 14# those dotfiles, as specified in the resetenv.conf, to the user's 15# home directory (who executed resetenv), saving the old dotfiles to 16# a unique directory. The user should use .resetenvrc for their 17# configuration options. 18# 19# A sample resetenv.conf is as follows: 20# 21# .aliases N 600 22# .cshrc Y 600 23# .fvwm2rc Y 600 24# .login Y 600 25# .logout Y 600 26# .xinitrc Y 755 27# README.dotfiles Y 600 28# .mailcap N 600 29# .mime.types N 600 30# 31# The first field is the filename to move into the users home directory 32# when resetenv is envoked. The second field is the "replace me?" field. 33# This is a "Y" (Yes)or "N" (No) answering the question of weather to 34# replace a particular dotfile that already exists in the users home 35# directory. This is most useful to the user who creates their own 36# config file (~/.resetenv) so that they have direct control over which 37# dotfiles are replaced with defaults, and which ones are not touched. 38# The third field is the permissions to apply to a newly copied dotfile. 39# 40################################################################################ 41# 42# Modifications History: 43# 44# 10/30/1998 -- PATH is now set in case user's PATH doesn't have needed commands 45# -- Now root can't run me 46# 08/04/2009 -- Works with directories now 47# 48################################################################################ 49# 50# Configuration 51# 52# 53# This is the directory where old dotfiles will be saved 54 55use Sys::Hostname; 56 57$backupdir = "old-dotfiles"; 58 59# This is the path that is set so that the commands that resetenv uses 60# (such as cp and mv) can be found even if the users PATH is foobared. 61 62$ENV{PATH} = '/opt/gnu/bin:/opt/local/bin:/usr/bin:/bin'; 63print "PATH = ", $ENV{PATH}, "\n"; 64 65# find ~template 66# 67# Currently the default dotfiles are in a homedir. They can be located 68# anywhere that is convenient really, however we use a cross-mounted home 69# directory for ease of maintenance. Perhaps it might be nice to have 70# different sets of dotfiles for different clusters, but this works 71# fine for now. --Mark 72# 73# Note: getpw functions return ($name, $passwd, $uid, $gid, $quota, 74# $comment, $gcos, $homedir, $shell) 75 76@template = getpwnam("template"); 77$templatedir = $template[7]; 78unless ( -d $templatedir) { die "Can't find templatedir $templatedir: $!";} 79print "Template dotfiles location: $templatedir\n\n"; 80 81unless ( -f "$templatedir/resetenv.conf" ) { 82 die "$templatedir/resetenv.conf does not exist: $!"; 83} 84 85$configfile = "$templatedir/resetenv.conf"; 86 87# Find the homedir of the user 88# 89# Notes: $< is a special variable for the UID of the user running 90# this process. 91 92($name, $tmp, $uid, $gid, $tmp, $tmp, $gcos, $userdir, $tmp) = getpwuid($<); 93 94print "Username: $name UID: $uid GID: $gid\n"; 95print " Name: $gcos Homedir: $userdir\n\n"; 96 97if ( $name eq "root" ) { 98 print "Idiot! Don't run resetenv as root! Bailing out!\n\n"; 99 exit; 100} 101 102if (!($userdir =~ /\/home\/(.*)/)) { 103 print "resetenv can not be run on an account that has a " . 104 "homedir that is not in /home\n"; 105 exit; 106} 107 108# 109# Make a subdirectory in the user's home dir to keep old dotfiles 110# 111 112if ( ! -d "$userdir/$backupdir" ) { 113 print "Creating $userdir/$backupdir\n"; 114 mkdir ("$userdir/$backupdir",477) or die "mkdir: $!\n"; 115} 116 117($tmp,$tmp,$tmp,$dd,$mm,$yy,$tmp,$tmp,$tmp) = localtime(time); 118 119# 120# Create a directory to store old dotfiles 121# 122 123#$oldfilesdir = "$userdir/$backupdir/$mm-$dd-$yy"; 124my $mmddyyyy = sprintf '%02d-%02d-%04d', $mm + 1, $dd, $yy + 1900; 125$oldfilesdir = "$userdir/$backupdir/$mmddyyyy"; 126 127$x=1; 128$foo = "$oldfilesdir-$x"; 129while (-d $foo ) { 130 $x++; 131 $foo = "$oldfilesdir-$x"; 132} 133$oldfilesdir = "$oldfilesdir-$x"; 134print "Creating $oldfilesdir\n\n"; 135mkdir ("$oldfilesdir",477); 136 137# 138# Determine which config file to use 139# 140 141if ( -f "$userdir/.resetenvrc" ) { 142 $configfile = "$userdir/.resetenvrc"; 143} 144 145# 146# okay, now do the actual copying and such... 147# 148 149print "Using config file: $configfile\n\n"; 150 151# added Wed, 19 Dec 2001 17:02:16 -0600 152# -sdier 153 154open LOG, ">$oldfilesdir/log"; 155print LOG "#resetenv log, ".`date`."#ran by euid:egid:uid:gid:ppid:host\n$>:$):$<:$(:".getppid().":".hostname()."\n"; 156close LOG; 157 158open ( CONF, "$configfile" ); 159while ( <CONF> ) { 160 ( $dotfile, $replace, $mode ) = split; 161 print "$dotfile: "; 162 163 # if the file exists in the default directory 164 if ((-f "$templatedir/$dotfile")||(-d "$templatedir/$dotfile")){ 165 166 if (( -e "$userdir/$dotfile" ) && ( "$replace" eq "N" )) { 167 print "keeping current.\n"; 168 } elsif ( -e "$userdir/$dotfile") { 169 print "installing default.\n"; 170 system ("mv -f $userdir/$dotfile $oldfilesdir/$dotfile"); 171 system ("cp -r $templatedir/$dotfile $userdir/$dotfile"); 172 system ("chmod $mode $userdir/$dotfile"); 173 } else { 174 system ("cp -r $templatedir/$dotfile $userdir/$dotfile"); 175 system ("chmod $mode $userdir/$dotfile"); 176 } 177 178 } else { 179 180 if (($dotfile ne ".") && ($dotfile ne "..")){ 181 print "\nAlert! $dotfile not found in $templatedir\n\n"; 182 } 183 } 184} 185close ( CONF ); 186 187print "\nYour old dotfiles can be found in:\n\t$oldfilesdir\n"; 188 189