#!/usr/local/bin/perl -w # Author: Vladimir Bogdanov (Monk ID: c0d34w4y) # License? I guess GNU would apply. use strict; ######################## ### MAIN ######################## print intro(); my $pwd = `pwd`; chomp($pwd); exit unless (user_prompt("Add working directory '$pwd' to existing CVS repository? (y/n): ", "[yY]")); unless(exists $ENV{CVSROOT} && length($ENV{CVSROOT})) { $ENV{CVSROOT} = user_ask("\nEnvironment variable CVSROOT is not set! What should I set it to?\n CVSROOT: "); check_cvsroot(); } else { print "Will use existing CVSROOT '" . $ENV{CVSROOT} ."'\n"; $ENV{CVSROOT} = user_ask("\nNew CVSROOT: ") unless (user_prompt("Confirm if you are OK with it? (y/n): ", "[yY]")); check_cvsroot(); } my $module = user_ask("\nRepository module name for the working directory: "); my $cvs_module_path = "$ENV{CVSROOT}/$module"; my $cvs_dir = "$pwd/CVS"; eval { if (-d $cvs_dir) { print "\nDirectory '$cvs_dir' already exists!\n" ."Which implies that this working directory may belong to an existing cvs repository."; exit unless user_prompt("\nProceed anyway? (will replace the contents of '$cvs_dir' cvs directory): ", "[yY]"); } unless (-d $cvs_module_path) { print "\nDirectory '$cvs_module_path' doesn't exist! "; mkdir_rec($cvs_module_path) if (user_prompt("Create One? (y/n): ", "[yY]")); } print "\nInitializing..."; mkdir_rec($cvs_dir) unless (-d $cvs_dir); open(FOUT, ">$cvs_dir/Repository") or die "can't open '$cvs_dir/Repository'"; print FOUT "$module"; close(FOUT); open(FOUT, ">$cvs_dir/Root") or die "can't open '$cvs_dir/Root'"; print FOUT $ENV{CVSROOT}; close(FOUT); touch("$cvs_dir/Entries"); print "\nDone!\n"; }; if ($@) { print "\nFAILED: $@\n"; exit(0); } ######################## ### SUBS ######################## sub check_cvsroot { unless (-d $ENV{CVSROOT}) { print "Directory " . $ENV{CVSROOT} . " doesn't exist!\n"; exit; } } sub user_ask { print $_[0]; my $answer = ; chomp($answer); return $answer; } sub user_prompt { my $answer = user_ask($_[0]); return ($answer =~ m/$_[1]/); } #--------------------------------------------------------------------- # mkdir_rec($dir, $mode, $safe) # # safe = 1 - return appropriate numerical code instead of a die. # 0 - die. (default) # sub mkdir_rec { my ($dir, $mode, $safe) = @_; return 0 unless ($dir); # $dir required. return 1 if (-d $dir); # return if already exists my $mode_o = ($mode)?"-m $mode":""; # execute shell comand: use -p to create directories recursively # back tick command should return 0 on failure. system("mkdir -p $dir $mode_o") == 0 or ($safe ? return 0 : die "Failed to create '$dir'."); return 1; } sub touch { my $now = time; local (*TMP); foreach my $file (@_) { utime ($now, $now, $file) || open (TMP, ">>$file") || die ("Couldn't touch file: $!\n"); } } sub intro { return ; } __DATA__ #----------------------------------------------------------------- # A CVS IMPORT like Utility # # Associates your current directory with an existing CVS # repository. Similar to what a 'cvs import' command would do. # # by Vladimir Bogdanov (email: user=b_vlad server=telus.net, Perl Monk ID: c0d34w4y) #----------------------------------------------------------------- #### print join(" ", map { sprintf "%#02x", $_ }unpack("C*",pack("L",0x12345678)))